A Brief Introduction to Java Messaging Service (JMS)

Hi all, today in my blog I am going to write about the Java Message Service (JMS) API. Hope you will find it useful.

Overview:

Java Messaging Service (JMS) allows applications to create, send, receive and read messages. These messages differ from the normal emails we send day to day. That is, these messages are communicated between software components or applications.

Ex: A stock management component sending a message to the stock re-ordering component indicating a re-order of an item is required.

JMS communication is loosely coupled unlike in RMI. Not only that, it is very reliable. Every message sent or received in JMS is acknowledged and hence it is guaranteed that the messages can never be lost. There are some terminologies used in JMS. They are as follows:

  • JMS Provider – Provides administrative and controlling services such as managing the queues.
  • Clients – Sending and Receiving clients who send and receive messages to/from the queues or topics.
  • Messages – Objects that are used as the mean of communication between the senders and the clients.
    • Message contains a header, body and properties.
    • There are basically 6 message types in JMS.
      • Text – for sending text content to clients.
      • Map – for pairs of values to be sent.
      • Object – for sending objects.
      • Byte – for sending a stream of bytes which are uninterpreted.
      • Stream – for sending a stream of primitive values in Java programming language.
      • Message – when you want to send a message without a body and just the header, you can use this type.
  • Administered Objects – Objects that are managed by an administrator, not by the program. For example the Destination and ConnectionFactory objects belong to this.
    • Destination is where the messages will be kept until the consumers consume them. There are two types of destinations; Queue and Topic.
      • Queue is often used in Point-to-Point Messaging Domain. It retains messages until the consumers receive them or until they expire.
      • Topic is often used in Publisher/Subscriber Messaging Domain. It retains messages until all the active subscribers get the messages from it.

       

    • ConnectionFactory is used to create connection objects. There are two types in this also; QueueConnectionFactory and TopicConnectionFactory. The former one is used to create QueueConnection objects whereas the latter one is used to create TopicConnection objects.

Programming Model of JMS:

ProgrammingModel

Figure 1 – Programming Model of JMS API

  • First we look up for a ConnectionFactory object in the administered objects store.

  • Then we create a connection object using the ConnectionFactory object we have. Connection can be either of two types; TopicConnection and QueueConnection.

  • That connection object is used to create session objects. There can be multiple sessions. Sessions also can be either of two types; TopicSession and QueueSession.

  • Messages, consumers and producers can be created using the session object.

  • MessageProducer can send the messages to the destination, while MessageConsumer can retrieve messages from the destination.

Note: For a very simple JMS example, visit my blog here.

Messaging Domain in JMS:

There are two types of messaging domains in JMS.

    1. Point-to-Point Messaging Domain

      PTP

      Figure 2 – Point-to-Point Messaging Domain

      In this domain, a message can have only one consumer. The terminologies used in this domain are Sender, Receiver, and Queue. That is, a sender sends a message to a queue and a receiver fetches that message from the queue.

      Also, in this domain there is no timing dependency. That means, a receiver needs not to be active to get a message at the time the sender sends the message to the queue.

       

    2. Publisher/Subscriber Messaging Domain

      PS

      Figure 3 – Publisher/Subscriber Messaging Domain

      In this domain, there can be multiple subscribers for a single message. The terminologies used in this are Publisher, Subscriber and Topic. Publishers publish messages to a Topic. Subscribers should subscribe to this topic in order to get the messages.

      Also, in this domain there is timing dependency. That means, a subscriber cannot retrieve messages that are published by a publisher to the topic before the subscription of the subscriber to the topic. Moreover, subscribers have to be active continuously in order to get the messages from the topic.

      Therefore, there is a kind of restriction in message consumption due to this timing dependency. To relax this to some extent, JMS provides DurableSubscriber concept. A durable subscriber does not need to be alive to get a message at the time the message is published by a publisher. However, it should always be subscribed to the topic to get the messages.

Message Consumption:

There are two ways in which a message can be consumed in JMS.

  1. Synchronously

A subscriber or receiver explicitly retrieves a message from the destination by calling its receive() method.

  1. Asynchronously

Whenever a message is received to the queue or topic from a publisher or a sender, the JMS provider delivers that message to the corresponding client. In doing so, we use a Message Listener. The client can have a message listener that will listen to the destination. Whenever a message is received at the destination, the onMessage method of the message listener is called to send that message to the receiver.

So far I discussed the basics of the Java Message Service (JMS). It is a very interesting area to learn. You can see a simple JMS example here in my blog.

 

Advertisements

One thought on “A Brief Introduction to Java Messaging Service (JMS)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s