Platinum Partner
java,enterprise-integration,integration,.net & windows

ActiveMQ and .NET combined!

 ActiveMQ is one of the most popular messaging frameworks. For sure the most popular open source framework. Many people think that ActiveMQ works only with Java and this is not true at all. ActiveMQ can work with almost every popular language (including JavaScript!) through numerous protocols which it supports.

Today I will show you how to use ActiveMQ in .NET-based solutions.

Project setup

Using VS 2010's Extension Manger I installed NuGet Package Manager. After installation and VS 2010 restart, I created a project called ActiveMQNMS. I right-clicked it and selected "Manage NuGet packages...". In the search field I typed: "ActiveMQ". There was a package called Apache.NMS.ActiveMQ. I installed it. (Note: ActiveMQ has one dependency - Apache.NMS package. The NMS package provides a unified API for working with different messaging frameworks and providers.)

Starting ActiveMQ

I already had ActiveMQ installed on my machine. If you don't have one, download it from http://activemq.apache.org. The default instance listens on 61616 port. However, mine is listening on 62626. If you want to run my code, please remember to change the port.

To start ActiveMQ I executed:
activemq-5.5.0\bin\activemq 

Depending on configured ports, you can use ActiveMQ web console to manage your queues, topics, subscribers, connections, embedded Apache Camel, etc. I'm using 8282 port, and the console URL is: http://localhost:8282/admin.

Test stub

In general the .NET API is almost a copy of the Java API. So if you're familiar with JMS and/or ActiveMQ you don't need any documentation. Please note TestIntialize and TestCleanup methods.

using System;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ActiveMQNMS
{
    [Serializable]
    public class Person
    {
        public string FirstName
        {
            get;
            set;
        }
        public string LastName
        {
            get; set;
        }
    }
    [TestClass]
    public class ActiveMqTest
    {
        private IConnection _connection;
        private ISession _session;
        private const String QUEUE_DESTINATION = "DotNet.ActiveMQ.Test.Queue";
        [TestInitialize]
        public void TestInitialize()
        {
            IConnectionFactory factory = new ConnectionFactory("tcp://localhost:62626");
            _connection = factory.CreateConnection();
            _connection.Start();
            _session = _connection.CreateSession();
        }
        [TestCleanup]
        public void TestCleanup()
        {
            _session.Close();
            _connection.Close();
        }
    }
}

Writing Producer

Here is the producer:

[TestMethod]
public void TestA()
{
    IDestination dest = _session.GetQueue(QUEUE_DESTINATION);
    using (IMessageProducer producer = _session.CreateProducer(dest))
    {
        var person = new Person
        {
            FirstName = "Łukasz",
            LastName = "Budnik"
        };
        var objectMessage = producer.CreateObjectMessage(person);
        producer.Send(objectMessage);
    }
}

Run the test and refresh "Queues" list in ActiveMQ web console. You should see DotNet.ActiveMQ.Test.Queue queue with 1 enqueued and pending message. Purge the queue by hitting the purge link or you simply delete it.

Writing Consumer

Now we have to consume the message. Here is the code:

[TestMethod]
public void TestB()
{
    Person person = null;
    IDestination dest = _session.GetQueue(QUEUE_DESTINATION);
    using (IMessageConsumer consumer = _session.CreateConsumer(dest))
    {
        IMessage message;
        while ((message = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
        {
            var objectMessage = message as IObjectMessage;
            if (objectMessage != null)
            {
                person = objectMessage.Body as Person;
                if (person != null)
                {
                    Assert.AreEqual("Łukasz", person.FirstName);
                    Assert.AreEqual("Budnik", person.LastName);
                }
            }
            else
            {
                Assert.Fail("Object Message is null");
            }
        }
    }
    if (person == null)
    {
        Assert.Fail("Person object is null");
    }
}

Run tests. Refresh "Queues" tab in ActiveMQ web console. You should see 1 message enqueued and 1 message dequeued. As expected.

Summary

That's all. Simple, isn't it? ActiveMQ works very, very nicely with .NET.

I have to find some performance comparison for ActiveMQ and MS or pure .C#/NET messaging frameworks. Or maybe you have it? Please share.

cheers,
Łukasz

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}