Getting started with Apache NMS Framework and Apache ActiveMQ

Apache ActiveMQ is a very powerful Java-based message queue system with publish/subscribe capabilities that conforms to the Java Message Service (JMS) specification, and also supports integration with .NET applications. Apache does provide basic API documentationbut without examples. However, there's documentation for using the .NET Messaging Service (NMS) with Spring.NET. This article shows how to use the Apache ActiveMQ server together with NMS to create a simple publisher and subscriber.

Before creating the publisher/subscriber you need to download a copy of the Apache ActiveMQ server and the NMS DLLs. After downloading the ActiveMQ zip file, you will need to start the ActiveMQ server by navigating down to the bin folder and launching the activemq.bat batch file. Unfortunately, at the time of this article the NMS DLLs are available only in source code form, which you can download from http://activemq.apache.org/nms/source.html using a Subversion client. You'll find instructions for building the DLLs on the download page. The two application examples listed below need references to these DLLs.

Creating the Publisher

Two types of queues are available: Queue and Topic. Choose a Queue when you have one or many publishers sending messages but only a single subscriber. Use a Topic when you have one or many publishers and one or many subscribers. The examples here use the Topic queue type. You can download the source code for the sample applications to follow along.

The preceding code first creates an IConnectionFactory object, which obtains connections to a specific server. Next, it creates the connection and an ISession object to use with that connection. It uses the session object to create a Producer for a specified Queue or Topic. To create the producer, you need to specify whether you're connecting to a Queue or Topic, and provide a name. This example uses a Topic and specifies the topic name "testing." To produce messages, the code uses a loop with a small delay. The loop exits when a user presses a key; the delay gives users enough time to press a key, so the loop doesn't create a huge number of messages. Inside each loop iteration, the code creates an ITextMessage object from the Producer, populates the ITextMessage object with some text, and then sends it using the Producer. The send command provides a couple of useful parameters, including the option to persist the message on the server, the message priority, and the message TTL (Time to Live).