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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Build a REST API With Just 2 Classes in Java and Quarkus
  • How to Quarantine a Malicious File in Java
  • Model-Driven Development and Testing

Trending

  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • How to Convert XLS to XLSX in Java
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  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.1K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Build a REST API With Just 2 Classes in Java and Quarkus
  • How to Quarantine a Malicious File in Java
  • Model-Driven Development and Testing

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: