首页 文章

通过ReceiveAsync进行Azure队列处理会立即返回null

提问于
浏览
1

下面代码的正常预期行为是ReceiveAsync,在返回null之前查看Azure队列最多1分钟,或者如果收到消息则查看消息 . 对此的预期用途是具有IoT中枢资源,其中可以将多个消息添加到旨在用于若干DeviceClient对象之一的队列中 . 每个DeviceClient将连续轮询此队列以接收针对它的消息 . 因此,其他DeviceClient的消息将留在队列中以供其他人使用 .

实际行为是ReceiveAsync每次调用时都会立即返回null,没有延迟 . 这与TimeSpan给出的值无关 - 或者如果没有给出参数(并且使用默认时间) .

因此,不是每分钟看到1个日志项,而是说收到了一条空消息,我每秒得到2个日志项(!) . 这种行为与几个月前不同 . 所以我开始做一些研究 - 到目前为止收效甚微 .

using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Client;

public static TimeSpan receiveMessageWaitTime = new TimeSpan(0, 1 , 0);
Microsoft.Azure.Devices.Client.Message receivedMessage = null;

deviceClient = DeviceClient.CreateFromConnectionString(Settings.lastKnownConnectionString, Microsoft.Azure.Devices.Client.TransportType.Amqp);


// This code is within an infinite loop/task/with try/except code
if(deviceClient != null)
{
  receivedMessage = await deviceClient.ReceiveAsync(receiveMessageWaitTime);
  if(receivedMessage != null)
  {
    string Json = Encoding.ASCII.GetString(receivedMessage.GetBytes());
    // Handle the message
  }
  else
  {
    // Log the fact that we got a null message, and try again later
  }
  await Task.Delay(500); // Give the CPU some time, this is an infinite loop after all.
}

我查看了Azure集线器,发现队列中有8条消息 . 然后我又添加了2个,并且没有收到任何新消息,现在队列中有10个项目 .

我确实注意到了这个问题:Azure ServiceBus: Client.Receive() returns null for messages > 64 KB但是我无法确定队列中是否确实存在大的消息(因为receivemessage返回null ...)

就这样问题:

  • 你能预览队列中的消息吗?

  • 你能得到一个队列大小,例如在获取之前询问队列中的消息数量?

  • 你能否从队列中删除消息而不获取它们?

  • 你能创建一个基于回调的接收而不是无限循环吗? (我想在内部代码只会看一眼,就像我们已经在做的那样)

任何帮助将不胜感激 .

1 回答

  • 0

    如果您使用Azure ServiceBus,我建议您使用Service Bus Explorer来预览消息,获取队列中的消息数 . 此外,您可以删除邮件而不获取它们 .

    enter image description here

相关问题