JMS Tutorial

Java Message Service(JMS) API is an important API in J2EE . This API plays an important role in supporting other APIs in J2EE. The concept will become clear after discussing the concept in detail. In this chapter of JMS tutorial we will be discussing  the fundamental concepts of Java Messaging Service

Overview to Java Message Service

JMS is the technique used in J2EE technology  for an application to communicate with other applications.An application can communicate with any number of applications using JMS.This communication is loosely coupled.That means those  applications needed to communicate are not connecting each other directly . Instead , those applications are connecting to a common destination. An application can send messages to the destination and can take messages from the same destination.The concept is shown below as a schematic.This is  simple and it can be easily integrated with existing applications .

The API was developed based on JSR 914 specification.

Now let us discuss the basic architecture of JMS API.

Synchronous & Asynchronous consumption of messages

Usually there is no timing dependency between producer and consumer  in JMS . But we can make the message transfer in a synchronous way(With timing dependencies between clients ). We will see the differences between them with suitable examples in the coming chapters.

JMS API Architecture

The important components are listed here.

JMS Client :- The Java application which produces/consumes message is known as JMS Client.

Service Provider :- A  messaging  system which implements the JMS interfaces.There are numerous service providers are available today.Examples are open JMS , JBoss Messaging . In the coming sections we will be looking into JMS  examples . There ,we will be using OpenJMS as the service provider.

Messages :– Messages are objects using to communicate between clients.

Administered Objects : These are preconfigured objects. Two types of administered objects are destinations and connection factories.

1)Connection factory: It is an administered object with which a connection between JMS client and Service provider is establishing.

2)Destination : Destination is the object into which a JMS clients targets its messages. Also JMS clients receives messages from the destination. And hence the JMS system is loosely coupled .

Native Clients :- They uses Message Products native API instead of JMS API.

The following block diagram shows how the JMS API works.

An administered tool binds the administered objects – Connection factories and destinations ,to the  JNDI name space. A client  is looking for the administered objects in the JNDI namespace.And then establishes a logical connection with those objects using  service provider. So   the clients can establish connection with  destinations . Thus  clients can communicate with each other.

Messaging Domains

JMS API supports Point-to-Point and Publish-Subscribe approaches.

Point-to-Point Approach

The point to point approach consists of a sender , a receiver and a queue.Each message is addressed to a specific queue.Receiving clients can take  the messages  from the queue. We will be looking into a point to point approach in JMS  later .

Publish/Subscribe approach

In this approach there can be multiple publishers as well as multiple consumers. A publisher can have multiple consumers . The system is taking care to do the correct delivery.In this approach publishers and consumers have timing dependencies.Suppose a client is subscribed to a topic. It can only consume messages  published after the subscription has created. And the client must retain its active state to receive message .We  will be lookng into this approah in detail later .

The block diagram shown above gives the fundamental idea about publish/subscribe approach.

Building Blocks of a JMS application

Administered Objects: Destinations and connection factories are referred as administered objects.These are not usually maintaining by program . These objects will be bound to JNDI  name space . Clients can access these administered object using JNDI look up.

Connection :- It is a virtual connection with JMS provider.

Session :- It is a single threaded  context for producing and consuming messages.

Message Producer :– It is created from a session and is using to send messages to destinations.

Message Consumers :– It is also created from a session and is using to receive messages from destination.

Message Selector :- To filter the messages received.

Message :-Message objects are sending in JMS. A JMS message has 3 parts

a)header :- It contains a number of  predefined fields using for proper delivery and routing.

b)body :- As the name suggests it is the body of messages. JMS API allows five types of message bodies.

        1.TextMessage :- Body contains String data

        2.ByteMessage :- Body contains byte data

        3. MapMessage :- Body contains data in key/value pair

        4.StreamMessage :-Body contains a stream of primitive  values

        5.ObjectMessage : – Body contains an object

        6.Message :- Nothing in body. Only header and properties.

c)properties :- Additional properties other than header.

Reliability Mechanisms in JMS

JMS API has various mechanisms to ensure reliable messaging.They are:

1)Specifying message persistence :- It is possible to make messages persistent or non-persistent.Persistent messages will be persisted in case of JMS provider failure. It ensures reliable message delivery.

2)Setting message priority levels :- It is possible to set the message priority levels. Messages with higher priorities will be delivered first.

3)Allowing messages to expire :– It is possible to set expiration time for messages.This mechanism avoids the delivery of messages once it is obsolete.

4)Controlling message acknowledgment:-It is possible to make various levels of control over message acknowledgment. These controlling makes a reliable delivery of messages.

5)Creating Temporary Destinations:-Usually JMS destinations are created administratively . JMS API allows the creation of temporary destinations in program.Temporary destinations can be used to implement a request/response mechanism .It ensures reliable message delivery.

6)Creating durable subscriptions :-    In a Publish/Subscribe messaging domain , the subscriber should be active to get the published messages. If the subscriber is not active while a client publishes a message , the subscriber cannot get that message. If we create a durable subscriber in place of non-durable subscriber , the messages will not lost even if the subscriber is not active.This ensures a better reliability in case of Publish/Subscribe domain.

JMS Providers

There are different JMD providers available.For example JBoss 5.1 is bundled with JBoss Messaging commonly known as JBossMQ. JBoss 6.0 is bundled with HornetQ(Enhanced JBossMQ).We are explaining the concepts of JMS with OpenJMS in the coming chapters.

See also:

Configuring OpenJMS

JMS example

Sending and receiving ObjectMessages in JMS

JMS Domains:

Point to Point Messaging domain in JMS

Publish/Subscribe Messaging domain JMS

Message consumption

Synchronous message consumption in JMS

Asynchronous message consumption in JMS

Active MQ

JMS example using ActiveMQ