Autoreconnect problem with ActiveMQ and CachingCon

2019-04-08 23:11发布

问题:

I'm having a problem with ActiveMQ and Spring's CachingConnectionFactory. I'm setting them up like this:

<!-- A connection to ActiveMQ --> 
<bean id="myConnectionFactory" 
    class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="${jms.url}"/>
    <property name="userName" value="${jms.username}"/>
    <property name="password" value="${jms.password}"/>
</bean>

<!-- A cached connection to wrap the ActiveMQ connection --> 
<bean id="myCachedConnectionFactory" 
    class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory" ref="myConnectionFactory"/>
    <property name="sessionCacheSize" value="10"/>
    <property name="reconnectOnException" value="true"/>
</bean>

<!-- A destination in ActiveMQ --> 
<bean id="myDestination" 
    class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg value="${jms.queue}" />
</bean>

<!-- A JmsTemplate instance that uses the cached connection and destination --> 
<bean id="myProducerTemplate" 
    class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="myCachedConnectionFactory"/>
    <property name="defaultDestination" ref="myDestination"/>
</bean>

jms.url is using the failover transport:

failover:(tcp://firstbox:6166,tcp://secondbox:6166)?timeout=3000

The problem I'm having is that if one box goes down, we should start sending messages on the other, but it seems to still be using the old connection (every send times out). If I restart the program, it'll connect again and everything works.

My understanding is that the ActiveMQConnectionFactory should fix itself (reconnect to a new box), and the JmsTemplate should be requesting a new connection every time, so that should be ok. I'm wondering if the CachingConnectionFactory might be doing something bad (caching a producer that talks to the old server?).

Am I missing something I need to do here? My setup seems fairly normal, but I can't find anyone else having this problem.

回答1:

The problem I was having is that ActiveMQ wasn't telling the CachingConnectionFactory when it reconnected, so the cached connection was still being used. I replaced it with ActiveMQ's PooledConnectionFactory and the problem went away.



回答2:

FYI, I just tested this scenario (using CachingConnectionFactory for both producer/consumer connections) between two local AMQ brokers and the failover worked fine...

that being said...I'm seeing other Consumer connection issues when using a polling consumer pattern...must need to manually close connections or something.