在 Spring 集成中设置消息的生存时间

Setting time-to-live to messages in Spring Integration

我需要为我的消息设置一个生存时间。

我试过下面的例子,但是生存时间会被忽略。 :/

context.xml

<int:channel id="publishChannel"/>

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

出版商

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;
    }
}

希望有人能帮助我! :)

根据 JmsTemplate JavaDocs 我们有:

/**
 * 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;
}

因此,如果 explicitQosEnabled 不是 true (JmsTemplate#doSend),则它不起作用:

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

因此您应该为您的 <int-jms:outbound-channel-adapter> 添加 explicit-qos-enabled="true"time-to-live="5000"