我正在使用 spring-jms 3.0.5 版和 activeMQ 4.5.2 版。当部署到 Tomcat 时,一切似乎都正常工作。当部署到 WebSphere 时,似乎有四个单独的故障转移传输被实例化,并且我的 MessageListener 实现对于发布到我的主题的每条消息都会收到四次相同的消息。
这是我的配置:
<bean id="activeMQConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="failover://(tcp://server.com:12345,tcp://server2.com:12345)?randomize=false/>
</bean>
<bean id="topic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="TOPIC.ONE" />
</bean>
<bean id="jmsTemplate" class="org.sprinframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="topic" />
</bean>
<bean name="topicListener" class="com.foo.TopicListener" />
<jms:listener-container connection-factory="connectionFactory" cache="auto" destination-type="topic">
<jms:listener destination="topic" ref="topicListener" />
</jms:listener-container>
这是我的消息监听器:
import javax.jms.MessageListener;
import org.springframework.stereotype.Component;
@Component
public class TopicListener implements MessageListener {
public void onMessage(Message msg) {
log(msg);
}
}
这是我在日志中看到的(仅在 WebSphere 上)
11:59:59,764 () INFO (Thread-50) (DefaultLifecycleProcessor) Starting beans in phase 214783647
12:00:00,140 () INFO (ActiveMQ Task) (FailoverTransport) Successfully connected to tcp://server.com:12345
12:00:00,253 () INFO (ActiveMQ Task) (FailoverTransport) Successfully connected to tcp://server.com:12345
12:00:00,342 () INFO (ActiveMQ Task) (FailoverTransport) Successfully connected to tcp://server.com:12345
12:00:00,423 () INFO (ActiveMQ Task) (FailoverTransport) Successfully connected to tcp://server.com:12345
12:00:00,492 () INFO (Thread-50) (ContextLoader) Root WebApplicationContext: initialization completed in 100239 ms
然后,一旦我发布到该主题,我就会看到:
12:01:00,250 () INFO (org.springframework.jms.listener.DefaultMessageListenerContainer#0-1) (TopicListener) logging message
12:01:00,251 () INFO (org.springframework.jms.listener.DefaultMessageListenerContainer#3-1) (TopicListener) logging message
12:01:00,251 () INFO (org.springframework.jms.listener.DefaultMessageListenerContainer#2-1) (TopicListener) logging message
12:01:00,275 () INFO (org.springframework.jms.listener.DefaultMessageListenerContainer#1-1) (TopicListener) logging message
我已经看到一些迹象表明,如果我设置了,可能会出现这种行为concurrentConsumers > 1
,但据我所知,我没有。如何确保我只收到一次这些消息?
更新:
通过调试登录,我还看到:
2012-10-28 12:00:00,000 () DEBUG (Thread-50) (DefaultLifecycleProcessor) Starting bean 'org.springframework.jms.listener.DefaultMessageListenerContainer#0' of type [class org.springframework.jms.listener.DefaultMessageListenerContainer]
2012-10-28 12:00:00,011 () DEBUG (Thread-50) (DefaultLifecycleProcessor) Starting bean 'org.springframework.jms.listener.DefaultMessageListenerContainer#1' of type [class org.springframework.jms.listener.DefaultMessageListenerContainer]
2012-10-28 12:00:00,021 () DEBUG (Thread-50) (DefaultLifecycleProcessor) Starting bean 'org.springframework.jms.listener.DefaultMessageListenerContainer#2' of type [class org.springframework.jms.listener.DefaultMessageListenerContainer]
2012-10-28 12:00:00,029 () DEBUG (Thread-50) (DefaultLifecycleProcessor) Starting bean 'org.springframework.jms.listener.DefaultMessageListenerContainer#3' of type [class org.springframework.jms.listener.DefaultMessageListenerContainer]
为什么 Spring 要创建四个这样的 bean?