0

关于apache camel的消息转发效率

 2 years ago
source link: https://lichuanyang.top/posts/17762/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

关于apache camel的消息转发效率

发表于

2015-08-08 更新于 2021-09-23

阅读次数: 261 Valine: 0

公司使用activemq和camel做消息的分发,之前数据量不是很大,所以一直没怎么考虑效率问题,对camel的工作原理研究也不深。单是最近随着业务量的增加,camel的效率逐渐成了瓶颈,所以根据日志大概了解了camel的工作原理。虽然camel是被嵌入到activemq中,但在工作过程中,camel和activemq其实还是相对独立的。我们在camel中会配置一个到activemq的连接.

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

关于vm这种传输方式,参考http://activemq.apache.org/vm-transport-reference.html

看了下日志,发现这种配置下camel会有一个很严重的问题: camel每次执行转发操作时,都会新建一个到activemq的连接,之后再将其关闭。这严重拖慢了转发效率,因为事实上每次转发都可以使用同一个连接。

因此查了一下camel文档,找到了 http://camel.apache.org/activemq.html 。 里边有关于线程池的配置:

<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>

这个正好符合我们的需要。而且顺便把连接换成了多线程,可以进一步提升效率。

需要注意的是,如果使用的是activemq5.6, 这样做会导致内存泄露,我会在下一篇博客中详述。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK