background image

Setting Message Priority Levels

<< Specifying Message Persistence | Advanced Reliability Mechanisms >>
<< Specifying Message Persistence | Advanced Reliability Mechanisms >>

Setting Message Priority Levels

Setting Message Priority Levels
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.
You can use the setPriority method of the MessageProducer interface to set the priority
level for all messages sent by that producer. For example, the following call sets a priority
level of 7 for a producer:
producer.setPriority(7);
You can 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 send
call sets the priority level for message to 3:
producer.send(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 have to deliver messages in exact order of priority.
Allowing Messages to Expire
By default, a message never expires. If a message will become obsolete after a certain period,
however, you may want to set an expiration time. You can do this in either of two ways.
You can use the setTimeToLive method of the MessageProducer interface to set a default
expiration time for all messages sent by that producer. For example, the following call sets a
time to live of one minute for a producer:
producer.setTimeToLive(60000);
You can 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 send call sets a time to live of 10 seconds:
producer.send(message, DeliveryMode.NON_PERSISTENT, 3, 10000);
If the specified timeToLive value is 0, the message never expires.
When the message is sent, the specified timeToLive is added to the current time to give the
expiration time. Any message not delivered before the specified expiration time is destroyed.
The destruction of obsolete messages conserves storage and computing resources.
Creating Temporary Destinations
Normally, you create JMS destinations (queues and topics) administratively rather than
programmatically. Your JMS provider includes a tool that you use to create and remove
destinations, and it is common for destinations to be long-lasting.
Creating Robust JMS Applications
Chapter 31 · The Java Message Service API
941