Components required for JMS

by Vibrant Publishers
JMS refers to Java Messaging Service which allows the software developers to loosely couple applications. It has an API (Application Programming Interface) through which we can receive, send, read, and create enterprise system messages.
 
JMS is a message oriented product which is quite expensive and complex. It provides an interface (Java) to write infrastructure code and allow solutions to be built easily and quickly.

 

Following are the main components of JMS:

  • Administered: ObjectsDestinationConnectionFactory
  • Connection
  • Session
  • Objects: MessageProducer, MessageConsumer, MessageListener, MessageSelector

 

The component relationship could be explained with the below diagram.

 

1) Administered Objects:

 

Following are called as the administered objects as they are created by the JMS provider administrator:
  • Destination
  • ConnectionFactory

 

 

Destination:

 

This object has the configuration information provided by JMS provider. This object is used by the Client to specify the destination for messages to be send and location to receive the messages.
 
There are 2 types of Interfaces Destination uses. They are:
  1. Queue – It belongs to PTP (Point To Point) model
  2. Topic – It belongs to pub/sub (Publish / Subscribe) model

 

 

Connection Factory:

 

This object has the connection configuration information (IP address) provided by JMS provider. The connection is obtained through the Java Naming Directory Interface (JNDI) which enables the client to create a connection and get connected to the JMS server.

 

 

2) Connection:

This component provides the connection (physical) to the JMS server. The physical connection is obtained with the help of ConnectionFactory object which provides the instances of configuration details to connect to the JMS server.

 

 

3) Session:

This component is responsible to receiving and sending messages, and for handling acknowledgements and managing transactions with the help of JMS objects.

 

4) Objects:

 

It refers to the JMS object which are maintained and created by the admin that are used by the JMS clients. The following objects are used to receive and create messages in JMS:
  • MessageConsumer
  • MessageProducer
  • MessageSelector:  This is used to filter the message that does not meet the specified criteria.
  • MessageListener:  This is used to process and receive messages asynchronously

 

 

MessageConsumer:

 

MessageConsumer is for receiving messages synchronously or asynchronously to a particular destination which is created by the session object.
 
In case of synchronous communication, the consumer calls the any one of the methods receive or receiveNoWait and in case of asynchronous communication, the client registers MessageListener and starts consumer.
 
Almost all the messages in Java Messaging Service are exchanged asynchronously between clients where the producer never receive acknowledgement from consumer.

 

 

MessageProducer:

MessageProducer is for sending messages either via PTP or through pub/sub. In PTP (Point-To-Point) model, the destination is known as queue and in pub/sub model, the destination is known as topic.
 
In producer, we can set the delivery mode either as NON-PERSISTENT or PERSISTENT using the setDeliveryMode method. Non-Persistent has less overhead as the messages are not logged in the server or database.
 
We can also use the setPriority method to set the message priority. 0 indicates low priority and 9 indicates high priority. We can also use setTimeToLive method to mention the life time of the message in milliseconds.

 

 

MessageSelector:

 

MessageSelector belongs to String object which is used to filter the message that does not fit into the specified criteria.
 
The message selector can examine the header and compares an expression which is present in the String. The example for expression comprises of arithmetic operators, relational operators, string literals, and logical operators.

 

 

MessageListener:

 

To process messages asynchronously, we have to use instantiate the MessageListener interface. We have to perform the following steps to process and receive messages asynchronously:
  • Create object which implements MessageListener interface and invoke the onMessage method
  • Use setMessageListener method to register with the session object
  • Use Connection object’s start method to start receiving messages.