使用activeMQ + Spring在主题中持久订阅:javax.jms.InvalidClientIDException:客户端:已连接

时间:2014-02-06 13:33:21

标签: java spring activemq publish-subscribe

我正在尝试使用activeMQ和Spring 3.1的集成来配置具有持久订阅的主题。我的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:jms="http://www.springframework.org/schema/jms"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">

    <!-- A JMS connection factory for ActiveMQ -->
        <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
        p:brokerURL="${broker.url}" />

    <!-- A destination in ActiveMQ -->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="${broker.topic}" />
    </bean>

    <!-- A cached connection to wrap the ActiveMQ connection -->
    <bean id="cachedConnectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory"
        p:targetConnectionFactory-ref="connectionFactory" p:sessionCacheSize="10" />

    <!-- A JmsTemplate instance that uses the cached connection and destination -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate"
        p:connectionFactory-ref="cachedConnectionFactory" 
        p:defaultDestination-ref="destination" 
        p:pubSubDomain="true"/>

    <jms:listener-container 
        container-type="default" destination-type="durableTopic" concurrency="1" client-id="${broker.topic.durableId}">
        <jms:listener ref="myListener" destination="${broker.topic}"
            subscription="${broker.topic.durableId}" method="handleMessage" />
    </jms:listener-container>

</beans>

在我的JUnit测试中运行完美,我不会成为任何例外。但是,当我在我的Web应用程序(使用jetty)上尝试它时,虽然我正确地收到了消息,但我仍然遇到这个例外:

javax.jms.InvalidClientIDException: Broker: localhost - Client: subscribtionDurableName already connected from xxxx
    at org.apache.activemq.broker.region.RegionBroker.addConnection(RegionBroker.java:243) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.advisory.AdvisoryBroker.addConnection(AdvisoryBroker.java:90) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.MutableBrokerFilter.addConnection(MutableBrokerFilter.java:97) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.TransportConnection.processAddConnection(TransportConnection.java:733) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.jmx.ManagedTransportConnection.processAddConnection(ManagedTransportConnection.java:79) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.command.ConnectionInfo.visit(ConnectionInfo.java:139) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.TransportConnection.service(TransportConnection.java:292) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.TransportConnection$1.onCommand(TransportConnection.java:149) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.MutexTransport.onCommand(MutexTransport.java:50) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.WireFormatNegotiator.onCommand(WireFormatNegotiator.java:113) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.AbstractInactivityMonitor.onCommand(AbstractInactivityMonitor.java:270) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.TransportSupport.doConsume(TransportSupport.java:83) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:214) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:196) ~[activemq-core-5.7.0.jar:5.7.0]
    at java.lang.Thread.run(Unknown Source) ~[na:1.6.0_45]
12:53:54.195 [ActiveMQ InactivityMonitor ReadCheckTimer] TRACE o.a.a.t.AbstractInactivityMonitor - A receive is in progress
12:53:54.195 [ActiveMQ InactivityMonitor WriteCheckTimer] TRACE o.a.a.t.AbstractInactivityMonitor - xxxx message sent since last write check, resetting flag
12:53:54.208 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] WARN  o.s.j.l.DefaultMessageListenerContainer - Could not refresh JMS Connection for destination 'Topic' - retrying in 5000 ms. Cause: Broker: localhost - Client: subscribtionDurableName  already connected from xxx
javax.jms.InvalidClientIDException: Broker: localhost - Client: subscribtionDurableName  already connected from xxx
    at org.apache.activemq.broker.region.RegionBroker.addConnection(RegionBroker.java:243) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.advisory.AdvisoryBroker.addConnection(AdvisoryBroker.java:90) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.MutableBrokerFilter.addConnection(MutableBrokerFilter.java:97) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.TransportConnection.processAddConnection(TransportConnection.java:733) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.jmx.ManagedTransportConnection.processAddConnection(ManagedTransportConnection.java:79) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.command.ConnectionInfo.visit(ConnectionInfo.java:139) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.TransportConnection.service(TransportConnection.java:292) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.broker.TransportConnection$1.onCommand(TransportConnection.java:149) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.MutexTransport.onCommand(MutexTransport.java:50) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.WireFormatNegotiator.onCommand(WireFormatNegotiator.java:113) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.AbstractInactivityMonitor.onCommand(AbstractInactivityMonitor.java:270) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.TransportSupport.doConsume(TransportSupport.java:83) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:214) ~[activemq-core-5.7.0.jar:5.7.0]
    at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:196) ~[activemq-core-5.7.0.jar:5.7.0]
    at java.lang.Thread.run(Unknown Source) ~[na:1.6.0_45]

我试过了:

<?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:jms="http://www.springframework.org/schema/jms"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">

    <!-- A JMS connection factory for ActiveMQ -->
        <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
        p:brokerURL="${broker.url}" />

    <!-- A destination in ActiveMQ -->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="${broker.topic}" />
    </bean>

    <!-- A JmsTemplate instance that uses the cached connection and destination -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate"
        p:connectionFactory-ref="connectionFactory" 
        p:defaultDestination-ref="destination" 
        p:pubSubDomain="true"/>

    <jms:listener-container 
        container-type="default" destination-type="durableTopic" concurrency="1" client-id="${broker.topic.durableId}">
        <jms:listener ref="myListener" destination="${broker.topic}"
            subscription="${broker.topic.durableId}" method="handleMessage" />
    </jms:listener-container>

</beans>

具有相同的结果。

任何人都可以帮助我吗?感谢名单!!

1 个答案:

答案 0 :(得分:1)

问题是Web正在加载上下文两次(使用ContextLoaderListener和DispatcherSerlvet)。我更改了web.xml中的配置和问题soved:)。