Does Azure ServiceBus QueueClient.OnMessage execute on a different thread

冷暖自知 提交于 2019-12-04 08:16:58

问题


Does the QueueClient.OnMessage method always execute the callback parameter on a different thread?

I assume that if MaxConcurrentCalls is set to 10 then the queueClient would spin up a maximum of 10 threads to process the messages in parallel. Does a new thread get created if you pass in a MaxConcurrentConnection value of 1 or does it execute back on the current thread?

My actual problem:
Within a Worker Role I would like to handle multiple queues but have them all process concurrently. E.g.

        _queueClient1.OnMessage(x =>
            {
                // Do something
            }, new OnMessageOptions { MaxConcurrentCalls = 1});

        _queueClient2.OnMessage(x =>
        {
            // Do something
        }, new OnMessageOptions { MaxConcurrentCalls = 1 });

        _queueClient3.OnMessage(x =>
        {
            // Do something
        }, new OnMessageOptions { MaxConcurrentCalls = 1 });

        _queueClient4.OnMessage(x =>
        {
            // Do something
        }, new OnMessageOptions { MaxConcurrentCalls = 1 });

Would this result in each callback being executed in parallel so that the _queueClient4 callback isn't waiting for _queueClient2 to finish before it can execute?


回答1:


When you register a callback with OnMessage it is called on a different thread. If you set MaxConcurrentCalls to say 10, then we will create 10 threads for 10 messages and callback all of these for each message concurrently. You can do this across Queues too as you show above but of course there is no synchronization or ordering done between each individual callback processing.

When we invoke the callback on a new thread, then it is synchronous for that particular execution/message in that until the method completes or an exception is thrown, the message processing is not completed. If MaxConcurrentCalls is 0 then only a single thread will be processed for each of the callback registered. However when you have different QueueClient instances you can register different callbacks to them or the same callback with different concurrent counts etc.



来源:https://stackoverflow.com/questions/18255323/does-azure-servicebus-queueclient-onmessage-execute-on-a-different-thread

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