What it is
Apache ActiveMQ is a popular open-source message broker that implements JMS (Java Message Service). The ActiveMQ Java Client library allows Java applications to send, receive, and process messages asynchronously and reliably across distributed systems.
ActiveMQ Java Client provides ConnectionFactory, Connection, Session, MessageProducer, MessageConsumer, Queue, and Topic APIs to facilitate message sending, receiving, and management. It supports both point-to-point (queue) and publish-subscribe (topic) messaging models.
Installation
Add dependency in pom.xml:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.18.5</version>
</dependency>Getting started
The smallest useful thing you can do with it, and what each part means.
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("TEST.QUEUE");
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage("Hello ActiveMQ!");
producer.send(message);
System.out.println("Sent: " + message.getText());
session.close();
connection.close();import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("TEST.QUEUE");
MessageConsumer consumer = session.createConsumer(queue);
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received: " + textMessage.getText());
}
session.close();
connection.close();Advanced usage
Where the library earns its place over a simpler alternative.
Topic topic = session.createTopic("TEST.TOPIC");
MessageProducer producer = session.createProducer(topic);
TextMessage message = session.createTextMessage("Broadcast message");
producer.send(message);producer.setDeliveryMode(DeliveryMode.PERSISTENT);
producer.send(message);consumer.setMessageListener(msg -> {
if (msg instanceof TextMessage) {
try {
System.out.println("Received asynchronously: " + ((TextMessage) msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- JMSException
- Occurs for messaging-related errors. Check connection settings, broker availability, and session configuration.
- MessageFormatException
- Occurs when a message cannot be serialized/deserialized properly. Ensure compatible message types.
- InvalidDestinationException
- Occurs when the specified queue or topic does not exist. Verify destination names and broker configuration.
Best practices
- Use persistent delivery mode for critical messages to prevent data loss.
- Choose queues for point-to-point messaging and topics for publish-subscribe patterns.
- Handle exceptions and reconnections gracefully to maintain reliability.
- Monitor message queues and consumers to avoid bottlenecks or memory issues.
- Consider using connection pooling for high-throughput applications.
Background
Why it exists, and what it was reacting to.
ActiveMQ was created by the Apache Software Foundation to provide a robust, high-performance, and enterprise-grade messaging solution. It supports a wide variety of messaging patterns, protocols, and integrations, making it suitable for microservices, event-driven systems, and enterprise applications.
