由于JmsTemplate内部发送消息默认为持久化的,所以不用设置即可发送持久化消息。
如果要发送非持久化的消息,请按照以下配置即可:
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 其他配置 -->
<property name="deliveryMode" value="1"></property>
<property name="explicitQosEnabled" value="true"></property>
</bean>
对于发送非持久化消息需要配置explicitQosEnabled该属性为true,否则spring内部发送消息时将采用默认配置进行发送消息,
而spring内部默认为持久化发送,所以上述属性必须设置为true,当设置为true时,外部设置的属性才可以使用。
下面为属性explicitQosEnabled的注释信息,如下:
/**
* If "true", then the values of deliveryMode, priority, and timeToLive
* will be used when sending a message. Otherwise, the default values,
* that may be set administratively, will be used.
* @return true if overriding default values of QOS parameters
* (deliveryMode, priority, and timeToLive)
* @see #setDeliveryMode
* @see #setPriority
* @see #setTimeToLive
*/
public boolean isExplicitQosEnabled() {
return this.explicitQosEnabled;
}
下面是发送消息的地方,如下代码所示:
protected void doSend(MessageProducer producer, Message message) throws JMSException {
if (isExplicitQosEnabled()) {
producer.send(message, getDeliveryMode(), getPriority(), getTimeToLive());
}
else {
producer.send(message);
}
}