I'm working with this code for receiving messages from a queue:
function startReceiver(){ serviceBusService.getQueue(configurations.queueForRequest, function(err, queue){ if(!err){ var length = queue.CountDetails['d2p1:ActiveMessageCount']; if(length > 0) {
serviceBusService.receiveQueueMessage(configurations.queueForRequest, {isPeekLock:true}, function(error, lockedMessage){HandleMessage(error, lockedMessage)}); return; } } else{ console.log('Can not get queue'); } setTimeout(startReceiver, 3000);//if err or there are no messages then call this method later }); } function handleMessage(err, msg){ var result; if (!err){ serviceBusService.deleteMessage(msg, function(deleteError){ if(deleteError) { console.log('Can not delete a message'); } else{ console.log('Msg has been deleted'); } }); try{ result = GetResult(msg.body) } catch (er){ result = GetResultWhenExp(); } finally{ sendMessage(result); //send a response startReceiver(); //repeat a receiver loop } }//!error else{console.log('Error occured: '+err); setTimeout(startReceiver, 3000); //repeat a receiver loop later } }
The issue is I can receive a message when handleMessage() is running the first time only. Further startReceiver() can get the correct count of active messages, but handleMessage() gets undefined msg as argument always (i.e. serviceBusService.receiveQueueMessage() fails, err is "No messages to receive").
Using C# library with its standard fucnctions for receiving messages it works great.
What's wrong here? Please help