ActiveMQ遅延とタイミング配信

3796 ワード

AMQの遅延やタイミング配信は非常に簡単で、製品のニーズに応じてメッセージ生産時に必要な属性を追加することで目的を達成することができます。Brokerに「スケジュールサポート」を追加したことが前提です。
公式からの説明:
http://activemq.apache.org/delay-and-schedule-message-delivery.html
ActiveMQ from version 5.4 has an optional persistent scheduler built in to the Active MQ message brook.It is enabed by setting the breaker scheduler Support to true the Active.Active。
Proptyname
タイプ
description
AMQ_SCHEDULED_DELAY
long
The time in miliseconds that a message will wait before being scheduled to be delivered by the brook er
AMQ_SCHEDULED_PERIOD
long
The time in miliseconds to wait after the start time to wait before scheduling the message again
AMQ_SCHEDULED_REPEAT
要点
The number of times to repeat scheduling a message for delivery
AMQ_SCHEDULED_CRON
String
Use a Cron entry to set the schedule
For the connivence of Java JMS clients-there's an interface with the property names used for scheduling at org.apphe.activemq.ScheduledMessage.
For example、to have a message scheduled for delivery in 60 seconds-you would need to set the AMQ_SCHEDULED_DELAY
property:MessageProducer producer = session.createProducer(destination);TextMessage message = session.createTextMessage("test msg");long time = 60* 1000;message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);producer.send(message);You can set a message to wait with an initial delay,and the repeat delivery 10 times,waiting 10 secondbet ween each re-delivery:MessageProducer producer = session.createProducer(destination);TextMessage message = session.createTextMessage("test msg");long delay = 30* 1000;long period = 10* 1000;int repeat = 9;message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);producer.send(message);You can also use CRON to schedule a message、for example、if You want a message scheduled to be delivered everhour、you would need to set the CRON ntry to be-0*-e.g.MessageProducer producer = session.createProducer(destination);TextMessage message = session.createTextMessage("test msg");message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "0* * * *");producer.send(message);CRON scheduling Tanes pristy over using message delay-however,if a repeat and period is set with a CRON entry,the Active MQ scheduler will schedule delivery of the message for time the CRON entfireet.Eastime。with a one second delay between each message-and you wanted this to happen everhour-you'd dothis:MessageProducer producer = session.createProducer(destination);TextMessage message = session.createTextMessage("test msg");message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "0* * * *");message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 1000);message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1000);message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 9);producer.send(message)