如何禁用特定队列项的 Azure webjob 存储队列处理?

How can I disable Azure webjob storage queue processing for specific queue items?

我有一个队列触发的 Azure webjob 函数:

public void ProcessQueueMessage([QueueTrigger("%MyQueue%")] Item item, TextWriter logger)
    {
       // Do normal processing...
    }

我想根据条件有选择地忽略某些队列项目。例如,如果我没有 Widget X,并且我不想在有库存之前继续处理 widget 发货。所以我想暂时 ignore/skip 所有 Widget X 队列项目。所以像:

public void ProcessQueueMessage([QueueTrigger("%MyQueue%")] Item item, TextWriter logger)
{
    if (!HaveStock(WidgetX))
       // Ignore queue item - treat it as if it was not in the queue at all

    // Do normal processing...
}

抛出异常以使处理失败对我来说似乎不是一个选项,因为它会毒化消息。

我可以将 VisibilityTimeout 设置为某个任意高的值,然后抛出异常但是当我想执行处理时(而不是仅仅等待超时)我必须有一个单独的进程扫描将具有较高 VisibilityTimeout 的项目排队,并将超时设置为较低的值(甚至不确定这是否可能)。但是,我首先要避免的是扫描这些队列项的过程,因为我想尽可能将逻辑保留在队列触发的函数中。

我有什么选择?

更新:我认识到我试图完成的事情首先与队列触发函数的目的不一致,因为队列触发事件的原因是在消息出现时做某事,并且如果我想要选择性处理,我可以自己实现队列轮询和消息处理过程。但我正在尝试利用触发处理提供的更高级别的抽象。

我想说;设Queue为Queue,设FIFO为FIFO。

如果你想用队列处理条件消息处理,我建议两个选项。

1.使用 Azure 服务总线主题

对于选择性 FIFO 过滤器,可以将 Azure 队列替换为 Azure 服务总线主题。

查看教程以获得提示: https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-service-bus/#topics

2。使用辅助队列

类似于中毒队列,将被忽略的队列发送到辅助队列,稍后以另一种逻辑处理它们。我推荐这个,因为我们可以很容易地理解流程。

CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
_secondaryQueue= queueClient.GetQueueReference(storageQueueName);
_secondaryQueue.CreateIfNotExists();

public static void ProcessQueueMessage([QueueTrigger("%MyQueue%")] Item item, TextWriter logger)
{
    // send to secondary queue.
    await _secondaryQueue.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(item)));
}