【问题标题】:Using topics but not all the consumers take the message使用主题但并非所有消费者都接受消息
【发布时间】:2015-03-29 05:49:09
【问题描述】:

我想创建一个发送者来生成消息并将其发送给所有消费者。 我正在使用主题,但是出了点问题,例如,如果我有 3 个消费者,则只有一个以随机方式接收消息。 我不知道wrog是什么。这是我的服务器配置

<amq:broker brokerName="granicaBroker" id="broker"
        persistent="false" deleteAllMessagesOnStartup="true" enableStatistics="false"
        useLoggingForShutdownErrors="true">
        <amq:networkConnectors>
            <amq:networkConnector name="linkToBrokerB"
                uri="static:(tcp://xxx.xx.xxx.xx:61617)" networkTTL="3" duplex="true" />
        </amq:networkConnectors>
        <amq:transportConnectors>
            <amq:transportConnector
                uri="nio://xxx.xx.xxx.xx:61616?jms.useAsyncSend=true?jms.useCompression=true"
                disableAsyncDispatch="false" />
        </amq:transportConnectors>
    </amq:broker>


    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="JMS.TOPIC.NOTIFICATION" />
    </bean>

    <bean id="producerTemplate" class="org.springframework.jms.core.JmsTemplate"
        p:connectionFactory-ref="connectionFactory"
        p:defaultDestination-ref="destination" />

    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
        p:brokerURL="nio://xxx.xx.xxx.xx:61616" />

还有我的生产者类(只是发送消息的部分)

    @Autowired
    protected JmsTemplate jmsTemplate;

    final String text = applicationEvent.getMsg();

        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage(text);
                    return message;
            }
        });

我的客户端上下文配置:

    p:brokerURL="nio://xxx.xx.xxx.xx:61616" />

<bean id="simpleMessageListener" class="notifications.NotifierControllerImpl"/>
    <jms:listener-container container-type="default"
        connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="JMS.TOPIC.NOTIFICATION" ref="simpleMessageListener"
            method="onMessage" />
    </jms:listener-container>

还有java客户端类

public class NotifierControllerImpl implements MessageListener{
    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof TextMessage) {
                TextMessage tm = (TextMessage)message;
                System.out.println(tm.getText());
            }
        } catch (JMSException e) {
            System.out.println(e.toString());
        }
    }
}

【问题讨论】:

    标签: spring jms activemq spring-jms jmstemplate


    【解决方案1】:

    我改变jms:listener-container部分

    代码如下:

    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
            <property name="connectionFactory" ref="connectionFactory"/>
            <property name="destination" ref="destination"/>
            <property name="messageListener" ref="simpleMessageListener" />
        </bean>
    

    而且它有效!

    【讨论】:

      【解决方案2】:

      目的地需要是主题而不是队列;使用ActiveMQTopic 而不是ActiveMQQueue

      【讨论】:

      • 我更改为 ActiveMQTopic 因为它有意义,但现在客户端没有重新接收消息
      • 您是在发布消息之前先启动消费者吗?对于主题,消息仅在发布时发送给活跃的消费者,除非订阅是持久的(这不是默认的)。
      • 我在 tomcat 中使用代理,它首先运行,然后,我启动客户端
      • 您应该能够使用 activemq Web 控制台进行调试,您可以在其中看到消费者。
      • 但是我没有使用activemq的二进制分发,是一个带有嵌入式broker的war项目。
      猜你喜欢
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 2016-01-07
      • 2020-06-28
      相关资源
      最近更新 更多