当前位置: 首页 > ActiveMQ in Action 读书笔记 > 正文

11.5 消息重发和死信队列

11.5 Message redelivery and dead-letter queues

When messages expire on the ActiveMQ broker (they exceed their time-to-live, if set)or can’t be redelivered, they’re moved to a dead-letter queue, so they can be consumedor browsed by an administrator at a later point.

11.5 消息重发和 死 信 队列

当ActiveMQ 代理上的消息过期(如果设置了消息的生存期,这些消息超过了生存期),或者不能被重新分发时,这些消息会被转移到死信队列中,因此这些消息将不能被消费或被系统管理员浏览.

Messages are normally redelivered to a client in the following scenarios:
? A client is using transactions and calls rollback() on the session.
? A client is using transactions and closes before calling commit.
? A client is using CLIENT_ACKNOWLEDGE on a session and calls recover() on that session.

通常,以下场景中的消息会被重新分发到客户端:
(1) 客户端使用了事务,并调用了rollback()方法
(2)客户端使用了事务,并在提交之前关闭了练级
(3)客户端使用了CLIENT_ACKNOWLEDGE的session,并调用了recover()方法

A client application usually has a good reason to roll back a transacted session or callrecover()—it may not be able to complete the processing of the message(s) becauseof its inability to negotiate with a third-party resource, for example. But sometimes anapplication may decide to not accept delivery of a message because the message ispoorly formatted. For such a scenario, it doesn’t make sense for the ActiveMQ brokerto attempt redelivery forever.

客户端程序通常有足够好的理由来回退一个事务或者调用recover()方法,比如,客户端可能不能完成对消息的处理,因为客户端不能与第三方资源交互了.但是,有时应用程序可能决定不接收格式不良的消息.这些场景中,ActiveMQ代理继续尝试重新发送消息就显得没有必要了.

A configurable POJO is associated with the ActiveMQ connection that you can tuneto set different policies. You can configure the amount of time the ActiveMQ brokershould wait before trying to resend the message, whether that time should increaseafter every failed attempt (use an exponential back-off and back-off multiplier), andthe maximum number of redelivery attempts before the message(s) are moved to adead-letter queue.
Here’s an example of how to configure a client’s redelivery policy:

ActiveMQ中提供一个与连接关联的可配置的POJO,通过这个POJO你可以配置不同的策略.你可以配置ActiveMQ代理在重新发送消息之前的等待时间,是否在每次尝试重新发送失败后,增长这个等待时间(使用指数back-off 和 back-off指数,译注:即是否使用指数back-off,如果使用指数back-off,则第一次失败后重新发送之前等待500毫秒,第二次失败再等待500 * 2毫秒,这2就是multiplier),以及消息被转移到奥死信队列之前尝试重新发送消息的最大次数.
下面是一个演示如何配置客户端重发送的策略实例:

RedeliveryPolicy policy = connection.getRedeliveryPolicy();
policy.setInitialRedeliveryDelay(500);
policy.setBackOffMultiplier(2);
policy.setUseExponentialBackOff(true);
policy.setMaximumRedeliveries(2);

By default, there’s one dead-letter queue for all messages, called AcitveMQ.DLQ,which expired messages or messages that have exceeded their redelivery attempts getsent to. You can configure a dead-letter queue for a hierarchy, or an individualdestination in the ActiveMQ broker configuration, like in the following example,where we set an IndividualDeadLetterStrategy:

默认情况下,所有消息只有一个死信队列,名称为AcitveMQ.DLQ,所有过期消息或者超过设置的重发尝试次数的长发之后仍没有发送成功的消息都会发送在这个死信队列中.你可以将死信队列配置成一个等级结构,或者在ActiveMQ代理中配置一个独立的消息目的地,就像下面列子中配置的一样,使用IndividualDeadLetterStrategy来配置:

">
        

 Note that we configure this dead-letter strategy to ignore nonpersistent and expiredmessages, which can prevent overwhelming the ActiveMQ broker with messages, ifyou’re using time-to-live on nonpersistent messages.

请注意,上面的配置中配置的死信策略忽略了所有非持久化消息和过期消息,如果你使用实时的非持久化消息时,上面的配置这可以避免ActiveMQ代理被海量信息压垮.

When a message is sent to a dead-letter queue, an advisory message is generatedfor it. You can listen for dead-letter queue advisory messages on the topicActiveMQ.Advisory.MessageDLQd.*.

当一个消息被发送到死信队列后,代理会生成一个advisory消息.你可以监听死信队列的advisory消息主题:ActiveMQ.Advisory.MessageDLQd.*.

In the next section, we’ll look at some of the interceptor plug-ins that are availableto extend the behavior of the ActiveMQ broker.

下一节中,我们将看到使用一些拦截器插件扩展ActiveMQ代理的功能.

赞 赏

   微信赞赏  支付宝赞赏


本文固定链接: https://www.jack-yin.com/coding/translation/activemq-in-action/1708.html | 边城网事

该日志由 边城网事 于2013年12月16日发表在 ActiveMQ in Action 读书笔记 分类下,
原创文章转载请注明: 11.5 消息重发和死信队列 | 边城网事

报歉!评论已关闭.