Saturday, January 26, 2008

Importance of "Java Messaging Service" (JMS API) in Server Side Development

According to me Server-side programming & developing server-side components is the most challenging area for any Java Developer.Gone are the days for simpler client/server applications,and with emerging technologies like SOA,ESB(Enterprise Service Bus),BPM(Business Process Management) being considered by many large scale companies be it in financial sector or any other domain for their applications which are mainly n-tier applications,the efforts need to be put in designing and developing middle-tier components has increased and if you see ,most of the open source frameworks main focus is on Server-side programming,being a critical component in any n-tier application.And one API which has made my life easy in that regard is Java Messaging Service(JMS).

In my initial days(Iam talking about 9 years back) with Java,while working on different assignments I used different technologies like RMI,Corba and EJB for developing such applications and having seen the growth and emergence of different frameworks and APIs,I would have loved to start my IT career ,may be couple of years back but those struggling days have definitely made me sit up and realize the importance and advantages these new technologies are helping me out in design and development particularly in integration of enterprise applications in distributed computing arena with regards to developing highly scalable,reliable,loosely coupled messaging systems and providing asynchronous interactions
among J2EE components and legacy systems capable of messaging,compared to tightly coupled integration & legacy I was part of in my initial days.

Ok lets analyze the strides made by JMS API and the benefits & advantages it provides to any server side developer or architect.

What is Messaging?
Messaging is a method of communication between software components or applications.A messaging system is a peer-to-peer facility: A messaging client can send messages to, and receive messages from, any other client. Each client connects to a messaging agent that provides facilities for creating, sending, receiving, and reading messages. Messaging enables distributed communication that is loosely coupled. A component sends a message to a destination, and the recipient can retrieve the message from the destination. However, the sender and the receiver do not have to be available at the same time in order to communicate. In fact, the sender does not need to know anything about the receiver; nor does the receiver need to know anything
about the sender. The sender and the receiver need to know only what message format and what destination to use. In this respect, messaging differs from tightly coupled technologies, such as Remote Method Invocation (RMI) or RPC which require an application to know a remote application’s methods.

Enterprise messaging products (or as they are sometimes called, Message Oriented Middleware products) are becoming an essential component for integrating intra-company operations. They allow separate business components to be combined into a reliable, yet flexible, system.
In addition to the traditional MOM vendors, enterprise messaging products are also provided by several database vendors and a number of internet related companies.Java language clients and Java language middle tier services must be capable of using these messaging systems. JMS provides a common way for Java language programs to access these systems.the JMS API defines a common set of interfaces and associated semantics that allow programs written in the Java programming language to communicate with other messaging implementations.
And thats where JMS is useful and if you add Message Driven Beans(MDB) (combination of Session beans and JMS clients) ,introduced as part of EJB2.0 Specification,then you have an effective asynchronous messaging system in your middletier which covers areas like
security, concurrency, transaction etc.

The JMS API enables communication that is not only loosely coupled but also
• Asynchronous. A JMS provider can deliver messages to a client as they arrive;a client does not have to request messages in order to receive them.
• Reliable. The JMS API can ensure that a message is delivered once and only once. Lower levels of reliability are available for applications that can afford to miss messages or to receive duplicate messages.

Why do you need JMS ?
There are several reasons why you need JMS in a enterprise middleware application :
i)Decoupling : Different parts of an application can be developed such that they are not closely tied to each other
ii)Flexible Integration:Conversely loosely coupled systems can be put together by using MDB for wrapping existing systems.
iii)Separation of elements involved in messaging applications,for example a logging event can be separated from ,say retrieving a customer details in a bank application.
iv)Delivering low-level services of an application in offline-mode,meaning although the service must be provided, the main workflow doesnt have to wait for its completion,for example a logging service
v)Delivering same information to multiple-parties

vi)Most of the top J2EE application servers support messaging.Sun provides a list of JMS vendors at

Some myths & drawbacks about using JMS
1)Additional overhead in handling messages and messaging systems
2)Message-based system can be a single point of failure.

Different types of Messages
There are two main ways to send messages: point-to-point and publish-subscribe.
A point-to-point messaging(queue) application has a single producer (usually) and a single consumer. The producer produces messages while the consumer consumes them.A point-to-point system can actually have multiple producers,but usually only a single consumer. For example a print server,any machine on the network can send a print job to a particular print server.

The publish-subscribe messaging model (pub-sub) is more of a broadcast-oriented model. Publish-subscribe is based on the idea of topics and typically has many consumers-and potentially many producers as well.For example, subscription to any forum

Fundamentals in JMS API
The basic elements in JMS are administered objects(connection factories & destinations),connections,sessions,message producers,message consumers ,queues ,topics and messages.

A connection factory is the object a client uses to create a connection with a provider.A connection factory encapsulates a set of connection configuration parameters that has been defined by an administrator. A pair of connection factories come preconfigured with the J2EE SDK and are accessible as soon as you start the service.Each connection factory is an instance of either the QueueConnectionFactory or the TopicConnectionFactory interface.

A message producer is an object created by a session and is used for sending messages to a destination. The Point-to-Point form of a message producer implements the Queue-Sender interface. The pub/sub form implements the TopicPublisher interface.

A message consumer is an object created by a session and is used for receiving messages sent to a destination. A message consumer allows a JMS client to register interest in a destination with a JMS provider. The JMS provider manages the delivery of messages from a destination to the registered consumers of the destination.The PTP form of message consumer implements the QueueReceiver interface.The pub/sub form implements the TopicSubscriber interface.

Different JMS Message Types
The JMS API defines five message body formats, also called message types, which allow you to send and to receive data in many different forms and provide compatibility with existing messaging formats.
i)TextMessage : A java.lang.String object (for example, the contents of an Extensible Markup Language file).
ii)MapMessage : A set of name/value pairs, with names as String objects and values as primitive types in the Java programming language. The entries can be accessed sequentially by enumerator or randomly by name. The order of the entries is undefined.
iii)BytesMessage : A stream of uninterpreted bytes. This message type is for literally encoding a body to match an existing message format.
iv)StreamMessage: A stream of primitive values in the Java programming language,filled and read sequentially.
v)ObjectMessage :A Serializable object in the Java programming language.
vi)Message : Composed of header fields and properties only. This message type is useful when a message body is not required.

In order to use JMS, one must have a JMS provider that can manage the sessions and queues. There are free, open source and proprietary providers.

Articles about open source providers:

Articles about proprietary providers:

Suggested Reading

Importance of JMS
Benefits of JMS

Print this post