How to Control Message Delivery (Delivery Mode, Time-to-live and Priority)
JMS provides three ways of influencing the delivery of messages: delivery mode, priority and time-to-live.
- The delivery mode can be either PERSISTENT or NON_PERSISTENT. A persistent message's delivery is guaranteed. A non-persistent message may not arrive in some cases, for example when the messaging server is being shut down before the delivery. The advantage of non-persistent messages is that they may be faster.
- Messages with higher priority can arrive earlier than messages with lower priority.
- Time-to-live timeouts can prevent the delivery of messages that are older than a configurable amount of time.
Delivery mode, priority and timeout are best-effort services. JMS providers should do their best to implement them, but it is not required that they always work as you may expect. For example, JMS allows the delivery of an expired message.
In order to set a message's delivery options, there are variants of the send method that allow setting them for each message. Alternatively you could also set defaults in the MessageProducer. If you don't do this either, implementation-specific defaults are taken (which can usually be configured somewhere).
MessageProducer producer = ...;
Message msg = ...;
producer.send(msg,
DeliveryMode.NON_PERSISTENT, // delivery mode
4, // priority (0-9, 9 is highest)
1000*60); // time-to-live in milliseconds(here: 60 seconds)
The receiver can find out with which options a message has been sent by looking at the JMSDeliveryMode, JMSExpiration and JMSPriority header fields.