Using connection pooling

Talend ESB Mediation Developer Guide

Talend ESB
Installation and Upgrade
Design and Development
Talend ESB

When sending to an ActiveMQ broker using Camel it is recommended to use a pooled connection factory to efficiently handle pooling of JMS connections, sessions and producers. This is documented on the ActiveMQ Spring Support page.

You can grab ActiveMQ's org.apache.activemq.pool.PooledConnectionFactory with Maven:


And then setup the activemq Camel component as follows:

<bean id="jmsConnectionFactory" 
   <property name="brokerURL" value="tcp://localhost:61616" />

<bean id="pooledConnectionFactory" 
   init-method="start" destroy-method="stop"> 
   <property name="maxConnections" value="8" />
   <property name="connectionFactory" ref="jmsConnectionFactory" />

<bean id="jmsConfig" 
   <property name="connectionFactory" ref="pooledConnectionFactory"/>
   <property name="concurrentConsumers" value="10"/>

<bean id="activemq"
    <property name="configuration" ref="jmsConfig"/>
    <!-- if we are using transacted then enable CACHE_CONSUMER (if not using XA) to run faster
         see more details at:
    <property name="transacted" value="true"/>
    <property name="cacheLevelName" value="CACHE_CONSUMER" />

Notice the init and destroy methods on the pooled connection factory. This is important to ensure the connection pool is properly started and shutdown.

If you are using transactions then see more details at JMS. And remember to set cacheLevelName to CACHE_CONSUMER if you are not using XA transactions. This can dramatically improve performance.

The PooledConnectionFactory will then create a connection pool with up to 8 connections in use at the same time. Each connection can be shared by many sessions. There is an option maxActive you can use to configure the maximum number of sessions per connection; the default value is 500. From *ActiveMQ 5.7* onwards the option has been renamed to maxActiveSessionPerConnection to better reflect its purpose. Notice the concurrentConsumers is set to a higher value than maxConnections is. This is acceptable because each consumer uses a session and sessions can share the same connection. In the above example we can have 8 * 500 = 4000 active simultaneous sessions.