简体   繁体   English

带有Spring JMS的ActiveMQ - 如何发送NONPERSISTENT消息?

[英]ActiveMQ with Spring JMS - how to sent NONPERSISTENT message?

I am trying to explicitly set deliveryMode to NONPERSISTENT and send it to ActiveMQ. 我正在尝试将deliveryMode显式设置为NONPERSISTENT并将其发送到ActiveMQ。

Here is my Spring JMS configuration: 这是我的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>

And my producer: 而我的制片人:

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

While sending I see in my console log: 发送时我在控制台日志中看到:

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}

which means delivery mode is set as NONPERSISTENT. 这意味着交付模式设置为NONPERSISTENT。 However, when I open ActiveMQ WebConsole - there are messages marked as Persistent. 但是,当我打开ActiveMQ WebConsole时 - 会有标记为Persistent的消息。

Can someone explain me, why? 有人可以解释一下,为什么? How to fix this? 如何解决这个问题?

在此输入图像描述

See the documentation - you have to enable QOS settings (such as persistence) by setting explicitQosEnabled to true. 请参阅文档 - 您必须通过将explicitQosEnabled设置为true来启用QOS设置(例如持久性)。

This is also mentioned in the javadoc for setDeliveryPersistent . setDeliveryPersistent的javadoc中也提到了这setDeliveryPersistent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM