ActiveMq生产者流量控制(Producer Flow Control)

最近学习ActiveMq,昨日查看其配置文件activemq.xml的时候,被一行注释引到了http://activemq.apache.org/producer-flow-control.html页面,感受挺有用,遂翻译之保存成文。做为本身的第一篇译做,必有诸多不妥之处,故附上原文做对照,欢迎拍砖。html

 

Producer Flow Control生产者流量控制

In ActiveMQ 4.x flow control was implemented using TCP flow control. The underlying network connection of throttled consumers was suspended to enforce flow control limits. This strategy is very efficient but can lead to deadlocks if there are multiple producers and consumers sharing the same connection.java

在ActiveMQ 4.x版本中,流量控制是用TCP流量控制实现的。受节制的消费者底层的网络链接被挂起,以强制进行流量控制限制。这个策略很是高效,可是若是有多个生产者和消费者共享同一个链接的时候,可能会致使死锁。apache

As of ActiveMQ 5.0, we can now individually flow control each producer on a shared connection without having to suspend the entire connection. By 'flow control' we mean that if the broker detects that the memory limit for the destination, or the temp- or file-store limits for the broker, have been exceeded, then the flow of messages can be slowed down. The producer will be either blocked until resources are available or will receive a JMSException: this behaviour is configurable and described in the section below on <systemUsage>.网络

 在ActiveMQ5.0版本中,咱们能够分别对一个共享链接上的各个生产者进行流量控制,而不须要挂起整个链接。“流量控制”意味着当代理(broker)检测到目标(destination)的内存,或temp-或file-store超过了限制,消息的流量能够被减慢。生产者将会被阻塞直至资源可用,或者收到一个JMSException异常:这种行为是可配置的,下面的<systemUsage>章节会描述到。app

It's worth noting that the default <systemUsage> settings will cause the producer to block when the memoryLimit or <systemUsage> limits are reached: this blocking behaviour is sometimes misinterpreted as a 'hung producer', when in fact the producer is simply diligently waiting until space is available.less

值得注意的是,当内存限制或<systemUsage>限制达到的时候,<systemUsage>默认的设置会引发生产值阻塞:这种阻塞行为有时会被误解为“挂起的生产者”,而事实是生产者只是勤奋地等着空间可用。异步

  • Messages that are sent synchronously will automatically use per producer flow control; this applies generally to persistent messages which are sent synchronously unless you enable the useAsyncSend flag.
  • 同步发送的消息将会自动对每个生产者使用流量控制;除非你使能了useAsyncSend标志,不然这将对同步发送的持久化消息都适用。
  • Producers that use Async Sends - generally speaking, producers of non-persistent messages - don't bother waiting for any acknowledgement from the broker; so, if a memory limit has been exceeded, you will not get notfied. If you do want to be aware of broker limits being exceeded, you will need to configure the ProducerWindowSize connection option so that even async messages are flow controlled per producer.
  • 使用异步发送的生产者 —— 通常来讲,就是发送非持久化消息的生产者 —— 不须要等候来自代理的任何确认消息;因此,若是内存限制被超过了,你不会被通知。若是你真的想何时代理的限制被超过了,你须要配置ProducerWindowSize这一链接选项,这样就算是异步消息也会对每个生产者进行流量控制。
ActiveMQConnectionFactory connctionFactory = ...connctionFactory.setProducerWindowSize(1024000);

The ProducerWindowSize is the maximum number of bytes of data that a producer will transmit to a broker before waiting for acknowledgment messages from the broker that it has accepted the previously sent messages.async

ProducerWindowSize是一个生产者在等到确认消息以前,能够发送给代理的数据的最大byte数,这个确认消息用来告诉生产者,代理已经收到先前发送的消息了。ide

Alternatively, if you're sending non-persisted messages (which are by default sent async), and want to be informed if the queue or topic's memory limit has been breached, then you can simply configure the connection factory to 'alwaysSyncSend'. While this is going to be slower, it will ensure that your message producer is informed immediately of memory issues.学习

或者,若是你要发送非持久化的消息(该消息默认是异步发送的),而且想要获得队列或者主题的内存限制是否达到,你只需将链接工厂配置为“alwaysSyncSend”。虽然这样会变得稍微慢一点,可是这将保证当出现内存问题时,你的消息生产者可以及时获得通知。

If you like, you can disable flow control for specific JMS queues and topics on the broker by setting the producerFlowControl flag to false on the appropriate destination policy in the Broker configuration - e.g.

若是你喜欢,你能够经过在代理的配置中,将适当的目的地(destination)的策略(policy)中的producerFlowControl标志设置为false,使代理上特定的JMS队列和主题无效,例如:

<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic="FOO.>" producerFlowControl="false"/></policyEntries> </policyMap></destinationPolicy>

see Broker Configuration.

详情请看 代理设置 部分。

Note that, since the introduction of the new file cursor in ActiveMQ 5.x, non-persisted messages are shunted into the temporary file store to reduce the amount of memory used for non-persistent messaging. As a result, you may find that a queue's memoryLimit is never reached, as the cursor doesn't use very much memory. If you really do want to keep all your non-persistent messages in memory, and stop producers when the limit is reached, you should configure the <vmQueueCursor>.

注意,自从ActiveMQ 5.x中引入新的文件游标以后,非持久化消息被分流到了临时文件存储中,以此来减小非持久化消息传送使用的内存总量。结果就是,你可能会发现一个队列的内存限制永远达不到,由于游标不须要使用太多的内存。若是你真的想把全部的非持久化消息存放在内存中,并在达到内存限制的时候停掉生产者,你须要配置<vmQueueCursor>。

<policyEntry queue=">" producerFlowControl="true" memoryLimit="1mb"> <pendingQueuePolicy> <vmQueueCursor/></pendingQueuePolicy></policyEntry>

The fragment above will ensure that all non-persistent queue messages are kept in memory, with each queue having a limit of 1Mb.

上面的片断能够保证全部的非持久化队列消息都保存在内存中,每个队列的内存限制为1Mb。

How Producer Flow Control works 生产者流量控制是如何工做的

If you are sending a persistent message (so that a response of the OpenWire Message is expected then the broker will send the producer aProducerAck message. This informs the producer that the previous sending window has been processed, so that it can now send another window. Its kinda like consumer acks but in reverse.

若是你发送一条持久化消息(这样就会有一个对 OpenWire 响应消息的指望),代理将会给生产者发送一个应答消息。该消息会通知生产者其先前的发送窗口已经被处理了,因此它如今能够发送另外的窗口了。这有点像消费者应答,不过是反向的。

Advantage 优点

So a nice producer might wait for a producer ack before sending more data, to avoid flooding the broker (and forcing the broker to block the entire connection if a slow consumer occurs). To see how this works in source code, check out the ActiveMQMessageProducer code.

因此,一个友好的生产者能够再发送更多的数据以前,等待生产者应答,以此来避免对代理的冲击(而且若是出现了一个比较慢的消费者,强制代理阻塞整个链接)。若是你想知道这部分的源代码是怎么实现的,能够看一下 ActiveMQMessageProducer 的代码。

Though a client can ignore the producer ACKs altogether and the broker should just stall the transport if it has to for slow consumer handling; though this does mean it'll stall the entire connection.

然而一个客户端能够彻底忽略生产者的应答消息,而且处理慢消费者的时候,代理能够在须要的时候拖延传送;虽然这意味着它将拖延整个链接。

Configure Client-Side Exceptions 配置客户端的异常

An alternative to the indefinite blocking of the send() operation when no space is free on the broker is to instead configure that an exception to be thrown on the client-side. By configuring the sendFailIfNoSpace property to true, the broker will cause the send() operation to fail with ajavax.jms.ResourceAllocationException, which will propagate to the client. Below is an example of this configuration:

应对代理空间不足,而致使不肯定的阻塞 send()操做的一种替代方案,就是将其配置成客户端抛出的一个异常。经过将sendFailIfNoSpace属性设置为true,代理将会引发send()方法失败,并抛出javax.jms.ResourceAllocationException异常,传播到客户端。下面是一个配置的示例:

<systemUsage> <systemUsage sendFailIfNoSpace="true"> <memoryUsage> <memoryUsage limit="20 mb"/> </memoryUsage> </systemUsage></systemUsage>

The advantage of this property is that the client can catch the javax.jms.ResourceAllocationException, wait a bit and retry the send() operation instead of just hanging indefinitely.

这个属性的好处是,客户端能够捕获javax.jms.ResourceAllocationException异常,稍等一下,并重试send()操做,而不是无限期地傻等下去。

Starting in version 5.3.1 the sendFailIfNoSpaceAfterTimeout property has been added. This property causes the send() operation to fail with an exception on the client-side, but only after waiting the given amount of time. If space on the broker is still not freed after the configured amount of time, only then does the send() operation fail with an exception to the client-side. Below is an example:

 从5.3.1版本以后,sendFailIfNoSpaceAfterTimeout 属性被加了进来。这个属性一样致使send()方法失败,并在客户端抛出异常,但仅当等待了指定时间以后才触发。若是在配置的等待时间过去以后,代理上的空间仍然没有被释放,仅当这个时候send()方法才会失败,而且在客户端抛出异常。下面是一个示例:

<systemUsage> <systemUsage sendFailIfNoSpaceAfterTimeout="3000"> <memoryUsage> <memoryUsage limit="20 mb"/> </memoryUsage> </systemUsage></systemUsage>

The timeout is defined in milliseconds so the example above waits for three seconds before failing the send() operation with an exception to the client-side. The advantage of this property is that it will block for the configured amount of time instead of failing immediately or blocking indefinitely. This property offers not only an improvement on the broker-side, but also an improvement for the client so it can catch the exception, wait a bit and retry the send() operation.

定义超时的单位是毫秒,因此上面的例子将会在使send()方法失败并对客户端抛出异常以前,等待三秒。这个属性的优势是,它仅仅阻塞配置指定的时间,而不是当即另发送失败,或者无限期阻塞。这个属性不只在代理端提供了一个改进,还对客户端提供了一个改进,使得客户端能捕获异常,等待一下并重试send()操做。

Disabling Flow Control 使流量控制无效

A common requirement is to disable flow control so that message dispatching continues until all available disk is used up by pending messages (whether persistent or non persistent messaging is configured). To do this enable Message Cursors.

一个广泛的需求是使流量控制无效,使得消息分发可以持续,直到全部可用的磁盘被挂起(pending)的消息耗尽(不管是持久化的仍是配置了非持久化的)。要这样作,你可使用消息游标(Message Cursors)。

System usage 系统占用

You can also slow down producers via some attributes on the <systemUsage> element. Take a look at the following example:

你还能够经过<systemUsage>元素的一些属性来减慢生产者。来看一眼下面的例子:

<systemUsage> <systemUsage> <memoryUsage> <memoryUsage limit="64 mb" /> </memoryUsage> <storeUsage> <storeUsage limit="100 gb" /> </storeUsage> <tempUsage> <tempUsage limit="10 gb" /> </tempUsage> </systemUsage></systemUsage>

You can set limits of memory for NON_PERSISTENT messages, disk storage for PERSITENT messages and total usage for temporary messages, the broker will use before it slowdown producers. Using the default settings shown above, the broker will block the send() call until some messages are consumed and space becomes available on the broker. The default values are shown above, you will probably need to increase these values for your environment.

你能够为非持久化的消息(NON_PERSISTENT messages)设置内存限制,为持久化消息(PERSISTENT messages)设置磁盘空间,以及为临时消息设置总的空间,代理将在减慢生产者以前使用这些空间。使用上述的默认设置,代理将会一直阻塞sen()方法的调用,直至一些消息被消费,而且代理有了可用空间。默认值如上例所述,你可能须要根据你的环境增长这些值。

相关文章
相关标签/搜索