Thursday, July 5, 2012

Java Message Service API

Before learn about JMS API lets take a look at what is meant by messaging. Messaging is used to communicate among software component or software applications. A component should send the message to a destination and the recipient component can get the message from that destination. The communication done by messaging is loosely coupled and asynchronous. That is how messaging differs from remote method invocations and polling systems.

Now I'm going to explain what JMS API is by having a better understanding about messaging. Java Message Service API allows component or applications to create, send, receive and read messages. This API is designed by Sun and several partner companies. JMS API communication is loosely coupled and enable asynchronous and reliable (The JMS API can ensure that a message is delivered once and only once) communication service.

JMS consists with four main parts.
  • JMS provider -This implements the JMS interface and provides other administrative and control features
  • JMS client – programs or components written in Java. These can produce and consume messages.
  • Messages – These are communication objects
  • Administered Objects – These are preconfigured JMS objects created by an administrator for clients' usage. e.g. Destination and Connection Factories.

JMS API supports both
  • point to point – Use a queue and there is only one consumer
  • publish/subscribe message domains.
    • There can be multiple consumers
    • messages are based on topics
    • subscribers receive messages which were published after their subscription
    • subscribers must continue to be active in order to consume messages.(JMS API relaxes this timing dependency up to certain extend by allowing subscribers to create durable subscriptions, which receive messages sent while the subscribers are not active)

Version 1.1 of JMS API allows you to use the same code to both domains while message destination remains domain specific. the behavior of the application will depend in part on whether you are using a queue or a topic.

Message Consumption

JMS API provides both synchronous and asynchronous transitions.

  • Synchronous- Subscriber or the receiver explicitly fetches the message by calling the receive method. Receive method will wait until the message arrives or expires certain time limit.
  • Asynchronous- Client can register a message listener with a consumer.Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener's onMessage method, which acts on the contents of the message.

JMS Programming Components

  • Connection factory creates the connection
    • Connection factory is always a component of ConnectionFactory, QueueConnectionFactory or TopicConnectionFactory.
    • Connection provides an open TCP/IP connection between client and the provider.
  • connection creates the session.
    • One connection can create one or more sessions.
    • Session is a single threaded context to create and consume messages.
  • Session creates the Message Producer, Message Consumer and TextMessages.
  • Message producer creates the message and send it to the destination
  • Message consumer receive messages from the destination
    • receive method is used to receive messages synchronously.

In PTP destination is the queue.
In pub/sub destination is the topic.

No comments:

Post a Comment