How to set Message Persistence for JMS messages?

Answered

How to set Message Persistence for JMS messages?

Ninja Asked on 19th September 2018 in JMS.
Add Comment
1 Answer(s)
Best answer

Message persistence can be set using the two delivery mode fields of the DeliveryMode interface.

  • The PERSISTENT delivery mode, 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.
  • 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:

  • Use the setDeliveryMode method of the MessageProducer interface to set the delivery mode for all messages sent by that producer. For example, the following call sets the delivery mode to NON_PERSISTENT for a producer:

producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

  • 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 send call sets the delivery mode for message to NON_PERSISTENT:

producer.send(message, DeliveryMode.NON_PERSISTENT, 3, 10000);

The third and fourth arguments set the priority level and expiration time.

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.

Ninja Answered on 19th September 2018.
Add Comment