问题
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.
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/33336535/listen-to-queue-event-driven-no-polling-service-bus-storage-queue