Message Cursors 消息游标
A common problem in previous versions of ActiveMQ wasrunning out of RAM bufferwhen using non-persistent messaging.
在之前版本的ActiveMQ版本中,一个普遍的问题是使用非持久化消息传送时出现RAM缓冲耗尽的情况。
Beginning with ActiveMQ 5.0.0, there is a new memory model that allowsmessages to be paged in from storage when space is available (using Storecursors for persistent messages).
从ActiveMQ 5.0.0版本开始,出现了一种新的内存模式,在空间可用时允许消息从存储设备中以页为单位进入(对持久化消息使用存储游标)。
Releases prior to 5.0 kept references in memory for all the messages thatcould be dispatched to an active Durable Topic Consumer or a Queue. While areference itself is not large, it does impose a limit on the maximum number ofmessages that can be pending delivery.
5.0之前的版本对于可以分发到一个有效的持久性主题消费者或者队列的所有消息,都在内存中保留其引用。虽然一个引用本身并不大,它仍然占用了能够挂起等待发送的消息数的一点限度。
A typical approach for messaging systems dispatching persistent messagesis to pull them in batches from long term storage when a client is ready toconsume them, using a cursor to maintain the next to dispatch position. This isa robust and very scalable approach, but not the most performant for cases whenthe consumer(s) can keep up with the producer(s) of messages.
消息系统分发持久化消息的一个典型的途径是,当客户端准备好消费消息的时候,把它们成批的从长期存储设备中拉取出来,使用一个游标来维护下一次要分发的位置。这是一种健壮并且很有伸缩性的方法,但是在消费者(们)与生产者(们)步调一致的情况下,这不是最高效的方法。
ActiveMQ 5.0 takes a hybrid approach, allowing messages to pass fromproducer to consumer directly (after the messages have been persisted), butswitches back to using cursors if the consumer(s) fall behind.
ActiveMQ 5.0使用一种混合的方法,允许消息直接从生产者传递到消费者(当消息已经被持久化了之后),但是当消费者(们)落后之后,切换回使用游标的模式。
When Message Consumers are both active and fast – keeping up with theMessage Producer(s) – messages are stored and then passed to a dispatch queuein the broker associated with the Consumer:
当消息消费者既活跃,又快速的时候 ——紧随消息生产者的步调—— 消息被存储之后就被传递到代理上跟消费者相关联的分发队列里:
If a Consumer becomes active after messages are pending from the store for it,or it’s slower than the producer, then messages are paged in to the dispatchqueue from a pending cursor:
如果一个消费者在消息已经从存储中挂起之后变的活跃了, 或者它比生产者慢,消息将会以页为单位,从一个挂起的游标(pending cursor)进入分发队列:
Types of Cursor 游标的类型
The default message cursor type in ActiveMQ 5.0is Store based.It behaves as above. There are twoadditionaltypes of cursorthat could be used:VM CursorandFile based Cursor,described below.
ActiveMQ 5.0默认的消息游标类型是基于存储的。其工作方式如上所述。还有另外两种游标类型可用:虚拟内存(VM)游标和基于文件的游标,如下文所述。
VM Cursor 虚拟内存游标
The VM Cursor is how ActiveMQ 4.x works: references to a message are heldin memory, and passed to the dispatch queue when needed. This can be very fast,but also has the downside of not being able to handle very slow consumers orconsumers that have been inactive for a long time:
虚拟内存游标是ActiveMQ 4.x的工作方式:消息的引用保存在内存中,当需要的时候被传递到分发队列中。这样做速度很快,但也有缺点,即不能处理非常慢的消费者,或者很长时间不活跃的消费者:
File based Cursor 基于文件的游标
The File based Cursor is derived from the VM Cursor. When memory inthe broker reaches its limit, it can page messages to temporary files on disk.This type of cursor can be used when the message store might be relativelyslow, but consumers are generally fast. By buffering to disk, it allows themessage broker to handle message bursts from producers without resorting topaging in from slow storage:
基于文件的游标是从虚拟内存游标衍生而来的。当代理的内存达到限度之后,它可以将消息写入磁盘上的临时文件。这种类型的游标适用的场景是,消息存储相对要慢,但是消费者要快一些。通过在磁盘上做缓冲,消息代理可以在应对消息爆发的时候,不需要从慢存储中读取。
Paging forNon-Persistent Messages 非持久化消息分页
The store based cursor also handles cursors for non-persistent messages,which are not stored in the message store. Non-persistent messages are passeddirectly to the cursor, so the store based cursor embeds a file based cursorjust for these types of messages:
基于存储的游标也可以处理非持久化消息的游标,这些消息是保存在消息存储中的。非持久化消息直接被传递给游标,所以针对这些消息,基于存储的游标嵌入了一个基于文件的游标:
ConfiguringCursors 配置游标
By default, Store based cursors are used, but it is possible to configuredifferent cursors depending on the destination.
默认情况下使用的是基于存储的游标,但是可以根据目的配置不同的游标。
Topic subscribers 主题订阅者
For Topics there is a dispatch queue and pending cursor for everysubscriber. It’s possible to configure different policies for durablesubscribers and transient subscribers – e.g:
对所有的主题来说,每一个订阅者都有一个分发队列和挂起的游标。可以针对持久订阅者和短暂订阅者分别配置不同的策略,例如:
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic=“org.apache.>” producerFlowControl=“false“ memoryLimit=“1mb”>
<dispatchPolicy>
<strictOrderDispatchPolicy/>
</dispatchPolicy>
<deadLetterStrategy>
<individualDeadLetterStrategytopicPrefix=“Test.DLQ.” />
</deadLetterStrategy>
<pendingSubscriberPolicy>
<vmCursor/>
</pendingSubscriberPolicy>
<pendingDurableSubscriberPolicy>
<vmDurableCursor/>
</pendingDurableSubscriberPolicy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
Valid Subscriber types arevmCursorandfileCursor..The default is the store based cursor.
有效的订阅者游标类型是vmCursor和fileCursor。默认情况下是基于存储的游标。
Valid Durable Subscriber cursor types arevvmDurableCursorandfileDurableSubscriberCursor.The default is the store based cursor
有效的持久订阅者类游标类型是vvmDurableCursor和fileDurableSubscriberCursor。默认情况下是基于存储的游标。
Queues 队列
For Queues there is a single dispatch Queue and pending Queue for everydestination, so configuration is slightly different:
对于队列来说,每一个目的地都有一个单独的分发队列和挂起队列,所以配置略微有些不同:
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry queue=“org.apache.>”>
<deadLetterStrategy>
<individualDeadLetterStrategyqueuePrefix=“Test.DLQ.”/>
</deadLetterStrategy>
<pendingQueuePolicy>
<vmQueueCursor/>
</pendingQueuePolicy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
Valid Queue cursor types arevmQueueCursorandfileQueueCursor.The default is the store based cursor
有效的队列游标类型是vmQueueCursor和fileQueueCursor。默认情况下是基于存储的游标。