Java Message Service (JMS)
Java Message Service (JMS)
Contents
What is JMS. Building Blocks of a JMS Application JMS Administration. JMS Messaging Domain. P-2-P Messaging and Pub/Sub Messaging. JMS-API Programming Model. JMS Example Applications. Conclusion.
What is JMS?
JMS is an API(Application Programming Interface) that describes a common way for Java programs to create, send, receive and read distributed enterprise messages It enables loosely coupled communication (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.)
What is JMS?
Asynchronous messaging (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.) It ensures Reliable delivery of messages. (A message is guaranteed to be 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 Administration
JNDI : Java Naming and Directory Interface is a Java API for a directory service that allows Java Software clients to discover and look up data and objects via a name. Types of administrative objects are: ConnectionFactory, Destination. Administrated objects are placed in a JNDI by an admin tool (j2eeadmin).Clients lookup the admin objects in the JNDI and then establish a logical connection to the same objects through provider. A JMS client uses JNDI to look up the ConnectionFactory and then uses the ConnectionFactory to establish a JMS connection.
JMS Administration
Administrative Tool Bind JNDI Namespace
Lookup
JMS Client
Logical Connection
JMS Provider
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. When to use PTP messaging? Use PTP messaging when every message you send must be processed successfully by one consumer.
Point-to-Point Messaging
Msg Client1 Queue acknowledges Sender and a Receiver have no timing dependencies. the receiver can fetch the message whether or not it was running when client sent the message receiver acknowledges. Msg consumes Client2
sends
The JMS API relaxes this timing dependency to some extent by allowing clients to create durable subscriptions. Durable subscriptions can receive messages sent while the subscribers are not active. Durable subscriptions provide the flexibility and reliability of queues but still allow clients to send messages to many recipients. When to use Pub-Sub messaging domain? Use pub/sub messaging when each message can be processed by zero, one, or many consumers.
Publish/Subscribe Messaging
subscribes Msg Client1 delivers publishes Topic subscribes Client3 delivers Multiple consumers publishers and subscribers have timing dependency. a client can consume only messages published after its subscription and must continue to be active to consume messages. Client2
Session
sends to Destination
creates Msg
Example
1. To create connections we can use default connection factory objects named QueueConnectionFactory and TopicConnectionFactory.You can also create new connection factories by using the following commands: j2eeadmin -addJmsFactory jndi_name queue j2eeadmin -addJmsFactory jndi_name topic Here j2eeadmin is an administrative tool available with the J2EE Software Development Kit (SDK) version 1.3.1
2. At the beginning of a JMS client program, you usually perform a JNDI API lookup of the connection factory. Context ctx = new InitialContext(); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory"); TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
3. Add destination for messages :j2eeadmin -addJmsDestination queue_name queue j2eeadmin -addJmsDestination topic_name topic 4. Performing JNDI look up for destination using context object: Queue myQueue = (Queue) ctx.lookup("MyQueue"); Topic myTopic = (Topic) ctx.lookup("MyTopic"); 5. Creating connection: QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
6. Creating a session: A session is a single context for producing and consuming message TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); The first argument means that the session is not transacted; the second means that the session automatically acknowledges messages when they have been received successfully.
Similarly, you use a QueueConnection object to create a QueueSession: QueueSession queueSession = queueConnection.createQueueSession(true, 0); Here, the first argument means that the session is transacted; the second indicates that message acknowledgment is not specified for transacted sessions. 7. Create Sender: QueueSender queueSender = queueSession.createSender(myQueue); TopicPublisher topicPublisher = topicSession.createPublisher(myTopic);
8. Sending Message to queue or topic: queueSender.send(message); // or Publish the message to a topic topicPublisher.publish(message); 9. Create Receiver: QueueReceiver queueReceiver = queueSession.createReceiver(myQueue); TopicSubscriber topicSubscriber = topicSession.createSubscriber(myTopic);
10. Receive Message: queueConnection.start(); Message m = queueReceiver.receive(); topicConnection.start(); Message m = topicSubscriber.receive(1000); // time out after a second
Applications
Web chat communication for customer service. Queued transactions for delayed activity such as billing, order processing, email generation.
Conclusion
Thus we have seen that Java Message Service (JMS) makes it easy to develop enterprise applications that send and receive business data and events.JMS makes the learning curve easy by minimizing the set of concepts a Java developer must learn to use enterprise messaging products.
Thank You