O'Reilly - Java Message Service
O'Reilly - Java Message Service
O'Reilly - Java Message Service
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
Before examining the source code in detail, a quick explanation will be helpful. The chat<br />
client creates a JMS publisher and subscriber for a specific topic. The topic represents the<br />
chat room. The JMS server registers all the JMS clients that want to publish or subscribe to<br />
a specific topic. When text is entered at the command line of one of the chat clients, it is<br />
published to the messaging server. The messaging server identifies the topic associated<br />
with the publisher and delivers the message to all the JMS clients that have subscribed to<br />
that topic. As Figure 2.1 illustrates, messages published by any one of the JMS clients are<br />
delivered to all the JMS subscribers for that topic.<br />
2.1.2 Examining the Source Code<br />
Running the Chat example in a couple of command windows demonstrates what the Chat<br />
application does. The rest of this chapter examines the source code for the Chat application<br />
so that you can see how the Chat application works.<br />
2.1.2.1 Bootstrapping the JMS client<br />
The main( ) method bootstraps the chat client and provides a command-line interface.<br />
Once an instance of the Chat class is created, the main( ) method spends the rest of its time<br />
reading text typed at the command line and passing it to the Chat instance using the<br />
instance's write<strong>Message</strong>( ) method.<br />
The Chat instance connects to the topic and receives and delivers messages. The Chat<br />
instance starts its life in the constructor, which does all the work to connect to the topic<br />
and set up the TopicPublisher and TopicSubscribers for delivering and receiving<br />
messages.<br />
2.1.2.2 Obtaining a JNDI connection<br />
The chat client starts by obtaining a JNDI connection to the JMS messaging server. JNDI<br />
is an implementation-independent API for directory and naming systems. A directory<br />
service provides JMS clients with access to ConnectionFactory and Destinations (topics<br />
and queues) objects. ConnectionFactory and Destination objects are the only things in<br />
JMS that cannot be obtained using the JMS API - unlike connections, sessions, producers,<br />
consumers, and messages, which are manufactured using the JMS API. JNDI provides a<br />
convenient, location-transparent, configurable, and portable mechanism for obtaining<br />
ConnectionFactory and Destination objects, also called JMS administered objects because<br />
they are established and configured by a system administrator.<br />
Using JNDI, a JMS client can obtain access to a JMS provider by first looking up a<br />
ConnectionFactory. The ConnectionFactory is used to create JMS connections, which can<br />
then be used for sending and receiving messages. Destination objects, which represent<br />
virtual channels (topics and queues) in JMS, are also obtained via JNDI and are used by<br />
the JMS client. The directory service can be configured by the system administrator to<br />
provide JMS administered objects so that the JMS clients don't need to use proprietary<br />
code to access a JMS provider.<br />
JMS servers will either work with a separate directory service (e.g., LDAP) or provide<br />
their own directory service that supports the JNDI API. For more details on JNDI, see the<br />
sidebar Understanding JNDI.<br />
22