Search This Blog

Thursday, December 17, 2009

Overview of Java 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.


A JMS application is composed of the following parts:
A JMS provider is a messaging system that implements the JMS interfaces and provides administrative and control features.
JMS clients are the programs or components, written in the JavaTM programming language, that produce and consume messages.
Messages are the objects that communicate information between JMS clients.
Administered objects are preconfigured JMS objects created by an administrator for the use of clients. The two kinds of administered objects are destinations and connection factories.
Native clients are programs that use a messaging product's native client API instead of the JMS API. An application first created before the JMS API became available and subsequently modified is likely to include both JMS and native clients.
JMS API can be used in some of the following ways:
Consume messages asynchronously with a message-driven bean
Produce messages from an application client
Produce messages from a session bean
Access an entity bean from a message-driven bean
Produce and consume messages on more than one system
An enterprise application provider is likely to choose a messaging API over a tightly coupled API, such as remote procedure call (RPC), under the following circumstances.
The provider wants the components not to depend on information about other components' interfaces, so that components can be easily replaced.
The provider wants the application to run whether or not all components are up and running simultaneously.
The application business model allows a component to send information to another and to continue to operate without receiving an immediate response.
For example, components of an enterprise application for an automobile manufacturer can use the JMS API in situations like these:
The inventory component can send a message to the factory component when the inventory level for a product goes below a certain level so that 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.
Performs a JNDI lookup of the ConnectionFactory and Destination
Look up connection factory and destination. If either does not exist, exit. If you look up a TopicConnectionFactory or a QueueConnectionFactory
Creates a Connection and a Session:
Connection connection =   connectionFactory.createConnection(); Session session = connection.createSession(false,   Session.AUTO_ACKNOWLEDGE);
Creates a MessageProducer and a TextMessage:
MessageProducer producer = session.createProducer(dest); TextMessage message = session.createTextMessage();
Sends one or more messages to the destination:
for (int i = 0; i < NUM_MSGS; i++) {   message.setText("This is message " + (i + 1));   System.out.println("Sending message: " +     message.getText());   producer.send(message); }
Sends an empty control message to indicate the end of the message stream:
producer.send(session.createMessage());
Finally close the connection using connection.close() in the finally clause.


A sequence of how queue and topic would be used:
Queue name is jms/ControlQueue
Queue name is jms/Queue
Topic name is jms/Topic
Connection factory name is jms/DurableConnectionFactory
  SENDER: Created client-acknowledge session
  SENDER: Sending message: Here is a client-acknowledge message
  RECEIVER: Created client-acknowledge session
  RECEIVER: Processing message: Here is a client-acknowledge 
message
  RECEIVER: Now I'll acknowledge the message
PUBLISHER: Created auto-acknowledge session
SUBSCRIBER: Created auto-acknowledge session
PUBLISHER: Receiving synchronize messages from jms/
ControlQueue; count = 1
SUBSCRIBER: Sending synchronize message to jms/ControlQueue
PUBLISHER: Received synchronize message;  expect 0 more
PUBLISHER: Publishing message: Here is an auto-acknowledge 
message 1
PUBLISHER: Publishing message: Here is an auto-acknowledge 
message 2
SUBSCRIBER: Processing message: Here is an auto-acknowledge 
message 1
PUBLISHER: Publishing message: Here is an auto-acknowledge 
message 3
SUBSCRIBER: Processing message: Here is an auto-acknowledge 
message 2
SUBSCRIBER: Processing message: Here is an auto-acknowledge 
message 3