amqp / rhea

A reactive messaging library based on the AMQP protocol
Apache License 2.0
280 stars 79 forks source link

How to send schedule/delayed message? #236

Open ron-liu opened 5 years ago

ron-liu commented 5 years ago

Hi,

I want to implement sending delayed message, and I have been tried:

The following is the code I tried, but doesn't work, the message was sent straight away instead of delayed.

var container = require('rhea');
container.on('connection_open', function (context) {
    context.connection.open_receiver('examples');
    context.connection.open_sender('examples');
});
container.on('message', function (context) {
    console.log(context.message.body);
    context.connection.close();
});
container.once('sendable', function (context) {
    context.sender.send({body:'Hello World!',  'x-opt-delivery-delay': 10000, delivery_annotations: {AMQ_SCHEDULED_DELAY: 10000, 'x-opt-delivery-delay': 10000}, message_annotations: {AMQ_SCHEDULED_DELAY: 10000, 'x-opt-delivery-delay': 10000}, application_properties: {AMQ_SCHEDULED_DELAY: 10000, 'x-opt-delivery-delay': 10000}});
});

container.connect({'port':5672, host: 'activemq', username:'admin', password: 'admin' });
grs commented 5 years ago

What version of artemis and rhea are you using? This works for me using latest rhea and artemis 2.9 (feature was only exposed through amqp since artemis 2.7 I believe):

    context.sender.send({body:'Hello World!',  message_annotations: {'x-opt-delivery-delay': 10000}});

Perhaps also check that the message being delivered immediately is not one that was already on the queue. E.g. make the body random or at least change it for a test run.

ps2goat commented 3 years ago

I'm using ActiveMQ 5.19.x (AmazonMQ), is it supposed to work there? We aren't on Artemis, yet. I've tried setting these in both delivery_annotations and message_annotations (when calling release) with no apparent luck so far.

grs commented 3 years ago

I don't know. Question for the ActiveMQ I think. Do they expose that functionality over AMQP, and if so how do they do it?

ps2goat commented 3 years ago

Good question. I'm currently digging through their code, will report back. https://activemq.apache.org/components/nms/providers/activemq/advanced-features/delay-and-schedule-message-feature https://activemq.apache.org/maven/apidocs/src-html/org/apache/activemq/

ps2goat commented 3 years ago

I have a question on stackoverflow, if anyone wants to follow it there. For all I know, it could be an Amazon limitation. I haven't tried a JMS client at all.

It appears that ActiveMQ does have a conversion from AMQP to JMS, copying the headers from one to the other. So maybe if there was a possibility to add custom headers to the message this could work.