- Apache ActiveMQ - Home
- Apache ActiveMQ - Overview
- Apache ActiveMQ - Environment Setup
- Apache ActiveMQ - Features
- Apache ActiveMQ - Running Broker Server
- Apache ActiveMQ - Admin Console
Apache ActiveMQ Queue Based Example
- Apache ActiveMQ - Producer Application
- Apache ActiveMQ - Consumer Application
- Apache ActiveMQ - Test Application
Apache ActiveMQ Topic Based Example
- Apache ActiveMQ - Publisher Application
- Apache ActiveMQ - Subscriber Application
- Apache ActiveMQ - Test Application
Apache ActiveMQ Useful Resources
Apache ActiveMQ - Quick Guide
Apache ActiveMQ - Overview
What is ActiveMQ?
ActiveMQ is an open source message broker written in Java. It's fully compliant with JMS 1.1 standards. It is developed and maintained by Apache Software Foundation and is licensed under Apache license. It provides high availability, scalability, reliability, performance and security for enterprise level messaging applications.
JMS is a specification that allows development of message based system. ActiveMQ acts as a broker of messages which sits in between applications and allows them to communicate in asynchronous and reliable way.
Types of Messaging
There are two types of messaging options explained below for better understanding.
Point to Point
In this type of communication, the broker sends messages to only one consumer, while the other consumers will wait till they get the messages from the broker. No consumer will get the same message.
If there are no consumers, the Broker will hold the messages till it gets a consumer. This type of communication is also called as Queue based communication where the Producer sends messages to a queue and only one consumer gets one message from the queue. If there is more than one consumer, they may get the next message but they wont get the same message as the other consumer.
Publish/Subscribe
In this type of communication, the Broker sends same copy of messages to all the active consumers. This type of communication is also known as Topic based communication where broker sends same message to all active consumer who has subscribed for particular Topic. This model supports one-way communication where no verification of transmitted messages is expected.
Apache ActiveMQ - Environment Setup
This chapter will guide you on how to prepare a development environment to start your work with ActiveMQ. It will also teach you how to set up JDK, Maven and Eclipse on your machine before you set up ActiveMQ −
Setup Java Development Kit (JDK)
You can download the latest version of SDK from Oracle's Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
If you are running Windows and have installed the JDK in C:\jdk-19, you would have to put the following line in your C:\autoexec.bat file.
set PATH=C:\jdk-19;%PATH% set JAVA_HOME=C:\jdk-19
Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button.
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-19 and you use the C shell, you will have to put the following into your .cshrc file.
setenv PATH /usr/local/jdk-19/bin:$PATH setenv JAVA_HOME /usr/local/jdk-19
Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.
Setup Eclipse IDE
All the examples in this tutorial have been written using Eclipse IDE. So we would suggest you should have the latest version of Eclipse installed on your machine.
To install Eclipse IDE, download the latest Eclipse binaries from www.eclipse.org/downloads/. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:\eclipse on Windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately.
Eclipse can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe
%C:\eclipse\eclipse.exe
Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine −
$/usr/local/eclipse/eclipse
After a successful startup, if everything is fine then it should display the following result −
Set Maven
In this tutorial, we are using maven to run and build the spring based examples to run ActiveMQ based applications. Follow the Maven - Environment Setup to install maven.
Download ActiveMQ
You can download the latest stable version of ActiveMQ from its official page. Follow the Download ActivMQ to download ActiveMQ. We've used the 6.1.7 released on Jun 22nd, 2025. Extract the content of archive in the folder of your choice. We've extracted to D:/ &arr; apache-activemq-6.1.7.
Apache ActiveMQ - Features
ActiveMQ is designed to provide high availability, scalability, reliability, performance and security for enterprise level messaging applications. Following are some of the salient features of ActiveMQ.
JMS Compliant − ActiveMQ is fully compliant with JMS 1.1 standards. JMS spec provides a standard mechanism for synchronous or asynchronous message delivery, once-and-only-once message delivery, message durability for subscribers, etc.
Connectivity Options − ActiveMQ supports HTTP/S, multicast, SSL, Stomp, TCP, UDP, XMPP, thus providing a wide range of options for connectivity and allows various systems to communicate using their choice of protocols.
Pluggable Architecture − ActiveMQ allows to choose a persistence mechanism and also provides options to customize security for authentication and authorization as per the application needs.
Multi-Platform − ActiveMQ provides client APIs for many popular languages like Java, C, C++, .NET, Perl, PHP, Python, Ruby etc. ActiveMQ Broker will run in JVM but clients can be written using any of the supported languages.
Broker Cluster − ActiveMQ allows to prepare a network of brokers for scalability and can support different types of topologies.
Features Rich − ActiveMQ provides many advanced features for both broker and clients and support Apache Camel.
Simple Administration Interface − ActiveMQ administration console is easy to use but still provides many powerful administration features.
Apache ActiveMQ - Running Broker Server
We've downloaded the latest version of ActiveMQ as mentioned in ActiveMQ - Environment Setup. Now go to the folder D:/apache-activemq-6.1.7/bin and type the following command.
Example
D:\apache-activemq-6.1.7\bin>activemq start
Output
You'll see the similar output and ActiveMQ will start running.
Java Runtime: Oracle Corporation 19.0.2 C:\Program Files\Java\jdk-19
Heap sizes: current=1048576k free=1039360k max=1048576k
JVM args: -Dcom.sun.management.jmxremote -Xms1G -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=D:\apache-activemq-6.1.7\bin\..\conf\login.config -Dactivemq.classpath=D:\apache-activemq-6.1.7\bin\..\conf;D:\apache-activemq-6.1.7\bin\../conf;D:\apache-activemq-6.1.7\bin\../conf; -Dactivemq.home=D:\apache-activemq-6.1.7\bin\.. -Dactivemq.base=D:\apache-activemq-6.1.7\bin\.. -Dactivemq.conf=D:\apache-activemq-6.1.7\bin\..\conf -Dactivemq.data=D:\apache-activemq-6.1.7\bin\..\data -Djolokia.conf=file:D:\\apache-activemq-6.1.7\\bin\\..\\conf\\jolokia-access.xml -Djava.io.tmpdir=D:\apache-activemq-6.1.7\bin\..\data\tmp
Extensions classpath:
[D:\apache-activemq-6.1.7\bin\..\lib,D:\apache-activemq-6.1.7\bin\..\lib\camel,D:\apache-activemq-6.1.7\bin\..\lib\optional,D:\apache-activemq-6.1.7\bin\..\lib\web,D:\apache-activemq-6.1.7\bin\..\lib\extra]
ACTIVEMQ_HOME: D:\apache-activemq-6.1.7\bin\..
ACTIVEMQ_BASE: D:\apache-activemq-6.1.7\bin\..
ACTIVEMQ_CONF: D:\apache-activemq-6.1.7\bin\..\conf
ACTIVEMQ_DATA: D:\apache-activemq-6.1.7\bin\..\data
Loading message broker from: xbean:activemq.xml
INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:\apache-activemq-6.1.7\bin\..\data\kahadb]
INFO | Starting Persistence Adapter: KahaDBPersistenceAdapter[D:\apache-activemq-6.1.7\bin\..\data\kahadb]
INFO | Starting KahaDBStore
INFO | Opening MessageDatabase
INFO | Page File: D:\apache-activemq-6.1.7\bin\..\data\kahadb\db.data. Recovering pageFile free list due to prior unclean shutdown..
INFO | KahaDB is version 7
INFO | Page File: D:\apache-activemq-6.1.7\bin\..\data\kahadb\db.data. Recovered pageFile free list of size: 0
INFO | Starting Temp Data Store
INFO | PListStore:[D:\apache-activemq-6.1.7\bin\..\data\localhost\tmp_storage] started
INFO | Starting Job Scheduler Store
INFO | Persistence Adapter successfully started
INFO | Apache ActiveMQ 6.1.7 (localhost, ID:Home-55627-1754044777586-0:1) is starting
INFO | Listening for connections at: tcp://Home:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector openwire started
INFO | Listening for connections at: amqp://Home:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector amqp started
INFO | Listening for connections at: stomp://Home:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector stomp started
INFO | Listening for connections at: mqtt://Home:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector mqtt started
INFO | Starting Jetty server
INFO | Creating Jetty connector
WARN | ServletContext@o.e.j.s.ServletContextHandler@1a1f5f71{/,null,STARTING} has uncovered HTTP methods for the following paths: [/]
INFO | Listening for connections at ws://Home:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector ws started
INFO | Apache ActiveMQ 6.1.7 (localhost, ID:Home-55627-1754044777586-0:1) started
INFO | For help or more information please see: http://activemq.apache.org
INFO | ActiveMQ WebConsole available at http://127.0.0.1:8161/
INFO | ActiveMQ Jolokia REST API available at http://127.0.0.1:8161/api/jolokia/
Verification
Now open http://127.0.0.1:8161/admin/ in your browser. It will ask for credentials. Use admin/admin as username/password and it will load the ActiveMQ admin console where you can check Queues, Topics, Connections etc.
Apache ActiveMQ - Running Broker Server
We've downloaded the latest version of ActiveMQ as mentioned in ActiveMQ - Environment Setup. Now go to the folder D:/apache-activemq-6.1.7/bin and type the following command.
Example
D:\apache-activemq-6.1.7\bin>activemq start
Output
You'll see the similar output and ActiveMQ will start running.
Java Runtime: Oracle Corporation 19.0.2 C:\Program Files\Java\jdk-19
Heap sizes: current=1048576k free=1039360k max=1048576k
JVM args: -Dcom.sun.management.jmxremote -Xms1G -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=D:\apache-activemq-6.1.7\bin\..\conf\login.config -Dactivemq.classpath=D:\apache-activemq-6.1.7\bin\..\conf;D:\apache-activemq-6.1.7\bin\../conf;D:\apache-activemq-6.1.7\bin\../conf; -Dactivemq.home=D:\apache-activemq-6.1.7\bin\.. -Dactivemq.base=D:\apache-activemq-6.1.7\bin\.. -Dactivemq.conf=D:\apache-activemq-6.1.7\bin\..\conf -Dactivemq.data=D:\apache-activemq-6.1.7\bin\..\data -Djolokia.conf=file:D:\\apache-activemq-6.1.7\\bin\\..\\conf\\jolokia-access.xml -Djava.io.tmpdir=D:\apache-activemq-6.1.7\bin\..\data\tmp
Extensions classpath:
[D:\apache-activemq-6.1.7\bin\..\lib,D:\apache-activemq-6.1.7\bin\..\lib\camel,D:\apache-activemq-6.1.7\bin\..\lib\optional,D:\apache-activemq-6.1.7\bin\..\lib\web,D:\apache-activemq-6.1.7\bin\..\lib\extra]
ACTIVEMQ_HOME: D:\apache-activemq-6.1.7\bin\..
ACTIVEMQ_BASE: D:\apache-activemq-6.1.7\bin\..
ACTIVEMQ_CONF: D:\apache-activemq-6.1.7\bin\..\conf
ACTIVEMQ_DATA: D:\apache-activemq-6.1.7\bin\..\data
Loading message broker from: xbean:activemq.xml
INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:\apache-activemq-6.1.7\bin\..\data\kahadb]
INFO | Starting Persistence Adapter: KahaDBPersistenceAdapter[D:\apache-activemq-6.1.7\bin\..\data\kahadb]
INFO | Starting KahaDBStore
INFO | Opening MessageDatabase
INFO | Page File: D:\apache-activemq-6.1.7\bin\..\data\kahadb\db.data. Recovering pageFile free list due to prior unclean shutdown..
INFO | KahaDB is version 7
INFO | Page File: D:\apache-activemq-6.1.7\bin\..\data\kahadb\db.data. Recovered pageFile free list of size: 0
INFO | Starting Temp Data Store
INFO | PListStore:[D:\apache-activemq-6.1.7\bin\..\data\localhost\tmp_storage] started
INFO | Starting Job Scheduler Store
INFO | Persistence Adapter successfully started
INFO | Apache ActiveMQ 6.1.7 (localhost, ID:Home-55627-1754044777586-0:1) is starting
INFO | Listening for connections at: tcp://Home:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector openwire started
INFO | Listening for connections at: amqp://Home:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector amqp started
INFO | Listening for connections at: stomp://Home:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector stomp started
INFO | Listening for connections at: mqtt://Home:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector mqtt started
INFO | Starting Jetty server
INFO | Creating Jetty connector
WARN | ServletContext@o.e.j.s.ServletContextHandler@1a1f5f71{/,null,STARTING} has uncovered HTTP methods for the following paths: [/]
INFO | Listening for connections at ws://Home:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600
INFO | Connector ws started
INFO | Apache ActiveMQ 6.1.7 (localhost, ID:Home-55627-1754044777586-0:1) started
INFO | For help or more information please see: http://activemq.apache.org
INFO | ActiveMQ WebConsole available at http://127.0.0.1:8161/
INFO | ActiveMQ Jolokia REST API available at http://127.0.0.1:8161/api/jolokia/
Verification
Now open http://127.0.0.1:8161/admin/ in your browser. It will ask for credentials. Use admin/admin as username/password and it will load the ActiveMQ admin console where you can check Queues, Topics, Connections etc.
Apache ActiveMQ - Admin Console
Once ActiveMQ server is up and running. You can use the admin console to manage queues, topics, subscribers, connections, network etc.
Open http://127.0.0.1:8161/admin/ in your browser. It will ask for credentials. Use admin/admin as username/password and it will load the ActiveMQ admin console where you can check Queues, Topics, Connections etc.
Queue
Click on Queue tab, enter a queue name as testQueue and click on create button. Now you can see the queue in the list.
Topic
Similarly, you can create Topic and check topics in Topics tab.
Others
In the same way, you can explore subscribers, connections, network bridges, schedulers details.
Send
Send tab allows to send a JMS Message to a particular queue or topic by specifying the destination and other details.
Apache ActiveMQ - Producer Application
Now let's create a producer application which will send message to the ActiveMQ Queue.
Create Project
Using eclipse, select File &arr; New → Maven Project. Tick the Create a simple project(skip archetype selection) and click Next.
Enter the details, as shown below −
groupId − com.tutorialspoint
artifactId − producer
version − 0.0.1-SNAPSHOT
name − ActiveMQ Producer
Click on Finish button and a new project will be created.
pom.xml
Now update the content of pom.xml to include dependencies for ActiveMQ.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint.activemq</groupId>
<artifactId>producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ActiveMQ Producer</name>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>0.40.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>19</source>
<target>19</target>
</configuration>
</plugin>
<plugin>
<groupId>org.fusesource.mvnplugins</groupId>
<artifactId>maven-uberize-plugin</artifactId>
<version>1.14</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>uberize</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now create a Producer class which will send message to the ActiveMQ Queue.
package com.tutorialspoint.activemq;
import java.io.Console;
import java.util.Scanner;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.qpid.jms.JmsConnectionFactory;
public class Producer {
public static void main(String[] args) throws Exception {
// Create a connection to ActiveMQ JMS broker using AMQP protocol
JmsConnectionFactory factory = new JmsConnectionFactory("amqp://localhost:5672");
Connection connection = factory.createConnection("admin", "password");
connection.start();
// Create a session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a queue
Destination destination = session.createQueue("MyFirstQueue");
// Create a producer specific to queue
MessageProducer producer = session.createProducer(destination);
Scanner input = new Scanner(System.in);
String response;
do {
System.out.println("Enter message: ");
response = input.nextLine();
// Create a message object
TextMessage msg = session.createTextMessage(response);
// Send the message to the queue
producer.send(msg);
} while (!response.equalsIgnoreCase("Quit"));
input.close();
// Close the connection
connection.close();
}
}
Producer class creates a connection, starts the session, creates a producer and then asks user to enter message. If user enters "Quit" then application terminates else it will send the message to the queue.
We'll run this application in ActiveMQ - Test Application chapter.
Apache ActiveMQ - Consumer Application
Now let's create a consumer application which will receive message from the ActiveMQ Queue.
Create Project
Using eclipse, select File &arr; New → Maven Project. Tick the Create a simple project(skip archetype selection) and click Next.
Enter the details, as shown below −
groupId − com.tutorialspoint
artifactId − consumer
version − 0.0.1-SNAPSHOT
name − ActiveMQ Consumer
Click on Finish button and a new project will be created.
pom.xml
Now update the content of pom.xml to include dependencies for ActiveMQ.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint.activemq</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ActiveMQ Consumer</name>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>0.40.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>19</source>
<target>19</target>
</configuration>
</plugin>
<plugin>
<groupId>org.fusesource.mvnplugins</groupId>
<artifactId>maven-uberize-plugin</artifactId>
<version>1.14</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>uberize</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now create a Consumer class which will receive message from the ActiveMQ Queue.
package com.tutorialspoint.activemq;
import java.io.Console;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.qpid.jms.JmsConnectionFactory;
public class Consumer {
public static void main(String[] args) throws Exception {
// Create a connection to ActiveMQ JMS broker using AMQP protocol
JmsConnectionFactory factory = new JmsConnectionFactory("amqp://localhost:5672");
Connection connection = factory.createConnection("admin", "password");
connection.start();
// Create a session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a queue
Destination destination = session.createQueue("MyFirstQueue");
// Create a consumer specific to queue
MessageConsumer consumer = session.createConsumer(destination);
Console c = System.console();
String response;
do {
// Receive the message
Message msg = consumer.receive();
response = ((TextMessage) msg).getText();
System.out.println("Received = "+response);
} while (!response.equalsIgnoreCase("Quit"));
// Close the connection
connection.close();
}
}
Consumer class creates a connection, starts the session, creates a consumer and then receives message from queue if there is any. If queue contains quit as message then application terminates else it will keep polling queue for messages.
We'll run this application in ActiveMQ - Test Application chapter.
Apache ActiveMQ - Test Application
Start ActiveMQ Server
Now let's start the ActiveMQ Server. Go to the folder D:\apache-activemq-6.1.7\bin and type the following command.
Example
D:\apache-activemq-6.1.7\bin>activemq start
Output
You'll see the similar output and ActiveMQ will start running.
... INFO | Apache ActiveMQ 6.1.7 (localhost, ID:Home-55627-1754044777586-0:1) started INFO | For help or more information please see: http://activemq.apache.org INFO | ActiveMQ WebConsole available at http://127.0.0.1:8161/ INFO | ActiveMQ Jolokia REST API available at http://127.0.0.1:8161/api/jolokia/
Start the Producer Application
In eclipse, right click on the Producer.java source, and select Run As > Java Application. Producer application will start running and you'll see the output as follows −
Enter message:
Start the Consumer Application
In eclipse, right click on the Consumer.java source, and select Run As > Java Application. Consumer application will start running and you'll see the output as follows −
Send Message
In Producer console window, type Hi and press enter button to send the message.
Enter message: Hi
Receive Message
Verify in Consumer console window, the message is received.
Received = Hi
Send Quit as message to terminate both producer and consumer console window sessions.
Verification
Now open http://127.0.0.1:8161/admin/ in your browser. It will ask for credentials. Use admin/admin as username/password and it will load the ActiveMQ admin console where you can check Queues to check the status. It will show 2 messages enqueued and delivered.
Apache ActiveMQ - Publisher Application
Now let's create a publisher application which will send message to the ActiveMQ Queue.
Create Project
Using eclipse, select File &arr; New → Maven Project. Tick the Create a simple project(skip archetype selection) and click Next.
Enter the details, as shown below −
groupId − com.tutorialspoint
artifactId − publisher
version − 0.0.1-SNAPSHOT
name − ActiveMQ Publisher
Click on Finish button and a new project will be created.
pom.xml
Now update the content of pom.xml to include dependencies for ActiveMQ.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint.activemq</groupId>
<artifactId>publisher</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ActiveMQ Publisher</name>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>0.40.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>19</source>
<target>19</target>
</configuration>
</plugin>
<plugin>
<groupId>org.fusesource.mvnplugins</groupId>
<artifactId>maven-uberize-plugin</artifactId>
<version>1.14</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>uberize</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now create a Publisher class which will send message to the ActiveMQ topic to broadcast it to all the subscribers.
package com.tutorialspoint.activemq;
import java.util.Scanner;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.qpid.jms.JmsConnectionFactory;
public class Publisher {
public static void main(String[] args) throws Exception {
// Create a connection to ActiveMQ JMS broker using AMQP protocol
JmsConnectionFactory factory = new JmsConnectionFactory("amqp://localhost:5672");
Connection connection = factory.createConnection("admin", "password");
connection.start();
// Create a session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a topic
Destination destination = session.createTopic("MyFirstTopic");
// Create a publisher specific to topic
MessageProducer publisher = session.createProducer(destination);
Scanner input = new Scanner(System.in);
String response;
do {
System.out.println("Enter message: ");
response = input.nextLine();
// Create a message object
TextMessage msg = session.createTextMessage(response);
// Send the message to the topic
publisher.send(msg);
} while (!response.equalsIgnoreCase("Quit"));
input.close();
// Close the connection
connection.close();
}
}
Producer class creates a connection, starts the session, creates a producer and then asks user to enter message. If user enters "Quit" then application terminates else it will send the message to the topic.
We'll run this application in ActiveMQ - Test Application chapter.
Apache ActiveMQ - Subscriber Application
Now let's create a subscriber application which will receive message from the ActiveMQ Topic.
Create Project
Using eclipse, select File &arr; New → Maven Project. Tick the Create a simple project(skip archetype selection) and click Next.
Enter the details, as shown below −
groupId − com.tutorialspoint
artifactId − subscriber
version − 0.0.1-SNAPSHOT
name − ActiveMQ Subscriber
Click on Finish button and a new project will be created.
pom.xml
Now update the content of pom.xml to include dependencies for ActiveMQ.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint.activemq</groupId>
<artifactId>subscriber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ActiveMQ Subscriber</name>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>0.40.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>19</source>
<target>19</target>
</configuration>
</plugin>
<plugin>
<groupId>org.fusesource.mvnplugins</groupId>
<artifactId>maven-uberize-plugin</artifactId>
<version>1.14</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>uberize</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now create a Subscriber class which will receive message from the ActiveMQ Topic.
package com.tutorialspoint.activemq;
import java.io.Console;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.qpid.jms.JmsConnectionFactory;
public class Subscriber {
public static void main(String[] args) throws Exception {
// Create a connection to ActiveMQ JMS broker using AMQP protocol
JmsConnectionFactory factory = new JmsConnectionFactory("amqp://localhost:5672");
Connection connection = factory.createConnection("admin", "password");
connection.start();
// Create a session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a topic
Destination destination = session.createTopic("MyFirstTopic");
// Create a subscriber specific to topic
MessageConsumer subscriber = session.createConsumer(destination);
Console c = System.console();
String response;
do {
// Receive the message
Message msg = subscriber.receive();
response = ((TextMessage) msg).getText();
System.out.println("Received = "+response);
} while (!response.equalsIgnoreCase("Quit"));
// Close the connection
connection.close();
}
}
Subscriber class creates a connection, starts the session, creates a consumer and then receives message from topic if there is any. If topic contains "Quit" as message then application terminates else it will keep polling queue for messages.
We'll run this application multiple times to create multiple subscribers in ActiveMQ - Test Application chapter.
Apache ActiveMQ - Test Topic Based Application
Start ActiveMQ Server
Now let's start the ActiveMQ Server. Go to the folder D:\apache-activemq-6.1.7\bin and type the following command.
Example
D:\apache-activemq-6.1.7\bin>activemq start
Output
You'll see the similar output and ActiveMQ will start running.
... INFO | Apache ActiveMQ 6.1.7 (localhost, ID:Home-55627-1754044777586-0:1) started INFO | For help or more information please see: http://activemq.apache.org INFO | ActiveMQ WebConsole available at http://127.0.0.1:8161/ INFO | ActiveMQ Jolokia REST API available at http://127.0.0.1:8161/api/jolokia/
Start the Publisher Application
In eclipse, right click on the Publisher.java source, and select Run As > Java Application. Publisher application will start running and you'll see the output as follows −
Enter message:
Start the Subscriber Application
In eclipse, right click on the Consumer.java source, and select Run As > Java Application. Subscriber application will start running and you'll see the output as follows −
Start another Subscriber Application
In eclipse, again right click on the Subscriber.java source, and select Run As > Java Application. Another Subscriber application will start running and you'll see the output as follows −
Send Message
In Publisher console window, type Hi and press enter button to send the message.
Enter message: Hi
Receive Message
Verify in Subscriber console windows, the message is received in each window.
Received = Hi
Send "Quit" as message to terminate both publisher and subscriber console window sessions.
Verification
Now open http://127.0.0.1:8161/admin/ in your browser. It will ask for credentials. Use admin/admin as username/password and it will load the ActiveMQ admin console where you can check Topics to check the status. It will show multiple messages enqueued and delivered.