March 9, 2021
Hot Topics:

Getting started with Apache NMS Framework and Apache ActiveMQ

  • 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 
   //Create the connection 
   using (IConnection connection = 
      connection.ClientId = "testing listener"; 
      //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(
static void consumer_Listener(IMessage message) 
   Console.WriteLine("Receive: " + 

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).


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

Page 2 of 2

This article was originally published on June 9, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date