ActiveMQ-Prefetch机制和constantPendingMessageLimitStrategy

首先简要介绍一下prefetch机制。ActiveMQ通过prefetch机制来提高性能,这意味这 客户端的内存里可能会缓存一定数量的消息。缓存消息的数量由prefetch limit来控 制。当某个consumer的prefetch buffer已经达到上限,那么broker不会再向consumer 分发消息,直到consumer向broker发送消息的确认。可以通过在 ActiveMQConnectionFactory或者ActiveMQConnection上设置 ActiveMQPrefetchPolicy对象来配置prefetch policy。也可以通过connection options或者destination options来配置。例如: 

1

2

3

4

5

6

7

8

9

10

11

tcp://localhost:61616?jms.prefetchPolicy.all=50

 

tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=1

 

queue = new ActiveMQQueue("TEST.QUEUE?consumer.prefetchSize=10");

 

prefetch size 的缺省值如下:

• persistent queues (default value: 1000)

• non-persistent queues (default value: 1000)

• persistent topics (default value: 100)

• non-persistent topics (default value: Short.MAX_VALUE -1)

慢消费者会在非持久的 topics 和 queue上导致问题:一旦消息积压起来,会导致 broker 把大量消息保存在内存中,broker 也会因此而变慢。ActiveMQ采用一定的缓存策略来进行控制,例如

1

2

3

4

5

6

7

<policyEntry topic="PRICES.>"

    <!-- lets force old messages to be discarded for slow consumers --> 

    <pendingMessageLimitStrategy> 

        <constantPendingMessageLimitStrategy limit="50"/>                           // constantPendingMessageLimitStrategy 限制内存中只保留50条最新消息,其余将会被剔除或保存在temp store中

    </pendingMessageLimitStrategy> 

</policyEntry> 

<prefetchRatePendingMessageLimitStrategy multiplier="2.5"/>                  // prefetchRatePendingMessageLimitStrategy是倍数策略,如果prefetchSize为100,则保留2.5 * 100条消息

  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值