The company uses ActiveMQ and Camel for message distribution. Previously, the data volume wasn’t very large, so we never really considered efficiency issues, and the research into Camel’s working principles wasn’t deep. However, recently, as the business volume increased, Camel’s efficiency has gradually become a bottleneck, so I got a general understanding of Camel’s working principles based on logs. Although Camel is embedded into ActiveMQ, during the working process, Camel and ActiveMQ are actually relatively independent. We configure a connection to ActiveMQ in Camel.

http://camel.apache.org/activemq.html

Regarding the VM transport method, refer to http://activemq.apache.org/vm-transport-reference.html

After checking the logs, I found that with this configuration, Camel has a very serious problem: every time Camel performs a forwarding operation, it creates a new connection to ActiveMQ and then closes it. This severely slows down the forwarding efficiency, since in fact, the same connection could be reused for every forwarding operation.

So I looked up the Camel documentation and found http://camel.apache.org/activemq.html. It contains configuration for thread pools:

 

<pre name="code" class="html"><bean id="jmsConnectionFactory" 
   class="org.apache.activemq.ActiveMQConnectionFactory">
   <property name="brokerURL" value="tcp://localhost:61616" />
</bean>

<bean id="pooledConnectionFactory" 
   class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
   <property name="maxConnections" value="8" />
   <property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>

<bean id="jmsConfig" 
   class="org.apache.camel.component.jms.JmsConfiguration">
   <property name="connectionFactory" ref="pooledConnectionFactory"/>
   <property name="concurrentConsumers" value="10"/>
</bean>

<bean id="activemq" 
    class="org.apache.activemq.camel.component.ActiveMQComponent">
    <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: http://camel.apache.org/jms
    <property name="transacted" value="true"/>
    <property name="cacheLevelName" value="CACHE_CONSUMER" />
    -->
</bean>

This fits our needs exactly. And by switching the connection to multi-threaded, we can further improve efficiency.

 

It’s worth noting that if you’re using ActiveMQ 5.6, doing this will cause a memory leak. I will elaborate on this in the next blog post.

 

Source: https://lichuanyang.top/en/posts/17762/