服务总线主题订阅过滤器

Service bus Topic subscription filter

我正在开发一个将消息发布到服务总线主题的项目。我的主题有两个订阅。有没有办法过滤我的订阅者将收听的消息?

假设这是我的消息文本。

{
"Data" : {
   "Name": "Johnny Cruz"
   "Gender": "Male"
 }
}

我只想听一条男性留言。这是可以在主题中配置的东西吗?

订阅者可以定义他们希望从某个主题接收哪些消息。你可以参考Topic filters and actions.

Service Bus supports three filter conditions:

  • Boolean filters - The TrueFilter and FalseFilter either cause all arriving messages (true) or none of the arriving messages (false) to be selected for the subscription.

  • SQL Filters - A SqlFilter holds a SQL-like conditional expression that is evaluated in the broker against the arriving messages' user-defined properties and system properties. All system properties must be prefixed with sys. in the conditional expression. The SQL-language subset for filter conditions tests for the existence of properties (EXISTS), as well as for null-values (IS NULL), logical NOT/AND/OR, relational operators, simple numeric arithmetic, and simple text pattern matching with LIKE.

  • Correlation Filters - A CorrelationFilter holds a set of conditions that are matched against one or more of an arriving message's user and system properties. A common use is to match against the CorrelationId property, but the application can also choose to match against ContentType, Label, MessageId, ReplyTo, ReplyToSessionId, SessionId, To, and any user-defined properties. A match exists when an arriving message's value for a property is equal to the value specified in the correlation filter. For string expressions, the comparison is case-sensitive. When specifying multiple match properties, the filter combines them as a logical AND condition, meaning for the filter to match, all conditions must match.

下面是演示代码:

使用过滤器创建订阅

var filter=new SqlFilter("(sys.Label='Male');
namespaceManager.CreateSubscription(topicName, subName,filter);

示例消息

var message = new BrokeredMessage(body);
var message.Label = "Male";// or Female