7

I'm trying to figure out how can I listen to an events on a queue (especially an enqueue event).

Say I have a Console Application and a Service Bus Queue/Topic, how can I connect to the Queue and wait for a new message ?

I'm trying to achieve this without While(true) and constant polling, I'm trying to do it more in a quite listener way something like a socket that stay connected to the queue.

The reason I don't want to use polling is that I understand that its floods the server with requests, I need a solution that will work on great loads.

Thank you.

I gave very basic example for the simplicity of the question but My real situation are a bit more complex:

I've Web-API that send messages that are needed to be process to a Worker Role using Service Bus Queue.

I need somehow to know when the Worker has been processed the message. I want the Worker to send a message to the queue alerting that the Web API has processed the message, but now I need to make the Web-API "sit" and wait for a response of the Worker back, that lead me to my question:

How can I listen to a queue constantly and without polling (because there is a lot of instances that will pooling and its will create a lot of requests that maybe its best to avoid.

mathewc
  • 13,312
  • 2
  • 45
  • 53
Ron
  • 1,744
  • 6
  • 27
  • 53

2 Answers2

1

Use the Azure WebJobs SDK - it has triggers to monitor queues and blobs with simple code like:

public static void Main()
{
    JobHost host = new JobHost();
    host.RunAndBlock();
}

public static void ProcessQueueMessage([QueueTrigger("webjobsqueue")] string inputText, 
    [Blob("containername/blobname")]TextWriter writer)
{
    writer.WriteLine(inputText);
}

There is a great tutorial at What is the Azure WebJobs SDK.

viperguynaz
  • 12,044
  • 4
  • 30
  • 41
  • Will it periodically pull? – Léon Pelletier Oct 26 '15 at 00:14
  • It monitors the queue logs and pulls a message off the queue as soon as one is added. – viperguynaz Oct 26 '15 at 00:17
  • @viperguynaz I've updated my question. I dont think that Web Job will helped me out with this one. – Ron Oct 26 '15 at 00:28
  • The WebJob SDK can be used anywhere - try it in a proof of concept. – viperguynaz Oct 26 '15 at 00:44
  • 5
    I need to point out that the WebJob SDK QueueTrigger is actually a wrapper around a polling mechanism that uses a back-off algorithm so it's not exactly event driven but just looks like it. This is used to save on queue reads (which are billable operations in azure) for low traffic queues. The max wait between polls is configurable [How to use Azure queue storage with the WebJobs SDK](https://learn.microsoft.com/en-us/azure/app-service-web/websites-dotnet-webjobs-sdk-storage-queues-how-to) – Aaron Krone Aug 11 '17 at 19:52
1

I eventually used QueueClient.ReceiveAsync In order to wait for message with a parameter of a Timespan.

            BrokeredMessage msg = subClient.ReceiveAsync(TimeSpan.FromMinutes(3));

Here is an nice article that explains large parts of the Azure Service Bus link and Service Bus best practice.

Ron
  • 1,744
  • 6
  • 27
  • 53