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.
CachingConnectionFactory – extends the functionality of the SingleConnectionFactory and adds enhances it with a caching of Sessions, MessageProducers, and MessageConsumers.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With