使用Spring JMS与ActiveMQ - 如何发送非持久化消息?

6

我正在尝试将deliveryMode明确设置为NONPERSISTENT并发送到ActiveMQ。

这是我的Spring JMS配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="http://www.springframework.org/schema/jms
      http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.app" />
    <!-- enable the configuration of jms on annotations -->
    <jms:annotation-driven/>

    <!-- =============================================== -->
    <!-- JMS Common, Define JMS connectionFactory       -->
    <!-- =============================================== -->
    <!-- Activemq connection factory -->
    <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <!-- brokerURL, You may have different IP or port -->
        <constructor-arg index="0" value="tcp://localhost:61616" />
    </bean>

    <!-- Pooled Spring connection factory -->
    <bean id="connectionFactory"
          class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="amqConnectionFactory" />
    </bean>

    <!-- ======================================================= -->
    <!-- JMS Send, define default destination and JmsTemplate    -->
    <!-- ======================================================= -->
    <!-- Default Destination Queue Definition -->
    <bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- name of the queue -->
        <constructor-arg index="0" value="Send2Recv" />
    </bean>

    <!-- JmsTemplate Definition -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="defaultDestination" />
        <property name="deliveryPersistent" value="false"/>
        <property name="deliveryMode" value="1"/>
        <property name="timeToLive" value="10000"/>
    </bean>

    <!-- =============================================== -->
    <!-- JMS receive, define JmsListenerContainerFactory -->
    <!-- =============================================== -->
    <bean id="jmsListenerContainerFactory"
          class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="concurrency" value="3-10"/>
    </bean>

</beans>

我的制片人:
@Component
public class JmsMessageSender {

    private final Logger log = Logger.getLogger(JmsMessageSender.class.toString());

    @Autowired
    private JmsTemplate jmsTemplate;

    public void send(final Destination dest, final String text) {
        this.jmsTemplate.send(dest, session -> {
            Message message = session.createTextMessage(text);
            log.info("delivery mode {" + jmsTemplate.getDeliveryMode() + "}, timeToLive {" + jmsTemplate.getTimeToLive() + "}");
            return message;
        });
    }
}

发送时我在控制台日志中看到:

INFO: Established shared JMS Connection: ActiveMQConnection {id=ID:localhost-61129-1458640452010-1:1,clientId=null,started=false}
mar 22, 2016 10:54:12 AM com.app.JmsMessageSender lambda$send$1
INFO: delivery mode {1}, timeToLive {10000}
mar 22, 2016 10:54:12 AM com.app.JmsMessageSender lambda$send$1
INFO: delivery mode {1}, timeToLive {10000}

这意味着交付模式被设置为NONPERSISTENT。

然而,当我打开ActiveMQ WebConsole时,有一些消息被标记为持久性。

请问有人可以解释一下原因吗?如何解决这个问题?

enter image description here

1个回答

5
请查阅文档 - 您需要将explicitQosEnabled设置为true来启用QOS设置(如持久性)。
setDeliveryPersistent的javadoc中也提到了这一点。

真的帮了我很多!从文档中得知:一些JMS提供者允许通过配置ConnectionFactory在管理上设置默认的QOS值。这样做会导致对MessageProducer的send方法send(Destination destination, Message message)的调用将使用不同于JMS规范中指定的QOS默认值。为了提供一致的QOS值管理,必须明确启用JmsTemplate以使用其自己的QOS值,方法是将布尔属性isExplicitQosEnabled设置为true。 - mlewandowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接