0% found this document useful (0 votes)
37 views

Java Message Service (JMS)

Java Message Service (JMS) is an API that allows Java programs to asynchronously communicate through messages. It supports two messaging domains - point-to-point and publish-subscribe. For point-to-point, messages are delivered to a single recipient via queues while publish-subscribe delivers messages to multiple subscribers via topics. The JMS API defines common interfaces for message producers, consumers, and administrators to send, receive, and manage messages.

Uploaded by

Priya Tomar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Java Message Service (JMS)

Java Message Service (JMS) is an API that allows Java programs to asynchronously communicate through messages. It supports two messaging domains - point-to-point and publish-subscribe. For point-to-point, messages are delivered to a single recipient via queues while publish-subscribe delivers messages to multiple subscribers via topics. The JMS API defines common interfaces for message producers, consumers, and administrators to send, receive, and manage messages.

Uploaded by

Priya Tomar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Java Message Service (JMS)

Guided by:ByMrs. M.S. Bhende Rana Seminar Parth T.E.

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.)

Building Blocks of a JMS Application


JMS Clients o Java programs that send/receive messages Administered Objects o preconfigured JMS objects created by an admin for the use of clients o ConnectionFactory, Destination (queue or topic) JMS Provider o messaging system that implements JMS and administrative functionality

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

JMS Messaging Domains


JMS deals with two kinds of message domains: 1. Point-to-Point 2. Publish-Subscribe

Point-to-Point (PTP) Messaging Domain


Features : It consists of message queues, senders, and receivers. Message queue is used to hold the messages sent to them from the sender until the messages are consumed by the receivers or until the messages expire. Each message has only one consumer

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

Publish - Subscribe Messaging Domains


Features Clients address messages to a topic. Topics retain messages only as long as it takes to distribute them to current subscribers. Each message has multiple consumers/recievers. Sender and receiver have a timing dependencies.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.

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

JMS API Programming Model


Connection Factory creates Connection Message Producer creates creates Message Consumer receives from Destination

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

You might also like