示例筛选策略 - Amazon Simple Notification Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

示例筛选策略

下面的示例演示处理客户事务的 Amazon SNS 主题所发送的消息有效负载。

第一个示例包括 MessageAttributes 字段,其中包含描述事务的属性:

  • 客户的兴趣

  • 存储名称

  • 事件状态

  • 购买价格 (USD)

由于此消息包含 MessageAttributes 字段,只要在订阅中将 FilterPolicyScope 设置为 MessageAttributes,任何设置了 FilterPolicy 的主题订阅都可以选择性地接受或拒绝消息。有关将属性应用于消息的信息,请参阅 Amazon SNS 消息属性

{ "Type": "Notification", "MessageId": "a1b2c34d-567e-8f90-g1h2-i345j67klmn8", "TopicArn": "arn:aws:sns:us-east-2:123456789012:MyTopic", "Message": "message-body-with-transaction-details", "Timestamp": "2019-11-03T23:28:01.631Z", "SignatureVersion": "4", "Signature": "signature", "UnsubscribeURL": "unsubscribe-url", "MessageAttributes": { "customer_interests": { "Type": "String.Array", "Value": "[\"soccer\", \"rugby\", \"hockey\"]" }, "store": { "Type": "String", "Value":"example_corp" }, "event": { "Type": "String", "Value": "order_placed" }, "price_usd": { "Type": "Number", "Value": "210.75" } } }

以下示例显示了 Message 字段中包含的相同属性,也称为消息有效负载消息正文。只要在订阅中将 MessageBody 设置为 FilterPolicyScope,任何设置了 FilterPolicy 的主题订阅都可以选择性地接受或拒绝消息。

{ "Type": "Notification", "MessageId": "a1b2c34d-567e-8f90-g1h2-i345j67klmn8", "TopicArn": "arn:aws:sns:us-east-2:123456789012:MyTopic", "Message": "{ \"customer_interests\": [\"soccer\", \"rugby\", \"hockey\"], \"store\": \"example_corp\", \"event\":\"order_placed\", \"price_usd\":210.75 }", "Timestamp": "2019-11-03T23:28:01.631Z", "SignatureVersion": "4", "Signature": "signature", "UnsubscribeURL": "unsubscribe-url" }

以下筛选策略基于消息的属性名称和值接受或拒绝消息。

接受示例消息的策略

以下订阅筛选策略中的属性与分配给示例消息的属性匹配。请注意,无论设置为 MessageAttributes 还是 MessageBody,相同的筛选策略都适用于 FilterPolicyScope。每个订阅者根据他们从主题收到的消息的构成来选择其筛选范围。

如果此策略中的任一属性与分配给该消息的属性不匹配,则此策略将拒绝该消息。

{ "store": ["example_corp"], "event": [{"anything-but": "order_cancelled"}], "customer_interests": [ "rugby", "football", "baseball" ], "price_usd": [{"numeric": [">=", 100]}] }

拒绝示例消息的策略

以下订阅筛选策略在其属性与分配给示例消息的属性之间存在多个不匹配项。例如,由于消息属性中不存在 encrypted 属性名称,因此该策略属性会导致消息被拒绝,而不管分配给它的值如何。

如果存在任何不匹配项,则策略将拒绝消息。

{ "store": ["example_corp"], "event": ["order_cancelled"], "encrypted": [false], "customer_interests": [ "basketball", "baseball" ] }