// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Transactions; using Apache.NMS; using Apache.NMS.ActiveMQ; namespace Tx.NonXAMQ { public class Smart : MQ { public override IConnection GetConnection() { IConnectionFactory cf = new NetTxConnectionFactory(new Uri("tcp://localhost:61616")); IConnection con = cf.CreateConnection(); con.Start(); return con; } public IConnection GetTxConnection() { IConnectionFactory cf = new NetTxConnectionFactory(new Uri("tcp://localhost:61616")); IConnection con = cf.CreateConnection(); con.Start(); return con; } public override void Test() { for(int i = 0; i < 10; i++) { using(IConnection con = GetTxConnection()) { using(ISession ses = con.CreateSession(AcknowledgementMode.AutoAcknowledge)) { using(TransactionScope tx = new TransactionScope()) // has to be inside session - otherwise it hangs at dispose { using(IQueue qin = ses.GetQueue("Qin"), qout = ses.GetQueue("Qout")) { using(IMessageConsumer receiver = ses.CreateConsumer(qin)) using(IMessageProducer sender = ses.CreateProducer(qout)) { try { IMessage msg = receiver.Receive(); if(i % 2 != 0) throw new Problem(); sender.Send(msg); tx.Complete(); } catch(Problem) { Console.WriteLine("Problem"); } } } } } } } } public static void Main(string[] args) { Demo(new Smart()); Console.ReadKey(); } } }