最近学习ActiveMq,昨日查看其配置文件activemq.xml的时候,被一行注释引到了http://activemq.apache.org/producer-flow-control.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.

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

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>章节会描述到。

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.

值得注意的是,当内存限制或<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.

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

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()方法的调用,直至一些消息被消费,并且代理有了可用空间。默认值如上例所述,你可能需要根据你的环境增加这些值。

转载于:https://www.cnblogs.com/JPAORM/archive/2012/05/19/2509770.html

ActiveMq生产者流量控制(Producer Flow Control)相关推荐

  1. ActiveMQ Message Cursors、Async Sends、Optimized Acknowledgement、Producer Flow Control

    概述 ActiveMQ发送持久消息的典型处理方式是:当消息的消费者准备就绪时,消息发送系统把存储的消息按批次发送给消费者,在发送完一个批次的消息后,指针的标记位置指向下一批次待发送消息的位置,进行后续 ...

  2. TCP/IP / 如何进行流量控制( flow control )?

    目录 零.答案 一.滑窗结构 二.流量控制 三.零窗口 零.答案 接收端告诉发送端,自己现在能够接收的包的数量,发送端根据该数据来调整自己频率,从而完成了 TCP 的流量控制. 一.滑窗结构 发送方滑 ...

  3. RabbitMQ学习之Flow Control

    当RabbitMQ发布消息速度快于消费速度或者系统资源不足时,RabbitMQ将降低或阻断发布消息速度,以免服务器资源饱满而宕机,可以通过rabbitmqctl和web管理页面查看连接的状态为flow ...

  4. PCIe系列专题之二:2.5 Flow Control缓存架构及信用积分

    一.故事前传 之前我们讲了对PCIe的一些基础概念作了一个宏观的介绍,了解了PCIe是一种封装分层协议(packet-based layered protocol),主要包括事务层(Transacti ...

  5. Credit-based Flow Control的前世今生

    撰文 | 乔晶.姚迟 1 OneFlow 中的流控 OneFlow 团队此前发布的<仅此一文让您掌握OneFlow框架的系统设计>介绍了 OneFlow 是通过背压机制解决流控问题的.文中 ...

  6. flow control

    PCIE每个Virtual Channel都维护一个独立的Flow Control Credit Pool.发送端要发送TLP,首先得获得Credit.Flow Control对3种TLP有效: 1. ...

  7. ubuntu18 编译maplab opencv3_catkin 时出现Flow control statements are not properly nested.

    问题 CMake Error at cmake/OpenCVCompilerOptions.cmake:21 (else): Flow control statements are not prope ...

  8. 【RoCE】Flow Control

    概览 RoCE可以实现lossless无损网络环境,在二层网络上做到可靠网络传输,从而对原本在光纤网络环境下的应用在以太网环境下提供相同的服务,而不必对应用逻辑和上层协议更改.实现无损的方法有Glob ...

  9. Weir Flow Control售予First Reserve事宜完成之后更名为Trillium Flow Technologies

    出售之后,所有产品和服务将保持不变 休斯顿--(美国商业资讯)--Trillium Flow Technologies (Trillium)即之前的Weir Flow Control (WFC),今天 ...

最新文章

  1. mysql查询表的数据大小
  2. 共享文件迁移(fileserver)——从windows server 2003到windows server 2008
  3. eclipse调试报错,无法进入类
  4. C语言学习之有一个3X4的二维数组,要求用指向元素的指针变量输出二维数组各元素的值
  5. lightgbm 数据不平衡_数据不平衡问题
  6. ES6学习 - Promise对象
  7. (转载)测试理论面试题
  8. mysql 5.6 64位解压版_MySQL 5.6 for Windows 解压缩版配置安装(win 10 64位亲测)附安装包下载链接...
  9. mysql 字符串函数
  10. 如何利用回调模式去解决问题
  11. 【论文写作】本科、硕士研究生毕业论文格式问题
  12. 拼多多332亿美金市值超网易,黄铮离目标又近了一步!
  13. 本周小结!(回溯算法系列三)
  14. WINDOWS10更改主题
  15. fritzing元件太少_fritzing传感器元件库
  16. 国产快速启动软件ALTRun推荐
  17. 计算机word文档无法工作,教您电脑word打不开怎么办
  18. uni-app 分享给好友,生成小程序二维码海报
  19. 前端实现excel数据下载功能
  20. 一个简单音乐播放器的java实现(一)

热门文章

  1. jsp内置对象*session
  2. Asp.Net的Forms验证,解决Cookie和Seesion失效时间。
  3. 当.NET遇到SYBASE
  4. [转]Django REST framework 简介与中文教程
  5. [转]python3_unboundlocalerror报错原因
  6. 期货市场技术分析01_理论基础
  7. “工业4.0”下的可视化工厂建设方案
  8. 帆软报表插件开发系列之plugin.xml
  9. java 构造函数 单例_Java中的私有构造函数和单例类 - Break易站
  10. centos系统mysql连接workbench