【问题标题】:Performance issues with jms and spring integration. What is wrong with the following configuration?jms 和 spring 集成的性能问题。以下配置有什么问题?
【发布时间】:2012-11-09 19:25:22
【问题描述】:

我有一个jms生产者,每秒生成很多消息,发送到amq持久队列,由单个消费者消费,需要按顺序处理。但似乎生产者比消费者快得多,而且我遇到了性能和内存问题。消息的获取速度非常非常慢,而且消费似乎每隔一段时间就会发生(消费者以轮询方式“询问”消息,这很奇怪?!)

基本上所有事情都发生在 spring 集成中。这是生产者端的配置。第一个质押消息进入stakesInMemoryChannel,从那里,它们被过滤并抛出filteredStakesChannel,然后它们从那里进入jms队列(使用执行器,因此发送将在单独的线程中发生)

    <bean id="stakesQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg name="name" value="${jms.stakes.queue.name}" />
    </bean>

    <int:channel id="stakesInMemoryChannel" />

    <int:channel id="filteredStakesChannel" >
        <int:dispatcher task-executor="taskExecutor"/>
    </int:channel>

    <bean id="stakeFilterService" class="cayetano.games.stake.StakeFilterService"/>

    <int:filter 
       input-channel="stakesInMemoryChannel"
       output-channel="filteredStakesChannel" 
       throw-exception-on-rejection="false"
       expression="true"/>

    <jms:outbound-channel-adapter channel="filteredStakesChannel" destination="stakesQueue" delivery-persistent="true" explicit-qos-enabled="true"  />

    <task:executor id="taskExecutor" pool-size="100" />

另一个应用程序正在使用这样的消息...消息来自 jms stakesQueue 的 stakesInputChannel,之后它们被路由到 2 个单独的通道,一个保留消息,另一个做一些其他的事情,让我们称之为“处理”。

    <bean id="stakesQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg name="name" value="${jms.stakes.queue.name}" />
</bean>

<jms:message-driven-channel-adapter 
    channel="stakesInputChannel" 
    destination="stakesQueue" 
    acknowledge="auto"
    concurrent-consumers="1"
    max-concurrent-consumers="1"
    />
<int:publish-subscribe-channel id="stakesInputChannel" />
<int:channel id="persistStakesChannel" />
<int:channel id="processStakesChannel" />

<int:recipient-list-router 
        id="customRouter" 
        input-channel="stakesInputChannel" 
        timeout="3000" 
        ignore-send-failures="true" 
        apply-sequence="true"
        >
    <int:recipient channel="persistStakesChannel"/>
    <int:recipient channel="processStakesChannel"/>
</int:recipient-list-router> 


<bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy">
    <property name="queuePrefetch" value="${jms.broker.prefetch.policy}" />
</bean>

<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory">
        <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="${jms.broker.url}" />     
            <property name="prefetchPolicy" ref="prefetchPolicy" /> 
            <property name="optimizeAcknowledge" value="true" /> 
            <property name="useAsyncSend" value="true" />
        </bean>
    </property>
    <property name="sessionCacheSize" value="10"/>
    <property name="cacheProducers" value="false"/>
</bean>

【问题讨论】:

    标签: jms activemq messaging spring-integration


    【解决方案1】:

    不确定您所说的“按时间间隔(消费者以轮询方式“询问”消息,这很奇怪)”是什么意思。

    容器线程可能“看起来”像是在轮询,但实际上并非如此;它们在 AMQ 客户端中阻塞,直到消息到达或超时;当超时时,它立即返回到 AMQ 接收()。

    这个配置看起来不错;对于一个线程,消耗率将直接取决于您在路由器下游做什么。

    【讨论】:

    • 我用jmeter创建了jms计划,它直接向activemq发送消息。我发送了更多消息——每 20 毫秒 1 条——消费者应用程序正在完美地处理它们,没有任何延迟......所以,问题一定出在生产者配置中。
    【解决方案2】:

    建议使用PooledConnectionFactory。这建议与 Spring JmsTemplate 一起使用,它汇集了 Connection、Session 和 MessageProducer 实例,以便在它们不再使用后可以返回它们。

    我相信您在消费者方面看到的“间隔”行为是消费者超时。

    与 Gary Russel 所说的相反,amq.receive() 确实有效地轮询了队列。 Spring 配置隐藏了这一点,但消息基本上是在一个循环中从队列中拉出,该循环调用队列消费者的接收。队列的消费者在调用receive() 尝试获取消息之前,无法知道消息是否在队列中。

    这与主题相反,您在其中注册一个侦听器,该侦听器在消息进入时执行操作。主题是一种优雅的解决方案,因为您注册了一个处理消息的侦听器。

    有了Topic,你告诉activemq如何处理消息,有了Queue,activemq只是在你请求的时候给你消息。

    【讨论】:

    • 谢谢,我将使用池连接工厂对其进行更改...我将重复我对 Gary 说的内容...我使用 jmeter 创建了 jms 计划,它将消息直接发送到 activemq。我发送了更多消息 - 每 20 毫秒 1 条 - 消费者应用程序正在完美处理它们,没有任何延迟......所以,问题一定出在生产者配置中
    • "...确实有效地轮询队列..." 是;我的意思是 Spring 不会每隔一段时间(例如每隔几毫秒)“轮询”队列。实际上,除了超时后的快速周转外,线程一直在 amq.receive() 中等待消息到达。超时可以从默认值增加,但它是必需的,以便我们可以对其他事情做出反应,例如停止容器。
    • 啊,是的,它不会定期轮询它,它本质上相当于一个 while(true) 循环。很高兴我们在同一页上。
    猜你喜欢
    • 2015-11-01
    • 2011-09-14
    • 2012-11-06
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2020-03-04
    相关资源
    最近更新 更多