3
votes

I am using service bus queues to communicate between web role and worker role. I have two queues in my service bus. I am putting a message into the queue from web role and the worker role processes that message and puts another message back into another queue which the web role retrieves and sends to client. Both the web role and worker role threads are continuously running

I want to know when the execution control is at QueueClient.Receive, does it wait till it receives the message or if there is no message on the queue then it moves to next line? because in the situation when there is no message on the queue, i had put a breakpoint at QueueClient.Receive to check what happens next but the control doesn't go to next line it just disappears so i thought it might just be waiting.

But in my web role below i am sending a message to worker role through service bus queue and am expecting a response again from worker role immediately but sometimes worker role takes a bit of processing time and the web role doesn't get it and the web role thread goes to sleep.

I am a beginner in windows azure, so am a bit confused about this whole problem. Can anyone please help me out?

My worker 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 Web Role:

// 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
}   
1

1 Answers

3
votes

With the default method, a call to QueueClient.Receive will return immediately. You will get either a NULL value if there was no message in the queue at that particular time, or an instance of BrokeredMessage if there was a message.

QueueClient.Receive have 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 aren't any messages. This is useful to avoid having to reconnect multiple times until you get a message.

For the case you mention, you should try and use the QueueClient.Receive(timespan) version of the API, so the thread will wait for a longer time to receive the message. Another option would be to put your message receiving logic in a loop, and break until you get a message.