Filtered Subscription Example

Jul 30, 2013 at 5:21 AM
Now Wsp supports filtered subscriptions. When you subscribe to an event type, you can now optionally provide a filter. The filter is C# code. The code is provided as a string. The method which the code will be compiled within is:
    public bool FilterMethod(WspEvent wspEvent)
        (filter code)
An example of what a filter could be is:
   "string s; if (wspEvent.Headers.TryGetValue(100, out s) == true) { if ((s.GetHashCode() % 3) == 0) return true; } return false;"
This example gets a header value, calculates the modulo of its hash code, and subscribes when the modulo equals 0. If you wanted to create 10 partitions, you could use a modulo using 10 with each of the 10 subscribers providing slightly different C# code referring to one of the partitions.

Though the filter code could deserialize the body of the event, it would not be recommended. Instead, you should consider copying any data you may want to filter on user-defined headers. This will be orders of magnitude more efficient.

With filtered subscriptions, Wsp can now do content-based routing of events.