How does the QueueClient.Receive Service Bus work?

I am using Service Bus queues to communicate between the web role and the user role. I have two queues on my Service Bus. I put a message on a queue from a web role, and a worker role processes that message and puts another message back into another queue that the web role receives and sends to the client. And the web role and worker role flows are constantly running

I want to know when the execution control is in QueueClient.Receive, is it waiting for a message or no message in the queue and then moving on to the next line? because in a situation where there is no message in the queue, I put a breakpoint in the QueueClient.Receive to check what happens next, but the control does not advance to the next line, it just disappears, so I thought it might just wait.

But in my network role below, I send a message to the employee role through the Service Bus queue and expect a response from the worker role immediately, but sometimes the worker role takes a little processing time and the web role does not receive and the role thread on the network goes to sleep.

I'm new to windows azure so I'm a little confused about this issue. Can anyone help me?

My work role:

// Receive the message from Web Role
BrokeredMessage receivedBroadcastMessage = null;
receivedBroadcastMessage = WebRoleClient.Receive();

if (receivedBroadcastMessage != null)
{
    // Process the message

    receivedBroadcastMessage.Complete();
}

BrokeredMessage webRoleMessage = new BrokeredMessage(processedMessage);
WorkerRoleClient.Send(webRoleMessage);

      

My role on the Internet:

// send the request to worker role
BrokeredMessage webRoleMessage = new BrokeredMessage(details);
WebRoleClient.Send(webRoleMessage);

// receive the queue from worker role
BrokeredMessage workerRoleMessage = null;
workerRoleMessage = WorkerRoleClient.Receive();

if (workerRoleMessage != null)
{ 
    //process message

    workerRoleMessage.Complete();
}
else
{
    // sleep thread
}   

      

+3


source to share


1 answer


With the default method, the call to QueueClient.Receive will return immediately. You will receive either NULL if there was no message in the queue at the specified time, or a BrokeredMessage instance if there was a message.

QueueClient.Receive has a couple of overrides where you can specify a timeout. In this case, the method will wait until the timeout expires to return from the call if there are no messages. This is useful to avoid reconnecting multiple times until you receive the message.



In this case, you should try using the QueueClient.Receive (timespan) version of the API, so the thread will wait longer to receive the message. Another option would be to put your message, getting the logic in a loop, and break through until you get the message.

+3


source







All Articles