Send message from an Azure ServiceBus Topic after a specific delay with node.js

帅比萌擦擦* 提交于 2019-12-11 00:42:42

问题


Basically, within an Azure Service Bus, I have a topic and a subscription.

If a message arrives in the topic between 11:00AM, my subscriber should not handle it yet. However, at 14:00PM, I would expect my subscriber to treat it.

Is there a way to achieve this natively with Topic filters?

I don't find any mention of this kind of use case in the official documentation regarding filters.
Indeed, all presented samples are about:
"subscriber handling this kind of message, or never".
I'm looking for:
"subscriber expecting handling this kind of message but, but later at a specific time".


回答1:


Sounds like you want to defer a message ?

Don't know much about the Azure SDK for Node.js but from the MSDN Documentation you can set a ScheduledEnqueueTimeUtc on the message :

The scheduled enqueue time in UTC. This value is for delayed message sending. It is utilized to delay messages sending to a specific time in the future.

Only sample to send a message to a Queue is :

var message = {
    body: 'Test message',
    customProperties: {
        testproperty: 'TestValue'
}};
serviceBusService.sendQueueMessage('myqueue', message, function(error){
    if(!error){
        // message sent
    }
});

From the nodejs sdk, I found a constants.js file that defines these properties :

/**
* The broker properties for service bus queue messages.
*
* @const
* @type {string}
*/
BROKER_PROPERTIES_HEADER: 'brokerproperties',
...
/**
* The scheduled enqueue time header.
*
* @const
* @type {string}
*/
SCHEDULED_ENQUEUE_TIME_HEADER: 'x-ms-scheduled-enqueue-time',

If you have a look at the servicebusservice.js, there is a setRequestHeaders function that takes some properties of the message and set it as header.

So I guess you can set this property on the message like that :

// Set your scheduled date
var scheduledDate = Date.now();
scheduledDate.setHours(scheduledDate.getHours()+3);

var message = {
    body: 'Test message',
    brokerproperties: {
        'x-ms-scheduled-enqueue-time': scheduledDate.toUTCString()
}};

Let me know if it works :-)




回答2:


I was able to get it to work using this:

        const scheduledDate = new Date();
        scheduledDate.setMinutes(scheduledDate.getMinutes()+1);

        const message = {
        body: 'Hey, this worked again!',
        customProperties: {
            testproperty: 'TestValue'
        },
        brokerProperties: {
            ScheduledEnqueueTimeUtc: scheduledDate.toUTCString()
        }
    };
    serviceBusService.sendQueueMessage('myqueue', message, function(error){
        if(!error){
            console.log('We sent a message. Huzzah!');
        }
    });


来源:https://stackoverflow.com/questions/39116319/send-message-from-an-azure-servicebus-topic-after-a-specific-delay-with-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!