Pooling ActiveMQ Connections for Camel

September 30, 2012

In my previous camel.xml I used the following XML to set up the connection to ActiveMQ:

    <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
      <property name="connectionFactory">
	<bean class="org.apache.activemq.ActiveMQConnectionFactory">
	  <property name="brokerURL" value="vm://zuu:61613?create=false&amp;waitForStart=10000" />
	</bean>
      </property>
    </bean>

While this works, every time a message is sent Camel opens a new connection to the broker. I know I’m going to be sending a lot of messages, and I’d rather not waste time opening and closing connections for each one. A connection pool is the obvious fix.

By wrapping the ActiveMQConnectionFactory in a PooledConnectionFactory, I can maintain a pool of up to 8 connections that stay open and get returned to the pool (rather than closed) after each message is sent:

    <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
      <property name="connectionFactory">
	<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
	  <property name="maxConnections" value="8" />
	  <property name="connectionFactory">
	    <bean class="org.apache.activemq.ActiveMQConnectionFactory">
	      <property name="brokerURL" value="vm://zuu:61613?create=false&amp;waitForStart=10000" />
	    </bean>
	  </property>
	</bean>
      </property>
    </bean>

A small change, but it makes a real difference under load.

Questions or thoughts? Get in touch.