Thursday, July 28, 2016

JMS Example using Apache ActiveMQ

Apache ActiveMQ is the most popular and powerful open source messaging server. ActiveMQ apart from being powerful and open source, it is the light weight messaging server compared to other messaging servers like JBossMQ and Tibco EMS.

Setting up ActiveMQ

  1.      Download the Apache ActiveMQ from the ActiveMQ download link.
  2.      Extract the downloaded archive to some location in your system.
  3.      In the extracted root directory, we see a sub-folder named ‘/bin‘, which contains a file named ‘activemq.bat‘.
  4.      Run activemq.bat file.
  5.      ActiveMQ messaging server has started up successfully. To verify it, go to the URL http://localhost:8161/.  If we see the ActiveMQ page, it’s works fine.
  6.      Now go to the URL http://localhost:8161/admin, which is the default admin console ActiveMQ provides us with, where we can monitor some important metrics on Consumers, Topics, Queues, Messages, etc.
  7.      The default setup also provides with a modular Hawtio console for better management. This doesn’t need any separate configuration and can be seen at http://localhost:8161/hawtio/. This is equivalent to the default admin console provided at the URL http://localhost:8161/admin.

What is JMS?
JMS (Java Message Service) is an API that provides the facility to create, send and read messages. It provides loosely coupled, reliable and asynchronous communication.
JMS is also known as a messaging service., It’s mainly used to send and receive message from one application to another.

Advantage of JMS
1) Asynchronous: To receive the message, client is not required to send request. Message will arrive automatically to the client.
2) Reliable: It provides assurance that message is delivered.

There are two types of messaging domains in JMS.

1.      Point-to-Point Messaging Domain
2.      Publisher/Subscriber Messaging Domain

1) Point-to-Point (PTP) Messaging Domain
In PTP model, one message is delivered to one receiver only. Here, Queue is used as a message oriented middleware (MOM).
The Queue is responsible to hold the message until receiver is ready.
In PTP model, there is no timing dependency between sender and receiver.



2) Publisher/Subscriber (Pub/Sub) Messaging Domain
In Pub/Sub model, one message is delivered to all the subscribers. It is like broadcasting. Here, Topic is used as a message oriented middleware that is responsible to hold and deliver messages.
In PTP model, there is timing dependency between publisher and subscriber.



JMS Example using Apache ActiveMQ
The Sender.java sends a simple text message to the queue ‘TEACHTOJAVAQUEUE”.
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("TEACHTOJAVAQUEUE");
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
message.setText("Hello ...This is a sample message..sending from TeachToJava");
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();
}
}

The Receiver.java receives the message from the same queue.
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.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("TEACHTOJAVAQUEUE ");
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();
}
}

Output
Compile and run the Sender.java . The output will be displayed in the console.
Sent: Hello …This is a sample message..sending from TeachToJava.
Then compile and run the Receiver.java.
Message is: Hello …This is a sample message..sending from TeachToJava
The status related with each queue and topic can be found from the web console.

No comments:

Post a Comment