JMS Example using Apache ActiveMQ

We already discussed the fundamentals of JMS API.We discussed almost all concepts in JMS by using OpenJMS as JMS provider in earlier chapters. In this chapter we are discussing a JMS example using Apache ActiveMQ.

Overview to Apache ActiveMQ

Apache ActiveMQ is a message broker which fully implements the Java Messaging Service API 1.1.It can be used by programs written Java,C/C++,.NET,PHP etc.The list of supporting platforms and languages can be read from the ActiveMQ  page.

JMS Example using Apache ActiveMQ

In this section we are looking into a JMS example,which involves two JMS clients.First client sends a message to a queue destination. The second client retrieves the message from the queue destination. In a previous chapter we discussed a  similar example.There we used OpenJMS as JMS provider.In this example , we are using ActiveMQ as JMS provider.

Steps Involved

1)Download Apache ActiveMQ from ActiveMQ download page.

2)Extract the downloaded archive to suitable location.

3)Open a command prompt

4)Go to the bin directory of extracted ActiveMQ from command prompt.

5)Run the activemq command.

6)Once the ActiveMQ  completes the startup , the appropriate messages will be displayed in the console.

7)Now open the browser and type the url:http://localhost:8161/admin/

If it is asking for user name and password (recent  versions are secured with password), provide ‘admin’ as username and password.

8)A page looks like the one shown below will be opened.

9)It is possible to view administered objects like queue,topic etc from the page.

10)Now create a dynamic web project and put the following classes into it.The jar files in the lib package of extracted ActiveMQ should be added as dependent library of the created project.(Simply put those jars in the WebContent\WEB-INF\lib if we use eclipse as IDE)

Sender.java

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {

private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageProducer producer = null;

public Sender() {

}

public void sendMessage() {

try {
factory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_BROKER_URL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("SAMPLEQUEUE");
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
message.setText("Hello ...This is a sample message..sending from FirstClient");
producer.send(message);
System.out.println("Sent: " + message.getText());

} catch (JMSException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Sender sender = new Sender();
sender.sendMessage();
}

}

Receiver.java

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Receiver {
private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageConsumer consumer = null;

public Receiver() {

}

public void receiveMessage() {
try {
factory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_BROKER_URL);
connection = factory.createConnection();
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("SAMPLEQUEUE");
consumer = session.createConsumer(destination);
Message message = consumer.receive();

if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;
System.out.println("Message is : " + text.getText());
}
} catch (JMSException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Receiver receiver = new Receiver();
receiver.receiveMessage();
}
}

The Sender.java sends a simple text message to the queue ‘SAMPLEQUEUE’. The Receiver.java receives the message from the same queue.

Output

Compile and run the Sender.java . The output will be displayed in the console.

Sent: Hello …This is a sample message..sending from FirstClient.

Then compile and run the Receiver.java.

Message is : Hello …This is a sample message..sending from FirstClient

The status related with each queue and topic can be found from the web console.

See related Topics:

JMS Overview

Configuring OpenJMS

JMS example

Sending and receiving ObjectMessages in JMS

JMS Domains:

Point to Point Messaging domain in JMS

Publish/Subscribe Messaging domain JMS

Message consumption

Synchronous message consumption in JMS

Asynchronous message consumption in JMS

8 thoughts on “JMS Example using Apache ActiveMQ

  1. S Sahu Reply

    I love your Tutorials. Just a small suggestion from my end, please include atleast an example where values for context and other like queue name,etc are not getting hardcoded.

  2. Nava Reply

    Thanks to your good tutorial for this concept! for step #10, I use just activemq-all-5.7.0.jar that can be found in root folder.

  3. Praveen John Kumar Reply

    In the Receiver.java , why do we have 2 createConnection ?

    connection = factory.createConnection();
    connection = factory.createConnection();

  4. Adarsh Reply

    When I am running receiver.java I’m only getting successfully connected to localhost. But the received message is not getting print,

  5. pratham Reply

    how to coonect to amqp instead of openwire in activemq above code

  6. Sahan Sandaruwan Reply

    You need to start the activemq with command ‘activemq start’ instead of ‘activemq’

Leave a Reply

Your email address will not be published. Required fields are marked *