private ISession getSession() { if (session == null) { IConnectionFactory factory; IConnection connection; factory = new ConnectionFactory(new Uri(machineName)); connection = factory.CreateConnection(); session = connection.CreateSession(); } return session; }
public static void Main(string[] args) { try { Console.WriteLine("About to connect to ActiveMQ"); // START SNIPPET: demo IConnectionFactory factory = new ConnectionFactory(new Uri("tcp://localhost:61616")); using (IConnection connection = factory.CreateConnection()) { Console.WriteLine("Created a connection!"); ISession session = connection.CreateSession(); IDestination destination = session.GetQueue("FOO.BAR"); Console.WriteLine("Using destination: " + destination); // lets create a consumer and producer IMessageConsumer consumer = session.CreateConsumer(destination); IMessageProducer producer = session.CreateProducer(destination); producer.Persistent = true; // lets send a message ITextMessage request = session.CreateTextMessage("Hello World!"); request.NMSCorrelationID = "abc"; request.Properties["NMSXGroupID"] = "cheese"; request.Properties["myHeader"] = "James"; producer.Send(request); // lets consume a message ActiveMQTextMessage message = (ActiveMQTextMessage) consumer.Receive(); if (message == null) { Console.WriteLine("No message received!"); } else { Console.WriteLine("Received message with ID: " + message.NMSMessageId); Console.WriteLine("Received message with text: " + message.Text); } } // END SNIPPET: demo } catch (Exception e) { Console.WriteLine("Caught: " + e); Console.WriteLine("Stack: " + e.StackTrace); } }