October 21, 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 »

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.

To begin, create a publisher as listed below:

static void Main(string[] args)
{ 
   //Create the Connection 
   Factory IConnectionFactory factory = new 
      ConnectionFactory("tcp://localhost:61616/"); 
   using (IConnection connection = factory.CreateConnection()) 
   { 
      //Create the Session 
      using (ISession session = connection.CreateSession()) 
      { 
         //Create the Producer for the topic/queue 
         IMessageProducer prod = session.CreateProducer( 
            new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(
            "testing")); 
         //Send Messages 
         int i = 0; 
         while (!Console.KeyAvailable) 
         { 
            ITextMessage msg = prod.CreateTextMessage(); 
            msg.Text = i.ToString(); 
            Console.WriteLine("Sending: " + msg.Text); 
            prod.Send(msg, true, 1, TimeSpan.MinValue); 
            System.Threading.Thread.Sleep(250); 
            i++; 
         } 
      } 
   } 
   Console.ReadLine(); 
}

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


Tags: ASP.NET, messaging, Apache



Page 1 of 2



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel