Over a million developers have joined DZone.

ActiveMQ and .NET combined!

· Integration Zone

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

 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:

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
    public class Person
        public string FirstName
        public string LastName
            get; set;
    public class ActiveMqTest
        private IConnection _connection;
        private ISession _session;
        private const String QUEUE_DESTINATION = "DotNet.ActiveMQ.Test.Queue";
        public void TestInitialize()
            IConnectionFactory factory = new ConnectionFactory("tcp://localhost:62626");
            _connection = factory.CreateConnection();
            _session = _connection.CreateSession();
        public void TestCleanup()

Writing Producer

Here is the producer:

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

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:

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


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.


Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.


Published at DZone with permission of Łukasz Budnik, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}