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