JMS
Messaging systems
There are two type of messaging system
-
Point-to-point
-
Publish-subscribe
Point-to-point messaging
-
Messages are sent from an origin point to a destination point
-
They are sent via a queue in the middle
-
The queue itself is a long-lived service
-
The source and destination may be transient
-
The source establishes a session with the queue and sends messages to it -
synchronously
-
The destination establishes a session with the queue and receives messages from it -
synchronously
-
The source and destination need not be concurrent
-
The queue can hold messages till the destination is ready to receive them
Publish-subscribe
-
Sources publish to a topic
-
Subscribers to the topic are all sent the message
-
The "topic" is a long-lived service
-
It keeps a list of subscribers
-
The source and destinations are usually concurrent
JMS
-
JMS supports both types of messaging system
-
JMS is part of J2EE, but can run be run separately as long as you have
queue/topic servers
-
JMS can be run using the Java System Message Queue (IMQ), a more lightweight framework
than J2EE
JMS messages
-
A JMS message contains header info, mostly set by JMS
-
The body of a message can be of different types:
-
A
TextMessage
has a text body
-
A
BytesMessage
has a byte array body
-
A
MapMessage
has a set of string/value pairs, etc
-
A
TextMessage
has methods get/setText()
, etc
Locating the queue
-
The queue needs to be found, using a lookup system
-
JNDI is used by J2EE as lookup system
-
Search for a lookup system by
new InitialContext()
-
This will use properties such as
java.naming.factory.initial
which will need to be set to a suitable class for JNDI, System Message Queue, etc
-
Deploying the app as a J2EE app should set this property
-
Setting up the IMQ sets this property
Connections
-
Before you can talk to the queue, you need to get a connection
-
Get a
ConnectionFactory
from the lookup service e.g
connectionFactory = (ConnectionFactory)
jndiContext.lookup("jms/QueueConnectionFactory");
-
Use the factory to get a queue
dest = (Queue) jndiContext.lookup(queueName);
-
Use the factory to get a connection
connection = connectionFactory.createConnection()
-
Use the connection to destablish a session
on = connectionFactory.createConnection();
Session session = connection.createSession()
-
Within the session, use the queue to get a
Send/receive
-
Send a message by
producer.send(msg)
-
Receive a message by
msg = consumer.receive()
Message sender
Message receiver
Variations
-
Senders can block waiting for replies, or continue asynchronously
-
When a receiver gets a message, an acknowledgement can be automatically
generated
-
Messages sent to topics usually require subscribers to be alive in order
to receive them - if they are dead, they miss the messages
-
Subscribers can ask for messages to be kept till they come back on line
Utility class
The programmatic interface to the lookup service depends on the type of lookup service.
Sun supply a "portability" class to handle JDNI and IMQ lokup
References
Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Fri Oct 8 11:50:15 EST 2004
Copyright ©Jan Newmarch