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

12.6 使用 ActiveMQ消息调度 延迟发送消息

12.6 Scheduling messages to be delivered by ActiveMQ in the future

12.6 使用ActiveMQ消息调度延迟发送消息

The ability to schedule a message to be delivered after a delay, or at regular intervals,is an extremely useful feature provided by ActiveMQ. One unique benefit is that messagesthat are scheduled to be delivered in the future are stored persistently, so thatthey can survive a hard failure of an ActiveMQ broker and be delivered on restart.You specify that you want a message to be delivered at a later time by setting welldefinedproperties on the message. For convenience, the well-known property namesare defined in the org.apache.activemq.ScheduledMessage interface. These propertiesare shown in table 12.2.

ActiveMQ消息调度 实现的消息延迟发送或者在按照固定时间的间隔实现间隔发送的功能十分有用.其中一个独一无二的好处是消息调度设置为延迟发送的消息将会被持久化存储,因而在ActiveMQ代理严重失效是消息不会丢失并且在代理重启后会继续发送消息.你可以通过严格定义消息的属性来设置如何延迟发送消息.为方便起见,常用的延迟发送消息相关的属性都在org.apache.activemq.ScheduledMessage接口中有定义,如表12.2所示.

Table 12.2 TransportConnector properties for updating clients of cluster changes

Property name Type Description
AMQ_SCHEDULED_DELAY  false The time in milliseconds that a message will wait before being scheduled to be delivered by the broker
AMQ_SCHEDULED_PERIOD false The time in milliseconds after the start time to wait before scheduling the message again
AMQ_SCHEDULED_REPEAT false The number of times to repeat scheduling a message for delivery
AMQ_SCHEDULED_CRON String  

 表12.2 更新客户端集群时需要修改的TransportConnector属性

属性名称 类型 描述
AMQ_SCHEDULED_DELAY false 消息延迟发送的延迟时间(单位毫秒) 
AMQ_SCHEDULED_PERIOD false 代理启动后,发送消息之前的等待时间(单位毫秒)
AMQ_SCHEDULED_REPEAT false 调度消息发送的重复次数
 AMQ_SCHEDULED_CRON  String 使用一个cron实体设置消息发送调度

To have a message wait for a period of time before its delivered, you only need to setthe AMQ_SCHEDULED_DELAY property. Suppose you want to publish a message fromyour client, but have it actually delivered in 5 minutes time. You’d need to do somethinglike the following in your client code:

只需设置AMQ_SCHEDULED_DELAY这一个属性即可让消息等待一段时间后再发送.假设你打算从你的客户端发送消息,但需要设置消息延迟5分钟后发送,你可以使用下面的客户端代码来实现:

ActiveMQ will store the message persistently in the broker, and when it’s scheduled, itwill deliver it to its destination. This is important, because although you’ve specifiedthat you want the message to be delivered in 5 minutes time, if the destination is aqueue, it will be posted to the end of the queue. So the actual delivery time will bedependent on how many messages already exist on the queue awaiting delivery.

设置延迟发送之后,ActiveMQ将消息存储在代理中,等待设置的延时时间过了之后,消息会被发送到设定的目的地.尽管你已经指定了消息在5分钟之后发送,但是如果消息目的地是一个消息队列,则消息会被发送到队列的末端,这一点很重要.因而,消息发送的实际延迟时间将取决于当前消息的目的地队列中有多少个正等待的消息.

You can also use a the AMQ_SCHEDULED_PERIOD and AMQ_SCHEDULED_REPEAT propertiesto have messages delivered at a fixed rate. The following example will send a message100 times, every 30 seconds:

你也可以使用AMQ_SCHEDULED_PERIOD和AMQ_SCHEDULED_REPEAT属性设置消息按照固定间隔时间发送固定的次数.下面的示例代码将发送消息100,每次发送间隔时间为30秒:

Note that we specified the repeat as being 99, as the first message + 99 = 100. If youschedule a message to be sent once, the message ID will be the same as the one youpublished. If you schedule a repeat, or use the AMQ_SCHEDULED_CRON property toschedule your message, then ActiveMQ will create a unique message ID for the deliveredmessage.

注意,我们将消息重复发送的次数设置为99,因为第一次发送消息 + 99此重复=100.如果你设置消息只被发送一次,那么消息的ID即使你设置的消息ID.如果你设置了重复发送,或者使用AMQ_SCHEDULED_CRON属性来调度消息发送,那么ActiveMQ会生产一个新的唯一的消息ID作为重复发送的消息ID.

Cron is a well-known job scheduler on Unix systems, and it uses an expressionstring to denote when a job should be scheduled. ActiveMQ uses the same syntax, asdescribed next:

Cron是Unix系统中任务调度器,它使用一个字符串来表示一个任务何时需要被执行.ActiveMQ使用同样的预防,如下文本描述:

For example, if you want to schedule a message to be delivered at 2 a.m. on the twelfthday of every month, you’d need to do the following:

例如,如果你打算在每月的12号早上2点发送消息,你需要按照如下代码所示进行设置:

You can combine scheduling with cron and a simple delay and repeat, but the cronentry will always take precedence. For example, instead of sending one message at 2a.m. on the twelfth day of every month, you may want to schedule 10 messages to bedelivered every 30 seconds:

你可以同时使用cron和普通的延迟与重复来调度消息发送,但是cron方式的调度具有优先权.例如,不同于每月的12号早上2点只发送一条消息,你可能打算在每月的12号早上2点开始发送消息,并且每隔30秒再重复发送9个消息:

In this section we’ve looked at how to schedule messages for sometime in the futureusing ActiveMQ. You should now be able to send messages after a delay, send multipleinstances of the same message at regular intervals, and use a cron entry to schedulemessage delivery.

本节中我们看到了如何使用ActiveMQ调度消息发送使得消息能在未来的某个时间发送.现在,你应当能够延迟发送消息,也能够以固定间隔时间重复发送消息,并且能够使用cron实体来调度消息发送.

打个赏呗

   微信打赏  支付宝打赏


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

该日志由 边城网事 于2013年12月27日发表在 ActiveMQ in Action 读书笔记 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 12.6 使用 ActiveMQ消息调度 延迟发送消息 | 边城网事

12.6 使用 ActiveMQ消息调度 延迟发送消息 暂无评论

发表评论

快捷键:Ctrl+Enter