To delay a JMS message in Spring Boot with ActiveMQ, set the scheduled-delivery property AMQ_SCHEDULED_DELAY (in milliseconds) on the message before sending it. The broker holds the message and delivers it to the consumer after that time. The feature belongs to ActiveMQ, not to the JMS specification before 2.0, and it requires the broker's scheduler to be enabled.
The examples target Spring Boot 3.x (Jakarta JMS) with ActiveMQ Classic. Spring Boot 3 uses the jakarta.jms packages, so older javax.jms imports no longer work.
JMS 2.0 added a standard delivery delay (JMSContext.setDeliveryDelay and MessageProducer.setDeliveryDelay), but ActiveMQ Classic does not implement it for its classic client. Instead it provides its own scheduler, driven by message properties:
| Property | Meaning |
|---|---|
AMQ_SCHEDULED_DELAY | Delay in milliseconds before the first delivery |
AMQ_SCHEDULED_PERIOD | Milliseconds between repeated deliveries |
AMQ_SCHEDULED_REPEAT | How many extra times to repeat the delivery |
AMQ_SCHEDULED_CRON | A cron expression for scheduled delivery |
The constants are available on org.apache.activemq.ScheduledMessage. ActiveMQ Artemis supports a similar feature with the property _AMQ_SCHED_DELIVERY (an absolute time in milliseconds), so check which broker you run.
For Spring Boot 3 with ActiveMQ Classic, add the starter, which includes the Spring JMS support and the client:
Point the application at your broker in application.properties:
The scheduler must be switched on at the broker. In conf/activemq.xml set schedulerSupport="true" on the <broker> element and restart:
If scheduler support is off, the message property is ignored and the message is delivered immediately, which is the most common reason "the delay does nothing". Do not hard-code passwords; read them from the environment or a secret store.
Use JmsTemplate and a MessagePostProcessor to add the scheduling property.
Calling sendWithDelay("reminders", "Pay invoice 1042", 60_000) makes the consumer receive the message about one minute later. The post processor lambda receives a jakarta.jms.Message, and setLongProperty is the right setter because the broker expects a numeric value.
This delivers the message at 06:00 every day using standard five-field cron syntax. Use repeat and period properties for a fixed number of redeliveries instead.
The consumer needs no special code. A normal listener receives the message once the delay has passed.
Spring Boot enables JMS listener support automatically when the starter is on the classpath, so @EnableJms is not required.
If you cannot enable the broker scheduler, or the broker is not ActiveMQ, you have other options:
DelaySeconds).@Scheduled poller.TaskScheduler when the delay is short and losing it on restart is acceptable.For anything that must survive restarts, rely on a persistent store: the ActiveMQ scheduler writes to its scheduler store, so use persistent delivery (the default for JmsTemplate) and keep the broker data directory on durable storage.
The most likely cause is schedulerSupport being off in the broker configuration, or the property being set with the wrong type. Enable the scheduler and use setLongProperty with AMQ_SCHEDULED_DELAY.
No. JmsTemplate has no delay option. Spring only gives you the hook, MessagePostProcessor, to set a provider-specific property on the message.
Artemis uses a different property, _AMQ_SCHED_DELIVERY, holding the absolute delivery time in epoch milliseconds, and it also supports the JMS 2.0 delivery delay.
Send a message to the special ActiveMQ.Scheduler.Management topic with the scheduled job id, using the AMQ_SCHEDULER_ACTION property. The job id appears in the scheduler browse results.
Enable schedulerSupport on the ActiveMQ broker, add AMQ_SCHEDULED_DELAY (or a cron or repeat property) with a MessagePostProcessor, and consume with a normal @JmsListener. Use the Jakarta JMS packages in Spring Boot 3, and fall back to a database poller or a delay queue when the broker has no scheduler.