A Study in JMS (Java Messaging Service) : Chad Beaudin CS 522 Fall Semester 2002
A Study in JMS (Java Messaging Service) : Chad Beaudin CS 522 Fall Semester 2002
A Study in JMS (Java Messaging Service) : Chad Beaudin CS 522 Fall Semester 2002
(Java Messaging
Service)
Chad Beaudin
CS 522
Fall Semester 2002
JMS Overview
The Java Message Service 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
Messaging Benefits
JMS vs RPC
Messaging provides the ability to send data
asynchronously and in a disconnected manner.
Two messaging models
Point-to-point
Publish-and-Subscribe
Messages Explained
A 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.
Publisher Sample
See MyTopicPublisher.java for source.
1. Perform a JNDI API lookup of the TopicConnectionFactory and topic
topic = (Topic) jndiContext.lookup(topicName);
3.Create a TopicPublisher
topicPublisher = topicSession.createPublisher(topic);
4.Create a TextMessage
Message = topicSession.createTextMessage();
message.setText("This is message " + (i + 1));
Subscriber Sample
See 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);
TextListener Sample
1. 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. }
2.
4.
5.
References
http://java.sun.com/products/jms
/tutorial/1_3_1-fcs/doc/jms_tutorialTOC
.html
http://java.sun.com/products/jndi/