15
A Study in JMS (Java Messaging Service) Chad Beaudin CS 522 Fall Semester 2002

A Study in JMS (Java Messaging Service)

Embed Size (px)

DESCRIPTION

A Study in JMS (Java Messaging Service). Chad Beaudin CS 522 Fall Semester 2002. JMS Overview. The Java Message Service(JMS) is a Java API that allows applications to create, send, receive, and read messages - PowerPoint PPT Presentation

Citation preview

Page 1: A Study in JMS  (Java Messaging Service)

A Study in JMS (Java Messaging

Service)

Chad Beaudin

CS 522

Fall Semester 2002

Page 2: A Study in JMS  (Java Messaging Service)

JMS OverviewJMS Overview

The Java Message Service(JMS) is a Java API that allows applications to create, send, receive, and read messages

The JMS API minimizes the set of concepts a programmer must learn to use messaging products but provides enough features to support sophisticated messaging applications

Page 3: A Study in JMS  (Java Messaging Service)

JMS Overview (cont)JMS Overview (cont)The JMS API enables communication that is not only loosely coupled but also: Asynchronous. 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. Reliable. The JMS API can ensure that a message is delivered once and only once. Lower levels of reliability are available for applications that can afford to miss messages or to receive duplicate messages.

Page 4: A Study in JMS  (Java Messaging Service)

JMS Overview (cont)JMS Overview (cont)Messages can be consumed in either of two ways:

Synchronously. A subscriber or a receiver explicitly fetches the message from the destination by calling the receive method. The receive method can block until a message arrives or can time out if a message does not arrive within a specified time limit.

Asynchronously. A client can register a message listener with a consumer. A message listener is similar to an event listener. 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.

Page 5: A Study in JMS  (Java Messaging Service)

Messaging BenefitsMessaging Benefits

Messaging provides the ability to send data asynchronously and in a disconnected manner.

Two messaging modelsPoint-to-point

Publish-and-Subscribe

In an email like model, these would be equivalent to sending and receiving e-mails directly (point-to-point), or subscribing to a list server and sending and receiving e-mails through the list server (publish-and-subscribe).

Page 6: A Study in JMS  (Java Messaging Service)

JMS And JNDIJMS And JNDIJMS does not not define a standard address syntax by which clients communicate with each other. Instead JMS utilizes Java Naming & Directory Interface(JNDI). JNDI provides a mechanism by which clients can perform a “lookup” to find the correct information to connect to each other.Using JNDI provides the following advantages:

It hides provider-specific details from JMS clients. It abstracts JMS administrative information into Java objects that are easily organized and administrated from a common management console. Since there will be JNDI providers for all popular naming services, this means JMS providers can deliver one implementation of administered objects that will run everywhere. Thereby eliminating deployment and configuration issues.

Page 7: A Study in JMS  (Java Messaging Service)

JMS Advantages over RPCJMS Advantages over RPCRPC relies on the physical connection of the client and server to the network; it is a synchronous protocol.

What happens if the client is disconnected? Network could go down

Client could be a laptop that is used on the road.

In this case, the end user might still want to carry on working, but can't if an RPC model is being used—at least not without a great deal of work by the programmer.

Page 8: A Study in JMS  (Java Messaging Service)

What is the cost?What is the cost?JMS vs RPCJMS vs RPC

Applications become asynchronous by nature What if we require a method to give us a return value?

What if we require the data (the messages) to be delivered in a specific order?

Using messaging, JMS, we have to deal with these problems ourselves. RPC handled these issues for the programmer.

Page 9: A Study in JMS  (Java Messaging Service)

Messages ExplainedMessages ExplainedA message typically consists of a header and a body.

The message header contains vendor-specified values, but could also contain application-specific data as well.

Headers are typically name/value pairs.

The body contains data; the type of the data is defined by the specification.

TextA serialized Java objectOne of a number of other types of data.

Page 10: A Study in JMS  (Java Messaging Service)

Publisher SamplePublisher SampleSee MyTopicPublisher.java for source.1. Perform a JNDI API lookup of the TopicConnectionFactory and topic

topic = (Topic) jndiContext.lookup(topicName);

2. Create a connection and a session topicConnection = topicConnectionFactory.createTopicConnection();topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

3.Create a TopicPublisher topicPublisher = topicSession.createPublisher(topic);

4.Create a TextMessage Message = topicSession.createTextMessage();message.setText("This is message " + (i + 1));

5.Publishe one or more messages to the topictopicPublisher.publish(message);

6.Close the connection, which automatically closes the session and TopicPublisher

Page 11: A Study in JMS  (Java Messaging Service)

Subscriber SampleSubscriber SampleSee MyTopicSubscriber.java for source.1.Perform a JNDI API lookup of the TopicConnectionFactory and

topic (same as publisher)2.Create a connection and a session (same as publisher)3.Create a TopicSubscriber

topicSubscriber = topicSession.createSubscriber(topic);

4.Create an instance of the TextListener class and registers it as the message listener for the TopicSubscriber

topicListener = new TextListener();topicSubscriber.setMessageListener(topicListener);

5.Start the connection, causing message delivery to begin topicConnection.start();

6.Close the connection, which automatically closes the session and TopicSubscriber

topicConnection.close();

Page 12: A Study in JMS  (Java Messaging Service)

TextListener SampleTextListener Sample1. public void onMessage(Message message) {2. TextMessage msg = null;3. 4. try {5. if (message instanceof TextMessage) {6. msg = (TextMessage) message;7. System.out.println("Reading message: " + msg.getText());8. } else {9. System.out.println("Message of wrong type: " +10. message.getClass().getName());11. }12. } catch (JMSException e) {13. System.out.println("JMSException in onMessage(): " + e.toString());14. } catch (Throwable t) {15. System.out.println("Exception in onMessage():" + t.getMessage());16. }17.}

Page 13: A Study in JMS  (Java Messaging Service)

Running The SampleRunning The Sample1. Start the JMS provider. In this case the J2EE SDK

From a command prompt run the following command:

j2ee –verbose

Wait until the server displays the message "J2EE server startup complete

2. Create the Administered Object. This is the object to which you will publish and subscribe.

From a second command prompt run the following commandj2eeadmin -addJmsDestination CS522Topic topic

To verify the topic was created, view the list of Administered Objects by typing:j2eeadmin –listJmsDestination

Page 14: A Study in JMS  (Java Messaging Service)

Running The Sample (cont)Running The Sample (cont)3. Run the subscriber program

From a command prompt run the following command within the directory that contains the MyTopicSubscriber.class:java -Djms.properties=%J2EE_HOME%\config\jms_client.properties MyTopicSubscriber -topic=CS522Topic

4. Run the Publisher programFrom a command prompt run the following command within the directory that contains the MyTopicPublisher.class:java -Djms.properties=%J2EE_HOME%\config\jms_client.properties MyTopicPublisher -topic=CS522Topic -count=500 -delay=500

5. You will see text output in both the Publisher and Subscriber windows