本文介绍了是否Azure的ServiceBus QueueClient.OnMessage执行不同的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问QueueClient.OnMessage方法始终执行不同的线程上的回调参数?

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

我认为,如果MaxConcurrentCalls设置为10则queueClient将旋转了一个最大的10个线程来并行处理中的消息。是否一个新的线程得到,如果你在1 MaxConcurrentConnection值传递或是否执行回当前线程上创建的?

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 });

请问这样_queueClient4回调不是等着_queueClient2完成,然后才能执行此结果每次回调被并行执行的?

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?

推荐答案

当您注册的onMessage 回调它被称为在不同的线程。如果设置 MaxConcurrentCalls 说10,那么我们将创建10个线程,10消息和回调所有这些为每条消息兼任。当你看到前面,但当然没有同步或排序每个回调处理之间做你可以在队列做到这一点。

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.

当我们调用一个新的线程回调,那么它是同步的该特定执行/消息直到方法完成或抛出一个异常,消息处理未完成。如果 MaxConcurrentCalls 为0,那么只有一个线程会为每个注册的回调处理。然而,当你有不同的QueueClient情况下,你可以注册不同的回调给他们或用不同的并发数等相同的回调

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.

这篇关于是否Azure的ServiceBus QueueClient.OnMessage执行不同的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 15:53