Apache ActiveMQ

Tushar Patel
8 min readMar 23, 2023

--

What is Apache ActiveMQ?

Apache ActiveMQ is an open-source messaging and integration patterns server. It is written in Java and provides messaging features like point-to-point messaging, publish-and-subscribe messaging, and many other messaging features. ActiveMQ supports various protocols such as AMQP, STOMP, OpenWire, and MQTT, and can be used with popular programming languages like Java, C++, Python, Ruby, PHP, and more.

ActiveMQ offers a high-performance, robust, and scalable messaging solution that can be used for a variety of use cases such as messaging between applications, integration of distributed systems, and decoupling of various components of an application.

ActiveMQ has a rich set of features such as message persistence, message priority, message grouping, transaction support, and more. It also provides a web-based admin console for managing the server and monitoring its health.

ActiveMQ is widely used in enterprise applications, and it has a large and active community that continuously develops and supports it.

  • ActiveMQ uses the Java Message Service (JMS) API, which is a standard for messaging in Java-based applications. It also provides a C++ API, as well as APIs for other programming languages.
  • It supports both synchronous and asynchronous messaging, allowing applications to send and receive messages in real-time or at a later time.
  • It provides a variety of message delivery modes, such as persistent and non-persistent messaging, to ensure reliable and efficient message delivery.
  • It supports message filtering and selectors, which allow applications to subscribe to only certain types of messages or messages that meet specific criteria.
  • ActiveMQ provides a pluggable architecture, allowing users to add custom functionality and extend its capabilities.
  • ActiveMQ has seamless integration with various Apache projects, such as Camel, CXF, and Service Mix, which enables users to build complex, distributed systems.
  • It has a robust and scalable clustering capability, which allows users to distribute message processing across multiple servers for high availability and performance.
  • ActiveMQ provides a web-based admin console, which makes it easy to manage and monitor the server and its components.

What Is ActiveMQ Used For?

like any other message broker, ActiveMQ is used as a communication bridge between multiple components that can be hosted on separate servers or can be written in different programming languages.

Message brokers like this one are often found in enterprise systems — or any systems that have a complex architecture. The goal of implementation is to create reliable communication between components of that system.

A good example is the financial and banking industry, where a high availability system is essential. Speed is important in this industry, but more important than that is the reliability of the entire system.

Even though we all want 100% uptime for everything, there will still be rare moments when a service is offline. What do you do in this case?

Putting your message into a message queue will usually work in this scenario. The message doesn’t get delivered and processed immediately. Instead, it will be processed when the other side of the service comes back online.

The example above is a niche case. But the overall use case of messaging is the ability to asynchronously shape traffic. There might be cases when the service is still online, but it may just not be able to process messages as quickly as they are produced into the system. Or it might not be able to handle a burst without some padding.

So, if you have a system — and it is a must for a message to be sent no matter what and no matter the time frame, now or later — then a message broker like ActiveMQ is what you need.

ActiveMQ Architecture Overview

ActiveMQ’s anatomy consists of data patterns called messages, queues, and topics.

Messages are just what they sound like, payloads of data that can be shipped between various systems. These payloads can have headers and properties associated with them that can:

  • Categorize them
  • Control various portions of their routing
  • Facilitate moving the payloads around a connected network of applications

Here’s a basic JMS structure, such as ActiveMQ:

The payload itself can be any Java object in a pure-Java implementation. But it is typically some form of text message. Note that the text could be a markup format such as XML or JSON as well. The purpose of a messaging system is to move these payloads between heterogeneous systems in a standard way.

How ActiveMQ Messages Work?

Once messages make their way into the system, they are arranged into two patterns: queues and topics. Queues areFIFO (first-in, first out) pipelines of messages produced and consumed by brokers and clients. Producers create messages and push them onto these queues. Then those messages are polled and collected by consumer applications, one message at a time.

Topics are subscription-based message broadcast channels. When a producing application sends a message, multiple recipients who are ‘subscribed’ to that topic receive a broadcast of the message. This producing application is sometimes called a ‘publisher’ in the context of topic messaging,

All of these components are just parts of the JMS specification, but aren’t any one piece of software in particular. To realize the specification in real life, we can create a vendor-specific implementation of the JMS spec, known as a JMS provider.

To sum up, the main difference between aqueue and a topic is:

  • A topic implements a publish and subscribe workflow.
  • A queue implements a load balancing workflow.

Creating Point to Point Messaging Model in ActiveMQ

Producer sends the message to a specified queue within JMS provider and the only one of the consumers who listening to that queue receives that message.

Step1: Create a Java Project

Using Eclipse create a simple Java-Maven Project.

Step2: Maven Setup

<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.15</version>
</dependency>
</dependencies>

Step3: Creating java class and Making a connection

Create new Java class and start adding ActiveMQ connection. We will make a method to establish the connection with the ActiveMQ Here is the code to make a connection:

First create simple Java class for message sending using ActiveMQ.

public class ActiveMQMessageSender {

//URL of the JMS server.
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

// default broker URL is : tcp://localhost:61616"
private static String jmsQueue = "example_QUEUE";

public static void main(String[] args) throws JMSException {
// Getting JMS connection from the server and starting it
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();

//Creating a session to send/receive JMS message.
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

//The queue will be created automatically on the server.
Destination destination = session.createQueue(jmsQueue);

// MessageProducer is used for sending messages to the queue.
MessageProducer producer = session.createProducer(destination);

// We will send a small text message with small Json'
TextMessage message = session
.createTextMessage("{\"SampleJson\": {\"userId\": \"simplifyingTech\" } }");

// Here we are sending our message!
producer.send(message);

System.out.println("JMS Message Sent successfuly:: " + message.getText());
connection.close();
}
}

output:

Note: We can add whatever message we want.

Step4: Verify message on server.

Step5: Message Receiver / Consumer

This class is used to receive the text message from the queue.

public class ActiveMQMessageReceiver {

// URL of the JMS server
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
// default broker URL is : tcp://localhost:61616"

//The Queue receive messages from
private static String jmsQueue = "example_QUEUE";

public static void main(String[] args) throws JMSException {
// Getting JMS connection from the server
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();

// Creating session for sending messages
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

// Getting the queue
Destination destination = session.createQueue(jmsQueue);

// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = session.createConsumer(destination);

// Here we receive the message.
Message message = consumer.receive();

//We will be using TestMessage in our example. MessageProducer sent us a TextMessage
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("JMS Message Received successfully: '" + textMessage.getText() + "'");
}
connection.close();
}
}

Output:

Step6: Verify on Server.

ActiveMQ Key Functions

Over and above JMS, ActiveMQ provides additional functionality such as:

  • A native ActiveMQ connection factory object for Java.
  • Powerful observability features with its detailed JMX metrics and statistics engine.
  • Extended dashboarding through Jolokia and HawtIO.
  • JAAS security.
  • Multiple connection protocols.
  • Native HA and DR capabilities.
  • Built-in functionality for both horizontal and vertical scale.

Should You Use ActiveMQ?

ActiveMQ is a mature and well-adopted platform. 1,000s of companies use it. It’s by far the most flexible OSS message broker available.

As if that’s not enough, it also ships with Apache Camel and shares developers and committers with the Apache Camel product. This gives it a tremendous level of sophistication in terms of designing complex messaging patterns.

Get More Out of ActiveMQ

Though not the only free messaging solution available, Apache ActiveMQ stands out for balancing its versatility with enterprise-readiness. You can adopt ActiveMQ quickly. It will be accessible to anyone comfortable with JMS. ActiveMQ’s prolific community will ensure the products continued viability. And ActiveMQ 6 promises to extend this usefulness into the next generation of enterprise messaging solutions.

References

https://www.openlogic.com/blog/what-apache-activemq

https://simplifyingtechcode.wordpress.com/2021/12/16/introduction-to-jms-activemq-through-java-programs/

Conclusion

Overall, Apache ActiveMQ is a versatile messaging platform with a rich set of features, which makes it an ideal choice for building distributed, scalable, and robust applications.

Contributors: Tushar Patel, VRAJ PATEL, VanshPatel

--

--