【问题标题】:ActiveMQ with Spring JMS - how to sent NONPERSISTENT message?带有 Spring JMS 的 ActiveMQ - 如何发送 NONPERSISTENT 消息?
【发布时间】:2016-07-09 04:09:14
【问题描述】:

我正在尝试将 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 时 - 有消息标记为 Persistent。

谁能解释一下,为什么?如何解决这个问题?

【问题讨论】:

    标签: java spring activemq spring-jms


    【解决方案1】:

    参见the documentation - 您必须通过将explicitQosEnabled 设置为true 来启用QOS 设置(例如持久性)。

    setDeliveryPersistent 的 javadoc 中也提到了这一点。

    【讨论】:

    • 这对我很有帮助!来自文档:Some JMS providers allow the setting of default QOS values administratively through the configuration of the ConnectionFactory. This has the effect that a call to MessageProducer’s send method send(Destination destination, Message message) will use different QOS default values than those specified in the JMS specification. In order to provide consistent management of QOS values, the JmsTemplate must therefore be specifically enabled to use its own QOS values by setting the boolean property isExplicitQosEnabled to true.
    猜你喜欢
    • 2021-05-18
    • 2020-12-09
    • 2014-12-24
    • 2015-02-19
    • 2017-01-10
    • 1970-01-01
    • 2014-09-27
    • 2014-02-08
    • 1970-01-01
    相关资源
    最近更新 更多