JMS - Ensuring Reliability | Part - 2


Continuing from Part 1 which talked about Message Acknowledgment and means of ensuring basic reliability with JMS, here we talk about Persistence, Expiration, Priority and Temporary Queues/Topics.

What are the two delivery modes for messages to specify their Persistence? And, how do you specify them?
The two delivery modes are fields of the DeliveryMode interface and are as follows.

  1. The PERSISTENT delivery mode, which is the default, instructs the JMS provider to take extra care to ensure that a message is not lost in transit in case of a JMS provider failure. A message sent with this delivery mode is logged to stable storage when it is sent.
  2. The NON_PERSISTENT delivery mode does not require the JMS provider to store the message or otherwise guarantee that it is not lost if the provider fails.

You can specify the delivery mode in either of two ways.

  1. Use the setDeliveryMode() method of the MessageProducer interface - the parent of the QueueSender and the TopicPublisher interfaces - to set the delivery mode for all messages sent by that producer. E.g.
    TopicPublisher topicPublisher = topicSession.createPublisher(myTopic);
    topicPublisher.setDeliverymode(DeliveryMode.PERSISTENT);
  2. Use the long form of the send() or the publish() method to set the delivery mode for a specific message. The second argument sets the delivery mode. For example, the following publish() call sets the delivery mode for message to NON_PERSISTENT: (the third and fourth arguments set the priority level and expiration time)
    topicPublisher.publish(message, DeliveryMode.NON_PERSISTENT, 3, 10000);

If you do not specify a delivery mode, the default is PERSISTENT. Using the NON_PERSISTENT delivery mode may improve performance and reduce storage overhead, but you should use it only if your application can afford to miss messages.

How do you set message priority levels and what does priority indicate?
You can use message priority levels to instruct the JMS provider to deliver urgent messages first. You can set the priority level in either of two ways.

  1. Use the setPriority() method of the MessageProducer interface to set the priority level for all messages sent by that producer.
  2. Use the long form of the send() or the publish() method to set the priority level for a specific message. The third argument sets the priority level. For example, the following publish() call sets the priority level for message to 3:
    topicPublisher.publish(message, DeliveryMode.NON_PERSISTENT, 3, 10000);

The ten levels of priority range from 0 (lowest) to 9 (highest). If you do not specify a priority level, the default level is 4. A JMS provider tries to deliver higher-priority messages before lower-priority ones but DOES NOT GURANTEE the delivery of messages in exact order of priority.

How do you allow a JMS Message to expire?
By default, a message never expires. From the business standpoint, if a message will become obsolete after a certain period, you should set an expiration time. You can do this in either of two ways.

  1. Use the setTimeToLive() method of the MessageProducer interface to set a default expiration time for all messages sent by that producer.
  2. Use the long form of the send() or the publish() method to set an expiration time for a specific message. The fourth argument sets the expiration time in milliseconds. For example, the following publish call sets a time to live of 10 seconds:
    topicPublisher.publish(message, DeliveryMode.NON_PERSISTENT, 3, 10000);

If the specified timeToLive value is 0, the message never expires. Any message not delivered before the specified expiration time is destroyed. The destruction of obsolete messages conserves storage and computing resources.

What are temporary destinations and how are they used?

The JMS API also enables you to create temp 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 QueueSession.createTemporaryQueue() and the TopicSession.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 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, the consumer of the message can use the value of the JMSReplyTo field as the destination to which it sends a reply and 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.

0 Response to “JMS - Ensuring Reliability | Part - 2”


Leave a Reply