Java Reference
In-Depth Information
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 pri-
ority 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 ex-
piration 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 des-
troyed. 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 to create and remove destinations,
and it is common for destinations to be long-lasting.
The JMS API also enables you to create destinations (
TemporaryQueue
and
Tem-
poraryTopic
objects) that last only for the duration of the connection in which they
are created. You create these destinations dynamically using the
Ses-
sion.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