Azure 服务总线队列异步并行处理消息

Azure Service Bus Queue processing messages asynchronously in parallel

我创建了一个 windows 表单程序来侦听 Azure 服务总线队列,它为每个接收到的 brokeredMessage 执行一个很长的过程:

QueueClient Client = QueueClient.CreateFromConnectionString(ConfigurationWrapper.QueueConnectionString, ConfigurationWrapper.QueueName);

            // Configure the callback options
            OnMessageOptions options = new OnMessageOptions();
            options.AutoComplete = false;
            options.AutoRenewTimeout = TimeSpan.FromMinutes(1);


            // Callback to handle received messages
            Client.OnMessageAsync((message) =>
            {
                  message.Complete();
                //big jobs put 10 minutes
               Task t = Task.Factory.StartNew(() => DoAVeryLongJob(message));

               return t;
            }, options);

如果我在此队列中以 2 秒的间隔发送 2 条消息,程序处理第一条消息(调用 DoAVeryLongJob 需要 10 分钟),第二条消息将在第一次调用结束时处理(10分钟后)。我想要的是并行处理这些消息。

是否可以并行处理队列消息?

在您的 OnMessageOptions 实例中,您需要增加 MaxConcurrentCalls。以下代码将并行处理队列中的 5 条消息。

// Configure the callback options
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.MaxConcurrentCalls = 5;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);