## 11.4 消费者的消息追溯

### 11.4 Retroactive consumers

For applications that require messages to be sent and consumed as quickly as possible—for example, a real-time data feed—it’s recommend that you send messages withpersistence turned off.

### 11.4 消费者的消息追溯

There’s a downside to consuming nonpersistent messages, in that you’ll only beable to consume messages from the point when your message consumer starts. Youcan miss messages if your message consumer starts behind the message producer, orthere’s a network glitch and your message consumer needs to reconnect to the broker(or another one in a network).

In order to provide a limited method of retroactive consumption of messages withoutrequiring message persistence, ActiveMQ has the ability to cache a configurablesize or number of messages sent on a topic. There are two parts to this—your messageconsumers need to inform the ActiveMQ broker that it’s interested in retroactive messages,and you need to configure the destination in the broker to say how many messagesshould be cached for consumption at a later point.

To mark a consumer as being retroactive, you need to set the retroactive flag forthe message consumer. The easiest way to do that is to set the property on the topicname you use:

String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);
Connection connection = connectionFactory.createConnection();
connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("soccer.division1.leeds?consumer.retroactive=true");
MessageConsumer consumer = session.createConsumer(topic);
Message result = consumer.receive();

On the broker side, you can configure a number of recovery policies on a topic-by-topicbasis. The default is called the FixedSizedSubscriptionRecoveryPolicy, whichholds a number of messages in a topic, based on the calculated size the messages willtake from the broker memory. The default size is 64 KB.

You can configure the subscription recovery policy on a named topic, or use wildcardsto apply them to hierarchies. Here’s a sample configuration snippet of how tochange the default cache size for the FixedSizedSubscriptionRecoveryPolicy forall topics created in the ActiveMQ broker:

<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic=">">
<subscriptionRecoveryPolicy>
<fixedSizedSubscriptionRecoveryPolicy maximumSize="8mb"/>
</subscriptionRecoveryPolicy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>

Retroactive message consumers are a convenient mechanism to improve the reliabilityof your applications without incurring the overhead of message persistence. We’veseen how enable retroactive consumers and how to configure their broker-side counterpart,the SubscriptionRecoveryPolicy.

In the next section we’re going to look at how ActiveMQ stores messages that can’tbe delivered to message consumers—dead-letter queues.

微信赞赏  支付宝赞赏