azure - POCO 属性上的触发过滤器

标签 azure azure-webjobs azure-webjobssdk

我正在尝试创建一个由每个步骤的 QueueTrigger 处理的多步骤流程。如何在每个状态都有一个队列的情况下过滤 QueueMessage 对象的“状态”属性? 消息类型为:

public partial class TaskType{
    string BlobPathToProcess {get; set;}
    int State {get; set;}
}


 public static void Task(
     [QueueTrigger(queueName: "taskq")] TaskType msg
     TextWriter log,
     IBinder binder)
{
     //currently I use a switch statement on (TaskType.State == 1)

所以不是:

[QueueTrigger(queueName: "taskqstate1")] TaskType msg

[QueueTrigger(queueName: "taskqstate2")] TaskType msg

等等

也许像@pranav-rastogi关于 Singleton attribute 说上MSDN Ch9 cloud cover at min 24

[Singleton(@"{Region}\{Zone}"]
public static void Task([QueueTrigger(singleto-test")] WorkItem workItem, ...

其中 {Region} 和 {Zone} 是“workItem”对象的属性。

更像是:

[PocoFilter(PocoProperty="{State}", PocoValue="1"]
public static void Task1([QueueTrigger(queueName: "taskq")] TaskType msg ...

[PocoFilter(PocoProperty="{State}", PocoValue="2"]
public static void Task2([QueueTrigger(queueName: "taskq")] TaskType msg ...

最佳答案

据我所知,Azure Queue 没有消息过滤功能。

如果您迁移到 Azure 服务总线主题和订阅,您将能够根据消息属性进行筛选。

要创建主题和订阅,您可以查看这篇文章:

所以可以说,你已经创建了

  • 名为“MyTestTopic”的主题
  • 名称为“MySubscription1”且 SqlFilter 的订阅“状态 = 1”

    • 只有发送到属性 Status = 1 的主题的消息才会路由到此订阅。
  • 名为“MySubscription2”且 SqlFilter“Status = 2”的订阅

    • 只有发送到属性 Status = 2 的主题的消息才会路由到此订阅。

在你的网络作业中,你可以有两个函数,如下所示:

public static void ProcessQueueMessage1(
    [ServiceBusTrigger("MyTestTopic", "MySubscription1")] BrokeredMessage incomingMessage,
    ServiceBus("MyTestTopic")] out BrokeredMessage outputMessage)
{
    // Status should be 1
    Console.Out.WriteLine(incomingMessage.Properties["Status"]);

    // Get your poco
    var myPoco = incomingMessage.GetBody<TaskType>();

   //Process your message ...
   ....

   // clone the message
   outputMessage = incomingMessage.Clone();

   // Set the status to 2
   outputMessage.Properties["Status"] = 2
}

public static void ProcessQueueMessage2(
    [ServiceBusTrigger("MyTestTopic", "MySubscription2")] BrokeredMessage incomingMessage)
{
    // Status should be 2
    Console.Out.WriteLine(incomingMessage.Properties["Status"]);

    // Do what you need
}

在第一个方法中,我们使用 ServiceBusTriggerAttribute 来监听传入订阅 1 的消息,并使用 ServiceBusAttribute 将消息发送到主题。

当您想要发送消息时,请不要忘记设置 BrokeredMessage 的“Status”属性。

关于azure - POCO 属性上的触发过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36466128/

相关文章:

.net - 使用新库 (Microsoft.Azure.ServiceBus) 发送一条消息,该消息由旧库 (Microsoft.ServiceBus.Messaging) 使用 BodyType - String 读取

azure - 连接 Azure CDN 规则变量

python - 有没有办法使用 'requests' 通过 python 调用 Azure Devops ?

c# - 在应用程序洞察中禁用来自 Web 应用程序的默认跟踪日志消息

azure - 如何调用azure web作业并传递参数

php - Azure:如何正确使用 PHP Webjob 更改 SQL 数据库?

c# - Azure 机器学习 Web 服务不使用传递的 .ilearn 模型

c# - 拦截 Azure 函数主机关闭 : Flush Application Insights TelemetryClient

azure - 调试 Azure Web App Web 作业

azure - 在队列触发的 Azure Webjobs 中,是否可以在 Webjob 功能失败之后但中毒之前修改 Azure 存储队列消息?