ActiveMQ 的 prefetch-limit
1.官方文档
http://activemq.apache.org/what-is-the-prefetch-limit-for.html
One of the aims of ActiveMQ is to be a high performance message bus. This means
using a SEDA(http://activemq.apache.org/seda.html) architecture to perform as
much work as possible asynchronously. To be able to achieve high performance
it is important to stream messages to consumers as fast as possible so
that the consumer always has a buffer of messages, in RAM, ready to
process - rather than have them explicitly pull messages from the server
which adds significant latency per message.
ActiveMQ的一个目标是成为高性能消息总线.这就是说,ActiveMQ采用SEDA结构以异步方式尽可能
高效的处理更多工作.为了获取高性能,ActiveMQ必须尽可能快的将消息传送给消费者以便消费者
可以使用RAM消息缓存,而不是让消费者主动从代理服务器一个一个地拉取消息,因为这样会为每个
消息增加大量的延迟.
There is a danger however that this aggressive pushing of messages to the
consumers could flood a consumer as typically its much faster to deliver
messages to the consumer than it often is to actually process them.
这种侵入式的推送消息给消费者的行为是危险的,因为可能导致消费者被大量消息淹没.毕竟,通常
发送消息给消费者要比消费者处理消息快的多.
So ActiveMQ uses a prefetch limit on how many messages can be streamed to a
consumer at any point in time. Once the prefetch limit is reached, no more
messages are dispatched to the consumer until the consumer starts sending back
acknowledgements of messages (to indicate that the message has been processed).
The actual prefetch limit value can be specified on a per consumer basis.
因此,ActiveMQ使用prefetch limit限制任何时候消息消费者已接受到但尚未完成处理的消息数量.
一旦消息数量达到prefetch limit限制,则在消费者开始发送消息确认(指示消息已经被处理了)之前
不会再发送任何消息给消费者.可以为每一个消息消费者单独设置prefetch limit 值.
Its a good idea to have large values of the prefetch limit if you want high
performance and if you have high message volumes. If you have very few messages
and each message takes a very long time to process you might want to set the
prefetch value to 1 so that a consumer is given one message at a time.
Specifying a prefetch limit of zero means the consumer will poll for more
messages, one at a time, instead of the message being pushed to the consumer.
如果你打算高效的发送大量消息,那么为prefetch limit设置一个比较大的值是一个不错的注意.
如果你只打算发送很少的消息,并且每一个消息的处理时间很长,那么可以设置prefetch limit值
为1,这样消息者每次只会接收到一个消息.设置prefetch limit 值为0表示,消费者每次主动拉取更
多消息,而不是由代理推送消息到消费者.
Specifying the PrefetchPolicy
配置 prefetch limit
You can specify an instance of the ActiveMQPrefetchPolicy on an
ActiveMQConnectionFactory or ActiveMQConnection. This allows you to configure
all the individual prefetch values; as each different quality of service has a
different value. e.g.
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)
It can also be configured on the connection URI used when establishing a connection the broker:
To change the prefetch size for all consumer types you would use a connection URI similar to:
tcp://localhost:61616?jms.prefetchPolicy.all=50
To change the prefetch size for just queue consumer types you would use a connection URI similar to:
tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=1
It can also be configured on a per consumer basis using Destination Options.
queue = new ActiveMQQueue("TEST.QUEUE?consumer.prefetchSize=10");
consumer = session.createConsumer(queue);