Spring 使用主题,但不是所有的消费者都接受信息
我想创建一个发送者来生成消息并将其发送给所有消费者。 我使用的是topic,但有些地方不对劲,例如,如果我有3个消费者,只有一个会以随机方式接收消息。 我不知道什么是wrog。这是我的服务器配置Spring 使用主题,但不是所有的消费者都接受信息,spring,jms,activemq,spring-jms,jmstemplate,Spring,Jms,Activemq,Spring Jms,Jmstemplate,我想创建一个发送者来生成消息并将其发送给所有消费者。 我使用的是topic,但有些地方不对劲,例如,如果我有3个消费者,只有一个会以随机方式接收消息。 我不知道什么是wrog。这是我的服务器配置 <amq:broker brokerName="granicaBroker" id="broker" persistent="false" deleteAllMessagesOnStartup="true" enableStatistics="false" useL
<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" />
我的客户端上下文配置:
目的地必须是主题而不是队列;使用
ActiveMQTopic
而不是ActiveMQQueue
I更改jms:listener容器
部分
代码如下:
<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>
而且它有效 我更改了ActiveMQTopic,因为这很有意义,但是现在客户端没有重新接收消息在发布消息之前,您是否先启动消费者?对于主题,消息仅在发布时发送给活动使用者,除非订阅是持久的(这不是默认值)。我在tomcat中使用代理,它首先运行,然后启动客户端。您应该能够使用activemq web控制台对此进行调试,在这里你可以看到消费者,但我没有使用activemq的二进制发行版,而是一个带有嵌入式代理的war项目。
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>
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());
}
}
}
<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>