在Azure功能中按顺序处理服务总线消息(无并发调用) [英] Processing Service Bus messages in order (without concurrent calls) in an Azure Function

查看:103
本文介绍了在Azure功能中按顺序处理服务总线消息(无并发调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过"Azure函数"读取和处理来自Azure Service Bus队列的消息.消息应该以正确的顺序处理,所以我需要避免并发调用.

I need to read and process messages from an Azure Service Bus Queue by an "Azure function". The messages should be handled in the right order so I need to avoid concurrent calls.

为此,我使用Azure Function服务总线触发器(它是队列的唯一订阅者).根据文档,我将"servicebus/maxConcurrentCalls"(在host.json中)配置为1.在此之上,我用"Singleton"属性装饰了该函数. 除了所有这些,这些消息似乎被不同的线程以随机的顺序对待. 我在这里想念什么?还是我误会了什么?

I use an Azure Function service bus trigger for this (it's the only subscriber to the queue). According to the documentation I configured the "servicebus/maxConcurrentCalls" (in host.json) setting to 1. On top of this I decorated the function with the "Singleton" attribute. Besides all this the messages seem to be treated in a random order by different threads. What do I miss here? Or did I misunderstand something?

我使用的文档: https://github.com/Azure/azure-webjobs-sdk/wiki/辛格尔顿

host.json:

host.json:

{
  "serviceBus": {
    "maxConcurrentCalls": 1
  }
}

天蓝色功能:

using System;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;

[Singleton]
public static void Run(BrokeredMessage myQueueItem, TraceWriter log)
{
    Stream stream = myQueueItem.GetBody<Stream>();
    StreamReader reader = new StreamReader(stream);
    string messageContentStr = reader.ReadToEnd();

    log.Info($"New TEST message: {messageContentStr} on thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");   

    System.Threading.Thread.Sleep(2000);     
}

这是日志的摘录.如您所见,有不同的线程. 并且,例如,消息19"在消息10"之前.是的,我确定我将消息按正确的顺序放入队列中.

Here is an excerpt of the logging. As you can see there are different threads. And, for example, "Message 19" comes before "Message 10". And yes, I'm sure I put the messages in the right order in the queue.

....
2018-05-09T09:09:33.686 [Info] New TEST message: Message 19 on thread 33
2018-05-09T09:09:35.702 [Info] Function completed (Success, Id=007eccd0-b5db-466a-91c1-4f53ec5a7b3a, Duration=2013ms)
2018-05-09T09:09:36.390 [Info] Function started (Id=b7160487-d10d-47a6-bab3-78da68a93498)
2018-05-09T09:09:36.420 [Info] New TEST message: Message 10 on thread 39
...

推荐答案

看看并确保您的服务总线队列未分区.如果已分区,则您有多个消息代理服务请求,并且不能保证消息传递的顺序.您可以在此处了解更多信息:

Take a look and make sure that your Service Bus Queue is NOT partitioned. If it is partitioned, you have multiple message brokers servicing requests, and the order of messaging is not guaranteed. You can read more about it here: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-partitioning#not-using-a-partition-key

特别是:

在没有分区键的情况下,服务总线将消息分发到 对分区队列的所有片段采用循环方式,或者 话题.如果选择的片段不可用,则服务总线分配 邮件发送到另一个片段.这样,发送操作 尽管消息传递存储暂时不可用,但仍成功. 但是,您将无法实现分区的保证顺序 密钥提供.

In the absence of a partition key, Service Bus distributes messages in a round-robin fashion to all the fragments of the partitioned queue or topic. If the chosen fragment is not available, Service Bus assigns the message to a different fragment. This way, the send operation succeeds despite the temporary unavailability of a messaging store. However, you will not achieve the guaranteed ordering that a partition key provides.

这篇关于在Azure功能中按顺序处理服务总线消息(无并发调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆