簡體   English   中英

使用 NodeJS 將服務總線隊列中的消息從被處理延遲到預定時間

[英]Delay a message in the Service Bus Queue from being processed until a scheduled time with NodeJS

到目前為止,我已經嘗試了許多不同的教程來讓它工作,但到目前為止我還沒有能夠做到這一點。

目前我有一個 nodejs 應用程序,它向服務總線隊列和另一個連續輪詢的 nodejs 發送消息。 我的目標是將消息發送到輪詢系統可以處理消息的特定時間和日期的隊列。

到目前為止,我的結果是,一旦我發送消息,它就會變得可見並立即得到處理,這就是我所擁有的

//made some changes after the first suggestion, but still does not work
//what I'm doing here is offsetting the time difference with UTC(im in GMT-7 tz) by adding it manually 
//(this is just a test so if this would have worked I would have made it more elegant)
var scheduled_time = new Date().valueOf() + ((60000*60)*7.5);
  var current_time = Date.now();
  console.log(scheduled_time, current_time);

  var message = {
    body: 'Time ' + current_time.toString(),
    brokerProperties:{
      ScheduledEnqueueTimeUtc: scheduled_time,
      TimeToLive: 8
    },
    customProperties: {
      testproperty: 'TestValue'
    }};

  serviceBus.sendQueueMessage('myqueue', message, function(error){
    if(!error){
      // message sent
      console.log('message sent');
    }
  });

我的接收器很簡單

function receiveMessages() {
  serviceBus.receiveQueueMessage(queue,
      function (error, message) {
        if (error) {
          console.log(error);
        } else {
          console.log('Process after ' + message.brokerProperties.ScheduledEnqueueTimeUtc);
        }
      });
};

到目前為止,我已經閱讀了包含消息屬性描述的 GitHub 頁面,這對我所擁有的似乎是正確的,但它仍然不起作用。

謝謝

Date.now()返回您所在時區的日期,而不是 UTC。 您需要將其轉換為 UTC。 這個問題可以幫助你解決這個問題

問題之一似乎是ScheduledEnqueueTimeUtc的格式。 我能夠使用moment.js成功延遲來自 Node.js 的 Azure 服務總線中的隊列消息來調整和格式化日期:

// Delay message by 1 hour
var scheduled_time = moment().utc().add(1, 'hour').format('M/D/YYYY H:mm:ss A');
var message = {
    body: 'Time ' + Date.now().toString(),
    brokerProperties: {
        ScheduledEnqueueTimeUtc: scheduled_time
    },
    customProperties: {
        testproperty: 'TestValue'
    }
};

不幸的是,Service Bus 文檔和 JavaScript SDK 似乎沒有提及關於ScheduledEnqueueTimeUtc應該采用什么格式的任何內容,除了類型是字符串。 但是,如果您查看 .NET 語言中的代碼示例,則會使用 DateTime.toString() 傳遞該值,該值默認為“G”通用日期和時間格式說明符(例如:5/1/2009 9:00:00上午)。 按照這些示例,我們可以使用類似 moment.js 的內容復制格式:“ M/D/YYYY H:mm:ss A ”。 正如 TheDude 提到的,請記住該值必須是 UTC而不是本地時間。

此外,您的 TimeToLive 值 8 可能太短而無法處理,請注意,使用ScheduledEnqueueTimeUtc延遲或安排隊列並不能保證它會在指定的日期和時間得到處理。 它只保證消息不會被預先處理。 請參閱https://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.brokeredmessage.scheduledenqueuetimeutc.aspx 中的備注

根據我的經驗,我認為您可以嘗試檢查 Azure 上的服務總線和 Node.js 應用程序的區域以保持相同的區域。

更多信息,請參閱BrokeredMessagehttps://msdn.microsoft.com/en-us/library/azure/microsoft.servicebus.messaging.brokeredmessage.aspx的代理屬性。

上面的示例使用azure-sb包,該包又使用 HTTP 與 Azure 服務總線通信。 較新的@azure/service-bus包使用 AMQP,並且比azure-sb性能更高。

如果使用@azure/service-bus包,那么可以直接在消息上設置scheduledenqueuetimeutc並發送。 此屬性的類型為日期

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM