【问题标题】:Deleting a message from Azure Queue Service by value按值从 Azure 队列服务中删除消息
【发布时间】:2023-03-17 07:16:02
【问题描述】:

我正在使用 Azure 存储队列服务来跟踪由多个不同客户端排队的长时间运行的作业队列,但我现在需要从队列中删除任何符合某些给定条件的消息。

我意识到这对于队列来说有点反模式,但该服务确实提供了除了简单队列之外的一些功能(例如Delete MessagePeek Messages),所以我想我会尝试实现它。

我提出的解决方案可行,但不是很优雅而且效率很低 - 我想知道它是否可以做得更好 - 或者我是否应该将整个方法装箱并使用支持这个的机制设计要求(这将需要跨不同系统进行大量工作)。这是简化的代码:

        var queue = MethodThatGetsTheAppropriateQueueReference();
        await queue.FetchAttributesAsync(); //populates the current queue length
        if (queue.ApproximateMessageCount.HasValue)
        {
            // Get all messages and find any messages to be removed.
            //   this makes those messages unavailable for other clients 
            //   for the visibilityTimeOut period.
            //   I've set this to the minimum of 1 second - not ideal though
            var messages = await queue.GetMessagesAsync(queue.ApproximateMessageCount.Value);
            var messagesToDelete = messages.Where(x => x.AsString.Contains(someGuid));

            // Delete applicable messages
            messagesToDelete.ToList().ForEach(x => queue.DeleteMessageAsync(x));                
        }

注意最初我尝试使用PeekMessagesAsync() 来避免影响不需要删除的消息,但这并没有给您DeleteMessageAsync() 所需的PopReceipt

问题:

  1. 有没有办法做到这一点,而无需拉下 ALL 的消息? (可能有很多)
  2. 如果 1 不可能,如果我们使用 PeekMessagesAsync(),有没有办法获得 PopReceipt 的消息?

【问题讨论】:

    标签: c# azure azure-storage asp.net-core-1.1 azure-queues


    【解决方案1】:

    有没有办法做到这一点而不拉下所有的消息? (可能有很多)

    很遗憾,没有。您必须Get 消息(一次最多 32 条)并分析消息的内容以确定是否应该删除该消息。

    如果 1 不可能,有没有办法获取消息的 PopReceipt 如果我们使用 PeekMessagesAsync()?

    同样,不。为了获得PopReceipt,必须将消息出列,这只能通过GetMessagesAsync() 进行。 PeekMessagesAsync() 只是返回消息而不改变其可见性。

    可能的解决方案

    您可能需要查看Service Bus Topics and Subscriptions 以了解此类功能。

    您可以做的是创建一个topic,所有消息都将在其中发送。

    然后您将创建 2 subscriptions:在一个订阅中,您将设置一个规则来检查消息内容是否匹配值,而在另一个订阅中,您将设置一个规则来检查消息内容是否匹配值。

    Azure 服务总线会根据规则检查到达的每条消息,并相应地将消息推送到适当的订阅中。这样您就可以很好地区分应该/不应该删除的消息。

    【讨论】:

      猜你喜欢
      • 2017-01-16
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2020-01-08
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多