【问题标题】:Azure Service Bus Queue processing messages asynchronously in parallelAzure 服务总线队列以并行方式异步处理消息
【发布时间】:2015-12-23 18:16:31
【问题描述】:

我创建了一个在 Azure 服务总线队列上侦听的 Windows 窗体程序,它为每个接收到的 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 分钟后)。我想要的是并行处理这些消息。

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

【问题讨论】:

    标签: c# multithreading azure message-queue azure-servicebus-queues


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-11-01
      • 2019-07-23
      • 2013-05-22
      • 2020-01-31
      • 2018-03-04
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多