简体   繁体   中英

Azure queue handling via ReceiveAsync returns null right away

The normal expected behaviour for the code below, would be that ReceiveAsync, looks at the Azure queue for up to 1 minute before returning null or a message if one is received. The intended use for this is to have an IoT hub resource, where multiple messages may be added to a queue intended for one of several DeviceClient objects. Each DeviceClient will continuously poll this queue to receive message intended for it. Messages for other DeviceClients are thus left in the queue for those others.

The actual behaviour is that ReceiveAsync is immediately returning null each time it's called, with no delay. This is regardless of the value that is given with TimeSpan - or if no parameters are given (and the default time is used).

So, rather than seeing 1 log item per minute, stating there was a null message received, I'm getting 2 log items per second (!). This behaviour is different from a few months ago,. so I started some research - with little result so far.

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.
}

I looked at the Azure hub, and noticed 8 messages in the queue. I then added 2 more, and neither of the new messages were received, and the queue is now on 10 items.

I did notice this question: Azure ServiceBus: Client.Receive() returns null for messages > 64 KB But I have no way to see whether there is indeed a message that big currently in the queue (since receivemessage returns null...)

As such the questions:

  • Could you preview the messages in the queue?
  • Could you get a queue size, eg ask the number of messages in the queue before getting them?
  • Could you delete messages from the queue without getting them?
  • Could you create a callback based receive instead of an infinite loop? (I guess internally the code would just do a peek and the same as we are already doing)

Any help would be greatly appreciated.

If you use the Azure ServiceBus, I recommend that you could use the Service Bus Explorer to preview the message, get the number of message in the queue. And Also you could delete the message without getting them.

在此处输入图片说明

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