Skip to main content
 首页 » 编程设计

c#之Microsoft ServiceBus 无序接收 BrokeredMessages

2024年02月13日12duanxz

我有一个 javascript 日志实用程序,它可以将请求批量发送到我的服务器,然后将它们转发到队列客户端 (Microsoft.ServiceBus.Messaging.QueueClient)。我想将它们异步批量发送到 ServiceBus,并且仍然按照它们放入我发送的批处理中的顺序进行处理。 SendBatchAsync 的文档显示该方法用于“批处理”处理。这让我觉得我可以向它发送一批请求并将它们作为一个单元处理(即:按顺序)。虽然,消息似乎正在乱序处理。我正在使用 OnMessage 来接收消息;我不确定这是限制还是我错过了什么?

我知道异步不能保证与其他异步请求相比的顺序,但这是单个请求。我不想在响应 javascript 客户端之前等待响应,因为我只是想将它们发送出去,但我仍然需要确保它们保持有序,因为它们是顺序事件。

这是我将它们发送到队列的方式:

MyQueueClient.SendBatchAsync(MyListOfBrokerMessages); 

然后我处理它们:

        ServiceBus.TrackerClient.OnMessage((m) => 
        { 
            try 
            { 
                ProcessMessage(m); 
            } 

如果除了可能发出单个请求之外不作为批处理,我不明白批处理的意义。一定有某种方法可以发送一批并按顺序处理它??

编辑: 我已尝试使用 Send 而不是 SendBatchAsync 并且我已将 MaxConcurrentCalls 设置为 1,但消息仍然不按顺序。

请您参考如下方法:

取自MSDN:

SessionId: If a message has the Microsoft.ServiceBus.Messaging.BrokeredMessage.SessionId property set, then Service Bus uses the SessionId property as the partition key. This way, all messages that belong to the same session are handled by the same message broker. This enables Service Bus to guarantee message ordering as well as the consistency of session states.

对于使用 SessionId 和 AcceptSessionReceiver 的编码示例 see.