37 JMS – I
Dr S. Abirami
- JMS
Java Message Service (JMS) is an application program interface (API) that supports communication using messaging services between the computers in a network.
1.1 Definition of Messaging
Messaging is a method of communication between software components or applications.A messaging system is a peer-to-peer facility whichenables distributed communication that is loosely coupled.Messaging differs from tightly coupled technologies such as Remote Method Invocation (RMI) and also differs from electronic mail (e-mail).
1.2 Messaging System
- A messaging system is a peer-to-peer facility which allows any number computer applications to communicate with each other. It can send messages to, and receive messages from, any other application. Each client connects to a messaging interface that provides facilities for creating, sending, receiving, and reading messages. A Messaging system which enables the Client connectivity with a message interface is shown in Figure 1.
1.3 Messaging Clients
A messaging client system handles the communication between the application interface and the physical network. A client can be either an open-source product or a commercial product. Clients deal with the issues of sending a message over an unreliable network. Functionality of Messaging Clients is shown in Figure 2.
- Java Message Service – An Introduction
Java message service was introduced by Sun J2EE in 1999. Web Services, CORBA, and Java RMI, by default, use synchronous communications.In the synchronous approach, the client makes a call and waits for a response which is an example of tight coupling.In contrast, JMS promotes loose coupling.
The Java Message Service is a Java API that allows the applications to create, send, receive, and read messages.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.The JMS API enables communication that is not only loosely coupled but also reliable and asynchronous communication. In asynchronous communication, JMS provider can deliver messages to a client as they arrive; a client does not have to request messages in order to receive them. In reliable communication, 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.
- JMS Products
The specification defines interfacesonly which leads to many distinct (incompatible) implementations and open source implémentations (e.g. OpenJMS).
List of JMS Vendors are as follows:
BEA | WebLogic |
IBM | WebSphere MQ |
Sun | Sun ONE Message Queue |
TIBCO | TIBCO Enterprise for JMS |
Oracle | Oracle Java Message Service (OJMS) |
Fiorano | FioranoMQ |
4. JMS Real Time Example
In order to understand the real time requirement, where JMS could be useful, a small scenario has been presented here. For example, in automobile production industry, the inventory component can send a message to the factory component when the inventory level for a product goes below a certain level, so the factory can make more cars. The factory component can send a message to the parts components so that the factory can assemble the parts it needs. The parts components in turn can send messages to their own inventory and order components to update their inventories and order new parts from suppliers. Both the factory and parts components can send messages to the accounting component to update their budgets. The business publishes updated catalog items to its sales force and web site. All of these communications can take place automatically using Java Messaging Service.
5. JMS APPLICATION
A JMS application is composed of the following parts:
JMS Clients – These are the Java language programs that send andreceive messages.
Non-JMS Clients – These are clients that use a message system’s native client API instead of JMS.
Messages – Each application defines a set of messages that are used to communicate information between its clients.
JMS Provider – This is a messaging system (MOM) that implements JMS API in addition to the other administrative and control functionality required in a full featured messaging product.
Administered Objects – Administered objects are preconfigured JMS objects created by an administrator for the use of clients. Administered objects are placed in a JNDI namespace by an administrator.There are two types of JMS administered objects:
- ConnectionFactory – This is the object a client uses to create a connection with a provider.
- Destination – This is the object a client uses to specify the destination of messages it is sending and the source of messages it receives.
5.1 Advantages of JMS
Asynchronous:JMS is asynchronous by default. Hence to receive a message, there is no need for the client to send a formal request. The message will arrive automatically to the client as they become available.
Reliable:JMS provides the facility of assurance that the message will be delivered once and only once. As you know that duplicate messages create problems, JMS helps you avoiding such problems.
- JMS COMPONENTS
A typical JMS system contains the following components:
JMS Client which includes a JMS Sender and JMS Receiver JMS Provider.
JMS Administered Objects which includes ConnectionFactory and Destination JMS Message.
MOST POPULAR JMS PROVIDERS
Some of the most popular JMS providers are given in the table 1 as shown below:
S.No | JMS PROVIDER SOFTWARE | ORGANIZATION |
1 | WebSphere MQ | IBM |
2 | Weblogic Messaging | Oracle Corporation |
3 | Active MQ | Apache Foundation |
4 | Rabbit MQ | Rabbit Technologies(acquired by Spring Source) |
5 | HornetQ | JBoss |
6 | Sonic MQ | Progress Software |
7 | TIBCO EMS | TIBCO |
8 | Open MQ | Oracle Corporation |
7. COMMUNICATION MODELS FOR JMS
There are two communication models for JMS. They are Synchronous and Asynchronous models.
Synchronous communication: Insynchronous communication, both communication parties need to be active. Sender receives a confirmation from the receiver after the message is delivered. Synchronous communication allows blocking calls and is employed in scenarios when global authorizations are required (e.g. credit cards authorization systems)
Asynchronous communication: In asynchronous communication, both parties do not need to be active during communication. Confirmations are not required in asynchronous communication and it allows non-blocking calls. It is useful when massive communication processing is required and allows for efficient usage of hardware resources.
8. JMS Messaging Models
The JMS API supports two models: Point to Point Model and Publish/Subscribe Model.
8.1 Point-to-Point (PTP) Model:
In point-to-point messaging model, a producer creates and sends a message which will be received only by a single consumer.In point-to-point model, the message destination is called “Queue”.
The following figure 3 shows PTP model where each message has only one customer. A sender and a receiver of a message have no timing dependencies. The receiver can fetch the message whether or not it was running when the client sent the message.The receiver acknowledges the successful processing of a message.
8.2 Publish/Subscribe Model:
In publish/subscribe model, any number of clients can subscribe to a message destination. When a client sends (publishes) a message to the destination, that message is received by all clients who have subscribed to that destination.In this model, the message producer is called publisher and the consumers are called subscribers. Also the message destination is called as “Topic”.
The following figure 4 shows Publish/Subscribe model where each message can have multiple consumers.Publishers and subscribers have a timing dependency. A client that subscribes to a topic can consume only messages published after the client has created a subscription, and the subscriber must continue to be active in order for it to consume messages.
9. JMS PROGRAMMING MODEL
The basic building blocks of a JMS application are:
- Administered objects: Connection factories and Destinations
- Connections
- Sessions
- Message producers
- Message consumers
- Messages
JMS Programming Model has been depicted in Figure 5 along with its components.
Let us see the components involved in it one by one.
9.1 JMS Administered Objects
These objects are preconfigured objects. Two types of administered objects are Connection Factories and Destination.
- Connection factory:This is an administered object with which a connection between JMS client and Service provider is established. A connection factory is an object a client uses to create a connection to a Provider. A connection factory encapsulates a set of connection configuration parameters that has been defined by an administrator. A client uses the Connection Factory to create a Connection to a JMS provider.JMS clients find administered objects by looking them up in a JNDI namespace.Connection factories comes in two forms by implementing either QueueConnectionFactory or TopicConnectionFactory interfaces as shown in Figure 6.
At the beginning of a JMS client program, we usually perform a JNDI API lookup of the connection factory.For example, the following code fragment obtains an InitialContext object and uses it to look up the QueueConnectionFactoryor the TopicConnectionFactory by name. Here ConnectionFactory can be created either as QueueConnectionFactory or TopicConnectionFactory.
Context context = new InitialContext();
//1. ConnectionFactory
ConnectionFactoryconnectionFactory =(ConnectionFactory) context.lookup(“ConnectionFactory”);
//2. QueueConnectionFactory
QueueConnectionFactoryqueueConnectionFactory =(QueueConnectionFactory) context.lookup(“QueueConnectionFactory”);
//3. TopicConnectionFactory
TopicConnectionFactorytopicConnectionFactory =
(TopicConnectionFactory) context.lookup(“TopicConnectionFactory”);
In JMS – Connection, a connection encapsulates a virtual connection with a JMS provider. It encapsulates the TCP / IP link with the JMS Provider and authenticates the client and specifies a unique client identifier. Like connection factories, connections come in two forms, by implementing either the QueueConnection or the TopicConnection interface as defined in Figure 7.For example, a connection could represent an open TCP/IP socket between a client and a provider service daemon. You can use a connection to create one or more sessions also. The following code segment creates a connection either using a QueueConnection or TopicConnection interface.
//1. Connection
Connection connection = connectionFactory.createConnection();
//2. Queue Connection
Queue ConnectionqueueConnection =queueConnectionFactory.createQueueConnection();
//3. TopicConnection
TopicConnectiontopicConnection=topicConnectionFactory.createTopicConnection();
A JMS client usually creates a connection, one or more sessions, and a number of message producers.When an application completes, it is needed to close any connections that have been created. Closing a connection also closes its sessions and their corresponding message producers and message consumers. The following lines are used to close connections either queueConnection or topicConnection accordingly.
- connection.close();
- queueConnection.close();
- topicConnection.close();
- Destination: Destination is the object into which a JMS client targets its messages. Also the JMS client receives messages from the destination. That’s why we call it as loosely coupled.A Destination object is a JMS administered object that encapsulates a provider-specific address for storing and retrieving messages.Destination, like session, comes in two forms, by implementing either the Queue or the Topic interface.A destination is the object a client uses, to specify the target of messages it produces and the source of messages it consumes. Interfaces for JMS Destination is shown in Figure 8.
Session object has been used to create a destinationwhereas a client uses JNDI API to look up the destination. The following line of code depicts the usage of Queue/Topic to lookup the Destination object which has been created.
Destination destination = (Queue)context.lookup(“queue/MyQueue”);
Queue queue = (Queue)context.lookup(“queue/MyQueue”);
Topic topic = (Topic)context.lookup(“topic/MyTopic”);
9.2 JMS – Sessions
Session is a lightweight JMS object which acts as a single-threaded context for producing and consuming messages. A Session creates the following items:
- Message producers,
- Message consumers,
- Messages,
- Message Destination (Queue or Topic)
Sessions, like Connections, get created in two flavours, either by implementing the QueueSession or the TopicSession interface as shown in Figure 9.To start with, we use a Connection object to create a session. Later, using that session object, either session of queueconnection or topicconnection has been created.
//1. Session
Session session = connection.createSession(false, Session. DUPS_OK_ACKNOWLEDGE);
//2. QueueSession
QueueSessionqueueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//3. TopicSession
TopicSessiontopicSession= topicConnection.createTopicSession(true, 0);
9.3 JMS – Message
JMS application produces and consumes message.The Message interface is the root interface of all JMS messages. There are five types of messages available as listed below:
- BytesMessage,
- TextMessage,
- ObjectMessage,
- StreamMessage
- MapMessage.
We use Session object to create a message. Code Fragment to create a JMS Message has been given below. Text Message created here can belong to anyone of the five different types of messages as shown in Figure 10.
- TextMessage message = session.createTextMessage();
- message.setText(“Hello EJB3 MDB!!!”);
- BytesMessagebyteMessage = session.createBytesMessage();
- MapMessagemapMessage = session.createMapMessage();
- StreamMessagestreamMessage = session.createStreamMessage();
- ObjectMessageobjMsg = session.createObjectMessage();
Summary:
This module is intended to learn the basics of JMS, various components of JMS and the advantages and applications of JMS. This module also discusses about the JMS Messaging Model, Communication Model and Programming Models.
- http://www-sop.inria.fr/members/Francoise.Baude/AppRep/JMS.pdf
- http://theopentutorials.com/tutorials/java-ee/ejb3/mdb/java-message-service-jms-api/
- http://www.computing.dcu.ie/~mcrane/CA4006/CA4006%20Lecture%206%20Message%20Pas sing%20in%20Distd%20Systems.pdf
- https://www.novell.com/documentation/extend5/Docs/help/MP/jms/tutorial/
- http://www.indigoo.com/dox/wsmw/1_Middleware/JMS.pdf
- http://www-sop.inria.fr/members/Francoise.Baude/AppRep/JMS.pdf
- https://www.tutorialspoint.com/jsp/
- Mark Richards, Richard Monson-Haefel, AndDavid A. Chappell,“ Java Message Service”,Second Edition, O’Reilly Media,2009.