Is there a difference between calling MessageProducer.setTimeToLive()
vs. TextMessage.setJMSExpiration()
?
I can see from the documentation that javax.jms.MessageProducer.setTimeToLive() says that it
Sets the default length of time in milliseconds from its dispatch time that a produced message should be retained by the message system.
and that javax.jms.Message.setJMSExpiration() says that it
Sets the message's expiration value.
Those sound the same to me. Should I go ahead and set both to the same value?
You should not set expiration settings for Message
(or TextMessage
, as in your example). That text from Java EE 5 Javadocs is confusing, and it was improved in recent versions:
This method is for use by JMS providers only to set this field when a message is sent. This message cannot be used by clients to configure the expiration time of the message. This method is public to allow a JMS provider to set this field when sending a message whose implementation is not its own.
That means that you should NOT use Message#setJMSExpiration()
to set message expiration, since the provider will override that value when the message is sent.
You should instead use its MessageProducer
to set the timeout for all messages using MessageProducer#setTimeToLive()
or, if you only want to set the expiration for a particular message, do so using MessageProducer#send(Message, int, int, long)
as shown in Shashi's answer. Only in this case does the "per-message" setting override the MessageProducer
's setting.
There is a key difference, MessageProducer.setTimeToLive()
sets the same message expiry time on all messages sent by that producer. On the other hand TextMessage.setJMSExpiration()
sets message expiry time on a per message basis. The expiry time set on per message overrides the expiry set on MessageProducer
.
JMS also specifies a way to set message expiry time when calling MessageProducer.send
method.
void send(Message message, int deliveryMode,int priority, long timeToLive)
You can set message expiry on either MessageProducer
or TextMessage
. No need to set on both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With