Search code examples
springjmsspring-integrationmessagingqpid

Setting time-to-live to messages in Spring Integration


i need to set a time-to-live to my messages.

I tried the following example, but the time-to-live will be ignored. :/

context.xml

<int:channel id="publishChannel"/>

<int-jms:outbound-channel-adapter 
         channel="publishChannel" 
         destination="defaultDestination"
         time-to-live="5000" 
         pub-sub-domain="false" />

Publisher

import org.springframework.integration.annotation.Publisher;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Service;

@Service("publishService")
public class PublishService{
    private MessageChannel messageChannel;

    @Publisher(channel = "publishChannel")
    public Message<?> sendMessage (Message<?> message) {
        return message;
    }
}

I hope someone can help me! :)


Solution

  • According JmsTemplate JavaDocs we have:

    /**
     * Set the time-to-live of the message when sending.
     * <p>Since a default value may be defined administratively,
     * this is only used when "isExplicitQosEnabled" equals "true".
     * @param timeToLive the message's lifetime (in milliseconds)
     * @see #isExplicitQosEnabled
     * @see javax.jms.Message#DEFAULT_TIME_TO_LIVE
     * @see javax.jms.MessageProducer#send(javax.jms.Message, int, int, long)
     */
    public void setTimeToLive(long timeToLive) {
        this.timeToLive = timeToLive;
    }
    

    So, it doesn't work if explicitQosEnabled isn't true (JmsTemplate#doSend):

    if (isExplicitQosEnabled()) {
        producer.send(message, getDeliveryMode(), getPriority(), getTimeToLive());
    }
    

    Hence you should add explicit-qos-enabled="true" alongside with time-to-live="5000" for your <int-jms:outbound-channel-adapter>.