ActiveMq生产者流量控制(Producer Flow Control)_驯咆贸祷_新浪博客
在ActiveMQ5.0版本中,我们可以分别对一个共享连接上的各个生产者进行流量控制,而不需要挂起整个连接。“流量控制”意味着当代理(broker)检测到目标(destination)的内存,或temp-或file-store超过了限制,消息的流量可以被减慢。生产者将会被阻塞直至资源可用,或者收到一个JMSException异常:这种行为是可配置的,下面的<systemUsage>章节会描述到。
It's worth noting that the default
值得注意的是,当内存限制或<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
如果你喜欢,你可以通过在代理的配置中,将适当的目的地(destination)的策略(policy)中的producerFlowControl标志设置为false,使代理上特定的JMS队列和主题无效,例如:
<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic="FOO.>" producerFlowControl="false"/></policyEntries> </policyMap></destinationPolicy>
see
详情请看
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
注意,自从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
如果你发送一条持久化消息(这样就会有一个对
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
所以,一个友好的生产者可以再发送更多的数据之前,等待生产者应答,以此来避免对代理的冲击(并且如果出现了一个比较慢的消费者,强制代理阻塞整个连接)。如果你想知道这部分的源代码是怎么实现的,可以看一下
Though a client can ignore the producer ACKs altogether and the broker should just stall thetransport 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()操作的一种替代方案,就是将其配置成客户端抛出的一个异常。通过将sendFailIfNoSpace属性设置为true,代理将会引起send()方法失败,并抛出javax.jms.ResourceAllocationExcept
<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.ResourceAllocationExcept
Starting in version 5.3.1 the
<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()方法失败并对客户端抛出异常之前,等待三秒。这个属性的优点是,它仅仅阻塞配置指定的时间,而不是立即另发送失败,或者无限期阻塞。这个属性不仅在代理端提供了一个改进,还对客户端提供了一个改进,使得客户端能捕获异常,等待一下并重试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
一个普遍的需求是使流量控制无效,使得消息分发能够持续,直到所有可用的磁盘被挂起(pending)的消息耗尽(无论是持久化的还是配置了非持久化的)。要这样做,你可以使用消息游标(Message Cursors)。
System usage 系统占用
You can also slow down producers via some attributes on the
你还可以通过<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