Azure ServiceBus:无法从队列接收消息 [英] Azure ServiceBus: Can not receive messages from queue
问题描述
我正在使用以下代码从队列接收消息:
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'];//get count of active messages
if(length > 0) {
serviceBusService.receiveQueueMessage(configurations.queueForRequest, {isPeekLock:true},
function(error, lockedMessage){ HandleMessage(error, lockedMessage) });
return; //get out from this method
}
}
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 the message')
}
else{
console.log('Msg has been deleted');
}
});//delete the message which has been received
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
}
}
问题是,仅当handleMessage()第一次运行时,我才能收到一条消息.进一步的startReceiver()可以正确获取活动消息的数量,但是handleMessage()始终将未定义的msg作为参数(即serviceBusService.receiveQueueMessage()失败,错误是没有消息要接收").
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").
使用具有标准功能的C#库来接收消息,效果很好.
Using C# library with its standard fucnctions for receiving messages it works great.
这是怎么了?请帮助
I just copy-paste this example from azure-sdk-for-node repository and in my case it yields same behavior: first message has been received succesfully, however following requests to Bus returns "No messages to receive"
推荐答案
当前,我可以提出您的问题,并在与Peek-Lock
模式相关的某种复杂情况下检测到它应该是性能问题.从队列接收消息.
Currently, I can produce your issue, and detect that it should be the performance issue in some kind of a complicated scenario which relates to the Peek-Lock
mode to receive messages from queue.
就像在代码片段中一样,您使用Peek-Lock
模式来接收消息,这将锁定队列的第一条消息(因为队列传递消息FIFO),因为handleMessage()
函数中的deleteMessage()
是异步的函数,因此node.js不会在此结果上处于待决状态,它将立即在finally
部分中调用startReceiver()
.在这种情况下,它可能会尝试接收锁定的消息并出现此问题.
As in your code snippet, you use Peek-Lock
mode to receive messages, which will lock the first message of the queue (as queue delivers message FIFO), as the deleteMessage()
in your handleMessage()
function is an asyn function, so node.js will not pending on for this result, will call startReceiver()
in finally
section immediately. In which case it may try to receive a locked message and arise this issue.
您可以尝试两种操作来解决此问题:
There are two operations you can try to fix this issue:
- 增大接收处理器的间隔时间,尝试将
finally
部分下的startReceiver()
修改为setTimeout(startReceiver, 3000);
- 尝试使用
read and delete
接收模式,使用receiveQueueMessage(quene_name,callback)
代替receiveQueueMessage(quene_name,{ isPeekLock: true },callback)
- Enlarge the receive processor's interval time, try to modify
startReceiver()
underfinally
section tosetTimeout(startReceiver, 3000);
- Try to use
read and delete
receive mode, usereceiveQueueMessage(quene_name,callback)
instead ofreceiveQueueMessage(quene_name,{ isPeekLock: true },callback)
这篇关于Azure ServiceBus:无法从队列接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!