简体   繁体   中英

Delaying messages in ActiveMQ

I would like to delay a message to my JMS client. I'm using AMQ 5.8.0. In the documentation it states to set up the config setting schedulerSupport to true (it's false by default)

<broker xmlns="http://activemq.apache.org/schema/core"
        persistent="false"
        dedicatedTaskRunner="false"
        schedulerSupport="true"
        brokerName="mybroker">

Here is my test code for producing/consuming my record

TextMessage sendMessage = producerSession.createTextMessage();
sendMessage.setStringProperty(Constants.PARAM_ID, newUniqueId);
long delay = 20 * 1000;
sendMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay); //sets delay
sendMessage.setJMSCorrelationID(newUniqueId);
producer.send(sendMessage);

Message m = consumer.receive(); //get message. This should be null
if(m != null)
{
    id = m.getStringProperty(Constants.PARAM_ID); //I get the message well before 20 sec delay
}

I can see my record in the jetty browser under the scheduled tab. The message should be delayed by 20 seconds. However, when the consumer.receive() call is made, I get the record well before the 20 delay has expired..

Am I misunderstanding how this delay works? I don't want the record to be available for a period of time.

You have to use the ScheduledMessage.AMQ_SCHEDULED_DELAY property, ie

sendMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);

and to enable persistence (by default = true, if not set):

<broker persistent="true">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM