First steps with messaging in Java

First steps with messaging in Java

In this article, I will introduce some messaging concepts and try to emphasize some insights which are important for beginners. At the end of this article I will show a short example with ActiveMQ.

Types of messaging

Communication among processes can be synchronous or asynchronous and persistent or transient. If we want to build a complex distributed system, we have to use various combinations of communication. The rest of this article is focused on JMS, which is an interface of Message-Oriented Middleware and can be typically used for asynchronous persistent communication. In this short article you can see basic information about MOM and differences among other types of communication.

Typically, a sender (producer) program creates a message and pushes it to a queue and a receiver (consumer) program gets the message from the queue and processes it. Producer and consumer don’t know about their existence. Message queues are typically provided by Message Brokers. A Message Broker is a standalone application (service) that other applications connect to and send/receive messages. A Message Broker is responsible for storing messages until a receiver receives them. A Message Broker can route messages across machines to deliver a message to the destination application and can try delivering the message until the receiver correctly handles it. [1] Message broker as a message transfer agent is an extra component in the architecture and is the primary disadvantage of many message-oriented middleware systems [2].

ActiveMQ, HornetQ, RabbitMQ, ZeroMQ or blablaMQ?

We need to build asynchronous communication between two programs. Which library and implementation of Message Broker should we use? Do we really need a message broker or JMS compliant solution? ActiveMQ, HornetQ and RabbitMQ are message brokers and offer support for enterprise integration patterns. Basically all brokers are highly scalable, robust and reliable with some limitations. ZeroMq is a library to create distributed and concurrent applications. The ZeroMQ API is similar to the low level Socket API for communication over networks and also suitable for in-process asynchronous programming. Over the internet you can find some articles dealing with comparison of message brokers like this or Kuntal Ganguly’s comparison. I like the most Thomas Bayer’s comparison, where everything important is highlighted. Before you choose your …MQ solution, I recommend to you read this article.

Point-to-point vs Publish/Subscribe model

When we choose our messaging library and message broker, we should decide what kind of JMS destination is suitable. JMS destination is an object that represents the target of messages that the client produces and the source of messages that the client consumes. Queue represents a point-to-point model, where a single message is received by exactly one consumer and messages have to be delivered in the order sent. Topic represents a publish and subscribe model, where zero to multiple clients can subscribe to the message and there is no guarantee for the order of delivery from the messages sent. [3]

Messaging anti-patterns

We chose a library, message broker, destination and now we can build a message system, right? No, we can’t. If we don’t have any experience with messaging, we should know about common messaging anti-patterns. We don’t find them in documentations and should keep them in mind. Dejan Bosanac wrote great articles about messaging anti-patterns. I think the first is the most important, where you can find out how you shouldn’t use destinations. The second part is about managing connections and the third about message types.


[1] DotNetMQ: A Complete Message Queue System for .NET

[2] Message-oriented middleware

[3] JMS Queue – Difference between a Queue and a Topic




Leave a Reply

Your email address will not be published. Required fields are marked *