DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Efficiently Reading Large Excel Files (Over 1 Million Rows) Using the Open-Source Sjxlsx Java API
  • Build a REST API With Just 2 Classes in Java and Quarkus
  • How to Quarantine a Malicious File in Java

Trending

  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo
  • The Network Attach Problem Nobody Warns You About
  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  1. DZone
  2. Coding
  3. Languages
  4. ActiveMQ and .NET combined!

ActiveMQ and .NET combined!

By 
Łukasz Budnik user avatar
Łukasz Budnik
·
Apr. 15, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
29.4K Views

Join the DZone community and get the full member experience.

Join For Free

 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
Open source Test stub Console (video game CLI) Framework .NET API Java (programming language)

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

Opinions expressed by DZone contributors are their own.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Efficiently Reading Large Excel Files (Over 1 Million Rows) Using the Open-Source Sjxlsx Java API
  • Build a REST API With Just 2 Classes in Java and Quarkus
  • How to Quarantine a Malicious File in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook