background image

Advanced Reliability Mechanisms

<< Setting Message Priority Levels | The createDurableSubscriber Method >>
<< Setting Message Priority Levels | The createDurableSubscriber Method >>

Advanced Reliability Mechanisms

The JMS API also enables you to create destinations (TemporaryQueue and TemporaryTopic
objects) that last only for the duration of the connection in which they are created. You create
these destinations dynamically using the Session.createTemporaryQueue and the
Session.createTemporaryTopic
methods.
The only message consumers that can consume from a temporary destination are those created
by the same connection that created the destination. Any message producer can send to the
temporary destination. If you close the connection that a temporary destination belongs to, the
destination is closed and its contents are lost.
You can use temporary destinations to implement a simple request/reply mechanism. If you
create a temporary destination and specify it as the value of the JMSReplyTo message header
field when you send a message, then the consumer of the message can use the value of the
JMSReplyTo
field as the destination to which it sends a reply. The consumer can also reference
the original request by setting the JMSCorrelationID header field of the reply message to the
value of the JMSMessageID header field of the request. For example, an onMessage method can
create a session so that it can send a reply to the message it receives. It can use code such as the
following:
producer = session.createProducer(msg.getJMSReplyTo());
replyMsg = session.createTextMessage(
"Consumer " +
"processed message: " + msg.getText());
replyMsg.setJMSCorrelationID(msg.getJMSMessageID());
producer.send(replyMsg);
For more examples, see
Chapter 32, "Java EE Examples Using the JMS API."
Using Advanced Reliability Mechanisms
The more advanced mechanisms for achieving reliable message delivery are the following:
Creating durable subscriptions
: You can create durable topic subscriptions, which receive
messages published while the subscriber is not active. Durable subscriptions offer the
reliability of queues to the publish/subscribe message domain.
Using local transactions
: You can use local transactions, which allow you to group a series
of sends and receives into an atomic unit of work. Transactions are rolled back if they fail at
any time.
Creating Durable Subscriptions
To ensure that a pub/sub application receives all published messages, use PERSISTENT delivery
mode for the publishers. In addition, use durable subscriptions for the subscribers.
The Session.createConsumer method creates a nondurable subscriber if a topic is specified as
the destination. A nondurable subscriber can receive only messages that are published while it
is active.
Creating Robust JMS Applications
The Java EE 5 Tutorial · September 2007
942