blob: fe145d867323f3ce90137f4cebe63d9e5135dbcb [file] [log] [blame]
<div class="wiki-content maincontent"><p>ActiveMQ from version <strong>5.4</strong> has an optional persistent scheduler built into the ActiveMQ message broker. It is enabled by setting the broker <strong>schedulerSupport</strong> attribute to true in the <link><page ri:content-title="xml configuration"></page></link>. <br clear="none">
An ActiveMQ client can take advantage of a delayed delivery by using the following message properties:</p>
<table><tbody><tr><th colspan="1" rowspan="1"><p>Property name</p></th><th colspan="1" rowspan="1"><p>type </p></th><th colspan="1" rowspan="1"><p> description </p></th></tr><tr><td colspan="1" rowspan="1"><p>AMQ_SCHEDULED_DELAY</p></td><td colspan="1" rowspan="1"><p> long </p></td><td colspan="1" rowspan="1"><p> The time in milliseconds that a message will wait before being scheduled to be delivered by the broker</p></td></tr><tr><td colspan="1" rowspan="1"><p>AMQ_SCHEDULED_PERIOD </p></td><td colspan="1" rowspan="1"><p>long </p></td><td colspan="1" rowspan="1"><p> The time in milliseconds to wait after the start time to wait before scheduling the message again</p></td></tr><tr><td colspan="1" rowspan="1"><p>AMQ_SCHEDULED_REPEAT </p></td><td colspan="1" rowspan="1"><p>int </p></td><td colspan="1" rowspan="1"><p> The number of times to repeat scheduling a message for delivery</p></td></tr><tr><td colspan="1" rowspan="1"><p>AMQ_SCHEDULED_CRON </p></td><td colspan="1" rowspan="1"><p>String </p></td><td colspan="1" rowspan="1"><p> Use a Cron entry to set the schedule</p></td></tr></tbody></table>
<p>For example, to have a message scheduled for delivery in 60 seconds - you would need to set the <em>AMQ_SCHEDULED_DELAY</em> property:</p>
<structured-macro ac:macro-id="23d3c5d1-1f3d-476b-ade8-6c477b6647a8" ac:name="code" ac:schema-version="1"><plain-text-body>
IMessageProducer producer = session.CreateProducer(destination);
ITextMessage message = session.CreateTextMessage("test msg");
long time = 60 * 1000;
message.Properties["AMQ_SCHEDULED_DELAY"] = time;
producer.Send(message);
</plain-text-body></structured-macro>
<p>You can set a message to wait with an initial delay, and the repeat delivery 10 times, waiting 10 seconds between each re-delivery:</p>
<structured-macro ac:macro-id="2a059a1e-bbf7-46b3-9429-eabbda397b89" ac:name="code" ac:schema-version="1"><plain-text-body>
IMessageProducer producer = session.CreateProducer(destination);
ITextMessage message = session.CreateTextMessage("test msg");
long delay = 30 * 1000;
long period = 10 * 1000;
int repeat = 9;
message.Properties["AMQ_SCHEDULED_DELAY"] = delay;
message.Properties["AMQ_SCHEDULED_PERIOD"] = period;
message.Properties["AMQ_SCHEDULED_REPEAT"] = repeat;
producer.Send(message);
</plain-text-body></structured-macro>
<p>You can also use <a shape="rect" href="http://en.wikipedia.org/wiki/Cron">CRON</a> to schedule a message, for example, if you want a message scheduled to be delivered every hour, you would need to set the CRON entry to be - <em>0 * * * *</em> - e.g.</p>
<structured-macro ac:macro-id="ab4ffcde-e93e-4041-9440-5375bd6361a7" ac:name="code" ac:schema-version="1"><plain-text-body>
IMessageProducer producer = session.CreateProducer(destination);
ITextMessage message = session.CreateTextMessage("test msg");
message.Properties["AMQ_SCHEDULED_CRON"] = "0 * * * *";
producer.Send(message);
</plain-text-body></structured-macro>
<p>CRON scheduling takes priority over using message delay - however, if a repeat and period is set with a CRON entry, the ActiveMQ scheduler will schedule delivery of the message for every time the CRON entry fires. Easier to explain with an example. Supposing that you want a message to be delivered 10 times, with a one second delay between each message - and you wanted this to happen every hour - you'd do this:</p>
<structured-macro ac:macro-id="a071b052-b175-4e4e-a6eb-1055fb8cf033" ac:name="code" ac:schema-version="1"><plain-text-body>
IMessageProducer producer = session.CreateProducer(destination);
ITextMessage message = session.CreateTextMessage("test msg");
message.Properties["AMQ_SCHEDULED_CRON"] = "0 * * * *";
message.Properties["AMQ_SCHEDULED_DELAY"] = 1000;
message.Properties["AMQ_SCHEDULED_PERIOD"] = 1000;
message.Properties["AMQ_SCHEDULED_REPEAT"] = 9;
producer.Send(message);
</plain-text-body></structured-macro></div>