Tuesday, December 11, 2012

Chaining with Service Bus part 2

In the previous post I demonstrated how to send messages to a topic and how to add subscriptions receiving messages based on a region. The post will demonstrate how to scale out your system by using chaining and enabling messages to be forwarded from the region subscription to a specific country subscription. That post described how messages send to a topic are picked up by subscriptions based on a property of the message (in this case a region like USA or EMEA).

This post will demonstrate how to further distribute messages by another property called Country. To enable this we will use a technique called auto-forwarding. Consider the following:

- different systems insert purchase orders in our system. The purchase order is reflected by the following simplified class:


    [DataContract]
    public class PurchaseOrder
    {
        [DataMember]
        public string Region;
        [DataMember]
        public decimal Amount;
        [DataMember]
        public string Article;
        [DataMember]
        public string Country;
    }


Auto-forwarding enables a subscription to be "chained" to another topic or queue. The scenario we are realizing here is to have messages that are added to our EMEA subscription or forwarded to topics based on country e.g. Holland and Germany. Messages that are forwarded are automatically removed from the subscription (in our case, the EMEA subscription) and placed in the designated topic. See the code snippet below.


            //FORWARDING
            SubscriptionDescription description = new SubscriptionDescription(poTopic.Path, "Holland");
            description.ForwardTo = "Holland";
            SqlFilter filter = new SqlFilter("Country = 'Holland'");
            if (!namespaceClient.SubscriptionExists(poTopic.Path, "Holland"))
            {
                namespaceClient.CreateSubscription(description, filter);
            }
            //FORWARDING


This piece of code enables messages being send to the EMEA topic (as before) with another property called  Country with the value 'Holland' to be forwarded to a specific topic called 'Holland' for further processing.

By using forwarding you can scale out your load on the initial topic and distribute messages based on some property. In the figure below you can see that no messages appear anymore in the EMEA topic but they all show up in the Holland Topic which is accomplished by the code snippet above.

















No comments:

Post a Comment