我不知道爲什麼Spring決定排除這個,但是你可以擴展JmsTemplate並重載一些方法,傳遞一個timeToLive參數。
public class MyJmsTemplate extends JmsTemplate {
public void send(final Destination destination,
final MessageCreator messageCreator, final long timeToLive)
throws JmsException {
execute(new SessionCallback<Object>() {
public Object doInJms(Session session) throws JMSException {
doSend(session, destination, messageCreator, timeToLive);
return null;
}
}, false);
}
protected void doSend(Session session, Destination destination,
MessageCreator messageCreator, long timeToLive) throws JMSException {
Assert.notNull(messageCreator, "MessageCreator must not be null");
MessageProducer producer = createProducer(session, destination);
try {
Message message = messageCreator.createMessage(session);
if (logger.isDebugEnabled()) {
logger.debug("Sending created message: " + message);
}
doSend(producer, message, timeToLive);
// Check commit - avoid commit call within a JTA transaction.
if (session.getTransacted() && isSessionLocallyTransacted(session)) {
// Transacted session created by this template -> commit.
JmsUtils.commitIfNecessary(session);
}
} finally {
JmsUtils.closeMessageProducer(producer);
}
}
protected void doSend(MessageProducer producer, Message message,
long timeToLive) throws JMSException {
if (isExplicitQosEnabled() && timeToLive > 0) {
producer.send(message, getDeliveryMode(), getPriority(), timeToLive);
} else {
producer.send(message);
}
}
}
謝謝,我錯過了這一點,我可以設置'explicitQosEnabled'到TRUE;,但在參數TTL,producer.send()函數通過JmsTemplate的不適用於我。 – iMysak
不,你在模板上設置TTL;那麼當明確的QOS被啓用時,模板將在發送時設置TTL。 [見這裏](https://github.com/spring-projects/spring-framework/blob/master/spring-jms/src/main/java/org/springframework/jms/core/JmsTemplate.java#L622)。 –
是的,這是非常清楚的,但在這種情況下,我不能使用不同的過期不同的消息,因爲我需要。我無法在併發模式下使用它。 – iMysak