Issue
I have a problem when I want to set delivery delay for each message (IBM MQ and Spring boot). Normal way is to use jmsTemplate.deliveryDelay = 324234
but the problem is we are changing the property of the jmsTemplate
which is a global object (Singlteon). So I would like to send different delivery delays per message not a delay for all the messages (delay can be varied for each message).
It seems the right way is to set this delivery in header for each message like code below. but when I tried it, it didn't work. I received messages immediately.
jmsMessage.setLongProperty(
DELAY_DELIVERY_TAG,
deliveryDelayService.delay(message)
)
Does anyone know how I can set delivery delay per message?
Solution
The IBM docs on JMS 2.0 delivery delay says you can set it only on the producer (i.e. the JmsTemplate
), not the message:
An application can specify a delivery delay in milliseconds, when it sends a message, by using either MessageProducer.setDeliveryDelay(long deliveryDelay) or JMSProducer.setDeliveryDelay(long deliveryDelay)
It is fine to create a new JmsTemplate
per invocation; just read the Caching Connection Resources carefully and follow the advice written in the JmsTemplate Java API:
NOTE: The ConnectionFactory used with this template should return pooled Connections (or a single shared Connection) as well as pooled Sessions and MessageProducers. Otherwise, performance of ad-hoc JMS operations is going to suffer
Answered By - Daniel Steinmann
Answer Checked By - Clifford M. (JavaFixing Volunteer)