October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Getting started with Apache NMS Framework and Apache ActiveMQ

  • June 9, 2009
  • By Chris Bennett
  • Send Email »
  • More Articles »

Creating the Subscriber

Now that you have a producer for the messages, you can create a consumer (a.k.a. subscriber) to receive the messages. Here's the source code for the consumer application:

static void Main(string[] args) 
{ 
   //Create the Connection factory 
   IConnectionFactory factory = new 
      ConnectionFactory("tcp://localhost:61616/"); 
   //Create the connection 
   using (IConnection connection = 
      factory.CreateConnection()) 
   { 
      connection.ClientId = "testing listener"; 
      connection.Start(); 
      
      //Create the Session 
      using (ISession session = connection.CreateSession()) 
      { 
         //Create the Consumer 
         IMessageConsumer consumer = session.CreateDurableConsumer(
            new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(
            "testing"),"testing listener",null,false); 
         consumer.Listener += new MessageListener(
            consumer_Listener); 
         Console.ReadLine(); 
      } 
   } 
} 
static void consumer_Listener(IMessage message) 
{ 
   Console.WriteLine("Receive: " + 
      ((ITextMessage)message).Text); 
} 

Similar to the producer code shown earlier, you first need to create an IConnectionFactory object and an IConnection. The connection.ClientId specifies a unique identity for this client. Save this value, because the client application must reuse that string to ensure that it receives the appropriate messages. Next, the call to connection.Start instructs the connection to be prepared to start receiving messages. Create an ISession object and an IMessageConsumer object. You can create two types of consumers: durable and non-durable. A non-durable consumer will receive all messages sent after the consumer is created. A durable consumer will receive all messages sent since the durable consumer was first created. For durable consumers, ActiveMQ holds all messages sent when the consumer was disconnected. Using a durable consumer is the way to go for most uses.

With both the Publisher and Subscriber created, you're ready to test the queue.

Running the Publisher and Subscriber

First, launch the Publisher console app (see Figure 1), which will start sending messages into the Topic queue named "testing."

Next, start the Subscriber console app. You should see that it's receiving messages (see Figure 2).

Conclusion

ActiveMQ provides excellent publish/subscribe capabilities and performs well under load. Adding Apache NMS makes it easy to incorporate ActiveMQ into .NET applications without the cost overhead of commercial solutions.

You've seen only very basic capabilities here, but be aware that ActiveMQ provides a wide range of configuration options, so it's well worth your time to read through the configuration documentation, so you can configure it correctly for your specific applications.

Source Code

Download Source Code: NMSPublishSubscribe.zip

About the Author

Chris Bennett works with Crowe Horwath LLP, in their Indianapolis office. He can be reached at chris.bennett@crowehorwath.com


Tags: ASP.NET, messaging, Apache



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel