ActiveMQ tutorial 1: Getting started with ActiveMQ
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.
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.
Starting ActiveMQ
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:
INFO ActiveMQ JMS Message Broker (ID:francesco-PC-51222-1140729837569-0:0) started
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: