Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoreconnect problem with ActiveMQ and CachingConnectionFactory

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.

like image 449
Brendan Long Avatar asked May 06 '11 20:05

Brendan Long


People also ask

What is CachingConnectionFactory?

CachingConnectionFactory – extends the functionality of the SingleConnectionFactory and adds enhances it with a caching of Sessions, MessageProducers, and MessageConsumers.

What is pooled connection factory?

PooledConnectionFactory exposes a number of configuration options to control how the Connection instances it creates behave. By default, PooledConnectionFactory creates a single Connection instance and returns that same instance every time the createConnection() methods are called.


2 Answers

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.

like image 172
Brendan Long Avatar answered Oct 04 '22 22:10

Brendan Long


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.

like image 38
Ben ODay Avatar answered Oct 04 '22 21:10

Ben ODay