Using Freshworks apps? Check out what we can do for you! Learn More

Back

Java Message Service (JMS) using ActiveMQ

What is Java Message Service (JMS)?
Java Message Service is the technique used by applications to communicate without being tightly coupled to one another. It provides a platform for applications to send and receive messages to specific or multiple destinations. This technique can be integrated with existing applications to communicate with each other.

Java Message Service (JMS) using ActiveMQ


What is ActiveMQ?
Apache ActiveMQ is a message broker which fully implements the Java Messaging Service API 1.1 and Supports a variety of Cross Language Clients and Protocols such as Java, C, C++, C#, Ruby, Perl, Python, PHP. This makes messages to share a common format and semantic, which makes integration between different applications easier.
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 same queue destination. The concept is shown below as a schematic.

Installing ActiveMQ

  1. Download Apache ActiveMQ from ActiveMQ download page.
  2. Extract the downloaded archive to a suitable location.
  3. Open a command prompt
  4. Go to the bin directory of extracted Apache MQ from command prompt.
  5. Run the ActiveMQ command.
  6. Once the Apache MQ 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 username 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.
    Java Message Service (JMS) | Techaffinity
  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)

Producer.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 Producer {
private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageProducer producer = null;
public Producer() {}
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 from client”);
producer.send(message);
System.out.println(“Sent message: ” + message.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Sender sender = new Sender();
sender.sendMessage();
}
}

Consumer.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 Consumer {
private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageConsumer consumer = null;
public Consumer() {}
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(“Received message is : ” + text.getText());
}
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Receiver receiver = new Receiver();
receiver.receiveMessage();
}
}

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

Output
Compile and run the Producer.java . The output will be displayed in the console.
Sent message: Hello … This is a sample message from client.

Then compile and run the Consumer.java.
Received Message is : Hello …This is a sample message from client

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

Use cases
There are many different use cases, requirements and deployment options. Some of the main use cases are
1. Transactional messaging
2. High performance market data distribution
3. Clustering and general purpose async messaging model
4. Web Streaming of data
5. RESTful API to messaging using HTTP

Subscribe to Our Blog

Stay updated with latest news, updates from us