0
votes

I am working on an project that publish message to a service bus topic. I have two subscriptions on my topic. Is there a way to filter the message that my subscriber will listen?

Assuming this is my message text.

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

I only want to listen to a message that has a Male gender. Is this something configurable in Topic?

1

1 Answers

3
votes

Subscribers can define which messages they want to receive from a topic. You could refer to 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.

Following is the demo code:

Create subscription with Filter

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

Sample Message

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