blob: 5c8e1add82c87cbf57edccb2b61130c22732d14f [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><structured-macro ac:macro-id="9d386966-fbbb-49f5-afcd-e4fc705a19ed" ac:name="note" ac:schema-version="1"><parameter ac:name="title">Check your Message Properties</parameter><rich-text-body><p>The message property <code>scheduledJobId&#160;</code>is reserved for use by the Job Scheduler. If this property is set before sending, the message will be sent immediately and not scheduled. Also, <span>after a scheduled message is received, the property </span><span><code>scheduledJobId</code>&#160;will be set on the received message so </span>keep this in mind if using something like a Camel Route which might automatically copy properties over when re-sending a message.</p></rich-text-body></structured-macro><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 the connivence of Java JMS clients - there's an interface with the property names used for scheduling at <em><strong>org.apache.activemq.ScheduledMessage</strong></em>.</p><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="c68d7805-8bf6-4833-b600-9a20ee84ca98" ac:name="code" ac:schema-version="1"><plain-text-body> 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);
</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="912377b9-7446-4d59-ab20-80cea8490783" ac:name="code" ac:schema-version="1"><plain-text-body> 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);
</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="a4d4b451-b5a5-4c05-b608-6034275960a5" ac:name="code" ac:schema-version="1"><plain-text-body> MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("test msg");
message.setStringProperty(ScheduledMessage.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="6a9d7e73-9d13-44b2-80e0-63ca1b8130b3" ac:name="code" ac:schema-version="1"><plain-text-body> 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);
</plain-text-body></structured-macro></div>