JMS (Java Message Service)

What is messaging? 
  • Messaging is a method of communicating between software components and applications. Each client can register to a messaging service which provides facilities for creating, sending, receiving and reading messages. 
  • Messaging enables "loose coupling" in distributed computing.
What is JMS?
  • The Java Message Service is an API. It is not an implementation.
  • It minimizes the concepts programmer must know to write a rich messaging application. 
  • JMS enables communication
    • Asynchronous: Deliver messages as the client becomes alive;client does not have to request to receive messages
    • Reliable: Ensure that a message is delivered once and only once. 
JMS API Architecture and Concepts
  • Following figure shows the basic architecture of a messaging system with JMS.

    • JMS provider: a messaging system that "implements" JMS interface and provides administrative and control features.
    • JMS client: a program or a component that produces or consumes messages
    • Administrated objects: Pre-configured JMS objects created by an "administrator" to be used by a JMS client
    • Naive clients: all programs that use native client API for messaging instead of JMS.
  • JMS specification provides two messaging domains.
    1. Point-to-point messaging domain

      • Has message senders,receivers and "queues". 
      • Messages producers send a message to a specific queue.
      • receiving clients exact messages from queues.
      • Each message has only one consumer.
      • When the message is sent receiver may or may not present. But when it come alive messages are delivered in order.
                2. Publish/subscribe messaging domain 

      • Message produces sent them to a "topic". 
      • Publishers and subscribers can dynamically come and go. But messaging system is capable of distributing messages from multiple publishers to multiple subscribers.  
      • Publishers and subscribers have a time dependency because subscribers can only consume  messages that are published after the subscription has done. 
  • Message consuming can also done in two ways. 
    • Synchronously: Explicitly fetch messages by calling "receive" method.
    • Asynchronously: Whenever a message is addressed, JMS provider delivers it to the "message listener" which is subscribed with that particular consumer using "onMessage" method.

Basic Programming hierarchy 

  • Create a Contex.
  • Create a QueueConnectionFactory or a TopicConnectionFactory using a contex lookup (Java Naming and Directory Interface /"JNID" lookup).
  • Create a topic or a queue. Can use multiple topics or queues. 
  • Create a "connection" (a virtual connection with JMS provider) using connection factory created.
  • Create a "session" with the connection created.
  • Create a queuesender or a topicproducer using the session created. You have to specify the topic or queue it should send messages to.
  • Send or publish the message by queueSender.send(message) or topicPublisher.publish(message);
  • Create queueReceiver or topicSubscriber using the session. 
  • Receive messages by 
                   Message m = queueReceiver.receive();
                   Message m = topicSubscriber.receive(1000); //timeout time

Additionally JMS API enables you to,
  • Subscribe message listeners with subscribers/receivers
  •  Filter messages using message selectors
  • Write custom messages agreed with the API
  • Handle exceptions ( The root class for exceptions thrown by JMS API methods is JMSException. Catching JMSException provides a generic way of handling all exceptions related to the JMS API.)
With the next post I will introduce you with "Hello World example"
  • Aparche Qpid which supports JMS API
  • WSO2 Message Broker which runs on top of aparche Qpid.

Hasitha Hiranya

No comments:

Post a Comment