ActiveMQ is a fully compliant JMS Provider released under the Apache 2.0 License which includes some easy use Enterprise Integration Patterns and many advanced features. In this tutorial we will learn how to install it and run a first example of it.
Disambigution ActiveMQ vs Artemis MQ
- ActiveMQ: ActiveMQ is an open-source project that originated at the Apache Software Foundation. It has been around for a longer period and has a large community of contributors and users.
- ArtemisMQ: ArtemisMQ is also an open-source project, but it is a separate project that was created by merging the HornetQ messaging system with ActiveMQ’s codebase. It is now part of the Apache ActiveMQ family.
This tutorial is about the former option (ActiveMQ). If you want to learn about Artemis MQ we recommend this article: Getting started with ActiveMQ Artemis
Installing ActiveMQ
First of all download ActiveMQ from http://activemq.apache.org/download.html . Once downloaded, extract the files from the ZIP file into a directory of your choice.
Once unzipped ActiveMQ, from a console window, change to the installation directory and run ActiveMQ:
cd c:\apache-activemq-5.10.0
Then type:
bin\activemq
If ActiveMQ is up and running without problems, the Window’s console window or the Unix command shell will display information similar to the following log line:
You can monitor ActiveMQ using the Web Console by pointing your browser at
From ActiveMQ 5.8 onwards the web apps is secured out of the box.
The default username and password is admin/admin. You can configure this in the conf/jetty-real.properties file.
# Defines users that can access the web (console, demo, etc.) # username: password [,rolename ...] admin: admin, admin user: user, user
Running a sample application
By default ActiveMQ broker can be used by remote clients using the TCP transport. The transport is available out of the box on port 61616. Let’s code a simple class which contains a message Producer and a Message consumer connecting to the TCP transport:
package com.sample; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.ExceptionListener; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; public class HelloActiveMQ { public static void main(String[] args) throws Exception { HelloWorldProducer producer = new HelloWorldProducer(); HelloWorldConsumer consumer = new HelloWorldConsumer(); Thread threadProducer = new Thread(producer); threadProducer.start(); Thread threadConsumer = new Thread(consumer); threadConsumer.start(); } public static class HelloWorldProducer implements Runnable { public void run() { try { // Create a ConnectionFactory ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "tcp://francesco-PC:61616"); // Create a Connection Connection connection = connectionFactory.createConnection(); connection.start(); // Create a Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createQueue("DemoQueue"); // Create a MessageProducer from the Session to the Topic or // Queue MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Create a messages String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode(); TextMessage message = session.createTextMessage(text); // Tell the producer to send the message System.out.println("Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName()); producer.send(message); // Clean up session.close(); connection.close(); } catch (Exception e) { System.out.println("Caught: " + e); e.printStackTrace(); } } } public static class HelloWorldConsumer implements Runnable, ExceptionListener { public void run() { try { // Create a ConnectionFactory ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "tcp://francesco-PC:61616"); // Create a Connection Connection connection = connectionFactory.createConnection(); connection.start(); connection.setExceptionListener(this); // Create a Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createQueue("DemoQueue"); // Create a MessageConsumer from the Session to the Topic or // Queue MessageConsumer consumer = session.createConsumer(destination); // Wait for a message Message message = consumer.receive(1000); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); System.out.println("Received: " + text); } else { System.out.println("Received: " + message); } consumer.close(); session.close(); connection.close(); } catch (Exception e) { System.out.println("Caught: " + e); e.printStackTrace(); } } public synchronized void onException(JMSException ex) { System.out.println("JMS Exception occured. Shutting down client."); } } }
Obviously you have to replace “tcp://francesco-PC:61616” with your machine address.
Compiling the example
In order to compile the example, include the following library which is located at the root of ActiveMQ installation:
- activemq-all-5.10.0.jar
The expected output on the console is:
Sent message: 681936641 : Thread-0 Received: Hello world! From: Thread-0 : 263751065
Now verify from the Admin console that the message has been Enqueued and Dequeued:
Conclusion
ActiveMQ is fully compatible with the JMS specification and provides a robust JMS API for building Java-based messaging applications. It offers features like message selectors, transactions, and durable subscriptions.