JMS (Java Message Service), also known as a messaging service, is an API that provides the facility to create, send, and read messages. It gives loosely coupled, reliable and asynchronous communication between two or more systems. If you want to send a message from one application to another, you need to use JMS API.
For example, one application A is running in INDIA, and another application B is running in the USA. To transfer a message from application A to application B, we require Java Message Service.
Advantages of JMS Architecture:
1. Asynchronous: For receiving the message, the client is not required to send any request. The message will automatically arrive at the client.
2. Reliable: It gives assurance that message is delivered.
There are two types of models in JMS Architecture:
- Point-to-Point Messaging Domain (PTP)
- Publisher/Subscriber Messaging Domain (Pub/Sub)
Point-to-Point Messaging Domain:
In the PTP model, one message is delivered to only one receiver. Here, the Queue acts as message-oriented middleware (MOM) and is responsible for holding the message until the receiver is ready.
In the PTP model, there is no timing dependency available between the sender and receiver. A sender (producer) sends a message addressing a specific queue. A receiver (consumer) receives the message from the Queue established to hold its messages. Queues stores all messages sent to them until the messages are received or until the messages expire. The receiver can get the message whether or not it was running when the client sent the message. The receiver also acknowledges the successful processing of a message.
Publisher/Subscriber Messaging Domain:
In the Pub/Sub model, one message is delivered to all the subscribers. It is similar to broadcasting. Here, Topic acts as a message-oriented middleware that is responsible for holding and delivering messages.
In the Pub/Sub model, there is timing dependency present between publisher and subscriber. Publisher clients publish the messages to one or more topics. Subscriber clients subscribe to one or more topics and receive the messages when they are sent to them. The topics persist the messages as long as it takes to deliver them to all currently subscribed clients. Each message may have multiple receivers.
A client who has subscribed to a topic can consume published messages only after the client has created a subscription, and the subscriber must continue to be active for it to consume messages.
Message Sender:
Message Sender is used for sending messages to a destination queue, and its object is created by a session. It implements the interface MessageProduce.
First, we need to configure a connection object using the ActiveMQConnectionFactory factory object, and then we create a session object. With the session object’s help, we set the message broker, i.e., Queue, and configure the message sender object.
public class MessageSender { public static void main(String[] args) { Connection connection = null; try { Context ctx = new InitialContext(); ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); connection = cf.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("test.message.queue"); MessageProducer messageProducer = session.createProducer(destination); MapMessage message = session.createMapMessage(); message.setString("Name", "Tim"); message.setString("Role", "Developer"); message.setDouble("Salary", 850000); messageProducer.send(message); } catch (Exception e) { System.out.println(e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { System.out.println(e); } } System.exit(0); } } }
Message Receiver:
Message Receiver is used for receiving messages from a queue, and its object is created by a session. It implements the interface MessageProducer. Please see the below code for the message receiver. The process is the same in message sender and receiver. In the case of the receiver, a Message Listener is used. Listener remains active and is called only when the receiver consumes any message from the broker.
public class MessageReceiver { public static void main(String[] args) { try { InitialContext ctx = new InitialContext(); ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = cf.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("test.prog.queue"); MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new MapMessageListener()); connection.start(); } catch (Exception e) { System.out.println(e); } } } Please see the below code for a message listener receiving map message object. public class MapMessageListener implements MessageListener { public void onMessage(Message message) { if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage)message; try { String name = mapMessage.getString("Name"); System.out.println("Name : " + name); } catch (JMSException e) { throw new RuntimeException(e); } } else { System.out.println("Invalid Message Received"); } } }
2 Responses