In the chat example we explored in Chapter 2, we assumed that JMS clients would communicate with each other using established topics on which messages are asynchronously produced and consumed. In the next sections, we'll explore ways to augment this basic mechanism. We'll start by looking at temporary topics, which is a mechanism for JMS clients to create topics dynamically.
The constructor of the
Wholesaler
class creates a temporary topic. This
topic is used as a
JMSReplyTo
destination for messages published
to the "Hot Deals" topic in the
publishPriceQuotes( )
method:
public Wholesaler(String broker, String username, String password){ try { ... session = connect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); ...buyOrdersTopic = session.createTemporaryTopic( );
... } ... private void publishPriceQuotes(String dealDesc, String username, String itemDesc, float oldPrice, float newPrice){ try { javax.jms.StreamMessage message = session.createStreamMessage( ); ...message.setJMSReplyTo(buyOrdersTopic);
publisher.publish( message, javax.jms.DeliveryMode.PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY, 600000); ... }
When the Retailer
client decides to respond to a
"Hot Deals" message with a buy order, it uses the
JMSReplyTo
destination, which is the temporary
topic created by Wholesaler
application:
private void autoBuy (javax.jms.Message message){ int count = 1000; try { StreamMessage strmMsg = (StreamMessage)message; ... // If price reduction is greater than 10 percent, buy if (newPrice == 0 || oldPrice / newPrice > 1.1){ ...javax.jms.Topic buytopic = (javax.jms.Topic)message.getJMSReplyTo( );
publisher = session.createPublisher(buytopic
); ... }
A temporary topic is a topic that is dynamically created by the JMS
provider, using the createTemporaryTopic(
)
method of the
TopicSession
object. A temporary topic is associated with the connection that
belongs to the TopicSession
that created it. It is
only active for the duration of the connection, and it is guaranteed
to be unique across all connections. Since it is temporary, it
can't be durable: it lasts only as long as its associated
client connection is active. In all other respects it is just like a
"regular" topic.
Since a temporary topic is unique across all client
connections—-it is obtained dynamically through a method call
on a client's session object—it is unavailable to other
JMS clients unless the topic identity is transferred using the
JMSReplyTo
header. While any client may publish
messages on another client's temporary topic, only the sessions
that are associated with the JMS client connection that created the
temporary topic may subscribe to it. JMS clients can also, of course,
publish messages to their own temporary topics.
In the interest of exploring concepts like temporary topics we have
designed our B2B example so that the consumer responds directly to
the producer. In larger real-world applications, however, there may
be many publishers and subscribers exchanging messages across many
topics. A message may represent a workflow, which may take multiple
hops through various stages of a business process. In that type of
scenario the consumer of a message may never respond directly to the
producer that originated the message. It is more likely that the
response to the message will be forwarded to some other process.
Thus, the JMSReplyTo
header can be used as a place
to specify a forwarding address, rather than the destination address
of the original sender.
JMS provides a set of design patterns and helper classes for performing a direct request-reply conversation, which we will get into later in Section 4.6 of this chapter.