Azure Worker: Read a message from the Azure queue in a mutex way

泪湿孤枕 提交于 2019-12-07 13:59:41

问题


The run method of my worker role is:

public override void Run()
{
    Message msg=null;
    while (true)
    {

        msg = queue.GetMessage();
        if(msg!=null && msg.DequeueCount==1){
             //delete message
             ...
             //execute operations
             ...
        }
        else if(msg!=null && msg.DequeueCount>1){
             //delete message
             ...
        }
        else{
             int randomTime = ...
             Thread.Sleep(randomTime);                 
        }
    }
}

For performance tests I would that a message could be analysed only by a worker (I don't consider failure problems on workers).

But seems by my tests, that two workers can pick up the same message and read DequeueCount equals to 1 (both workers). Is it possible?

Does exist a way that allow just a worker to read a message in a "mutex" way?


回答1:


How is your "getAMessage(queue)" method defined? If you do PeekMessage(), a message will be visible by all workers. If you do GetMessage(), the message will be got only by the worker which firsts get it. But for the invisibility timeout either specified or the default (30 sec.). You have to delete the message before the invisibility timeout comes.

Check out the Queue Service API for more information. I am sure that there is something wrong in your code. I use queues and they behave as by documentation in dev storage and in production storage. You may want to explicitly put higher value of the Visibility Timeout when you do GetMessage. And make sure you do not sleep longer than the visibility timeout.



来源:https://stackoverflow.com/questions/9127191/azure-worker-read-a-message-from-the-azure-queue-in-a-mutex-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!