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.
Tuesday, December 11, 2012
Monday, December 10, 2012
Chaining Topics and Subscriptions
With the release of Windows Azure SDK 1.8 a new useful way of chaining queues or topics together is available. The ForwardTo method allows you to "connect" queues with eachother and implement scale out scenarios.
Consider a single Service Bus topic receiving purchase orders. After a while you notice that processing the orders by consumers takes more time than desired. To enable scale out scenarios where more consumers can take messages at a time you can choose to distribute messages to different topics based on e.g. region.
The code snippet below demonstrates how to use the initial topic scenario where messages are pushed to a subscription based on region. This snippet sends only one message to the EMEA region and then some sort of random based on Ticks. This snippet can be used to populate the Topic and its subscriptions.
string ServiceNamespace = "yournamespace";
TokenProvider credentials = TokenProvider.CreateSharedSecretTokenProvider("issuer",
"yourkey");
// Create namespace client
NamespaceManager namespaceClient = new NamespaceManager(
ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty), credentials);
TopicDescription poTopic = null;
if (!namespaceClient.TopicExists("PurchaseOrderTopic"))
{
poTopic = namespaceClient.CreateTopic("PurchaseOrderTopic");
}
else
{
poTopic = namespaceClient.GetTopic("PurchaseOrderTopic");
}
MessagingFactory factory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb",
ServiceNamespace, string.Empty), credentials);
TopicClient myTopicClient = factory.CreateTopicClient(poTopic.Path);
SqlFilter EmeaFilter = new SqlFilter("Region = 'EMEA'");
SqlFilter UsaFilter = new SqlFilter("Region = 'USA'");
if (!namespaceClient.SubscriptionExists(poTopic.Path, "EMEA"))
{
SubscriptionDescription myAgentSubscription = namespaceClient.CreateSubscription(poTopic.Path, "EMEA",
EmeaFilter);
}
if (!namespaceClient.SubscriptionExists(poTopic.Path, "USA"))
{
SubscriptionDescription myAuditSubscription = namespaceClient.CreateSubscription(poTopic.Path, "USA",
UsaFilter);
}
PurchaseOrder order = new PurchaseOrder()
{
Amount = 100,
Article = "Car",
Region = "EMEA"
};
BrokeredMessage POMessage = new BrokeredMessage(order);
POMessage.Properties["Region"] = order.Region;
Console.WriteLine("Sending initial message to EMEA");
myTopicClient.Send(POMessage);
while (Console.ReadLine() != "q")
{
POMessage = new BrokeredMessage(order);
if (DateTime.Now.Ticks % 2 == 0)
{
Console.WriteLine("Sending another message to EMEA");
POMessage.Properties["Region"] = "EMEA";
}
else
{
Console.WriteLine("Sending another message to USA");
POMessage.Properties["Region"] = "USA";
}
myTopicClient.Send(POMessage);
}
Pressing several times will add random messages.
The next blog post demonstrates how to redistribute messages based on their country by using Auto-forwarding feature of the Service Bus.
Consider a single Service Bus topic receiving purchase orders. After a while you notice that processing the orders by consumers takes more time than desired. To enable scale out scenarios where more consumers can take messages at a time you can choose to distribute messages to different topics based on e.g. region.
The code snippet below demonstrates how to use the initial topic scenario where messages are pushed to a subscription based on region. This snippet sends only one message to the EMEA region and then some sort of random based on Ticks. This snippet can be used to populate the Topic and its subscriptions.
string ServiceNamespace = "yournamespace";
TokenProvider credentials = TokenProvider.CreateSharedSecretTokenProvider("issuer",
"yourkey");
// Create namespace client
NamespaceManager namespaceClient = new NamespaceManager(
ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty), credentials);
TopicDescription poTopic = null;
if (!namespaceClient.TopicExists("PurchaseOrderTopic"))
{
poTopic = namespaceClient.CreateTopic("PurchaseOrderTopic");
}
else
{
poTopic = namespaceClient.GetTopic("PurchaseOrderTopic");
}
MessagingFactory factory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb",
ServiceNamespace, string.Empty), credentials);
TopicClient myTopicClient = factory.CreateTopicClient(poTopic.Path);
SqlFilter EmeaFilter = new SqlFilter("Region = 'EMEA'");
SqlFilter UsaFilter = new SqlFilter("Region = 'USA'");
if (!namespaceClient.SubscriptionExists(poTopic.Path, "EMEA"))
{
SubscriptionDescription myAgentSubscription = namespaceClient.CreateSubscription(poTopic.Path, "EMEA",
EmeaFilter);
}
if (!namespaceClient.SubscriptionExists(poTopic.Path, "USA"))
{
SubscriptionDescription myAuditSubscription = namespaceClient.CreateSubscription(poTopic.Path, "USA",
UsaFilter);
}
PurchaseOrder order = new PurchaseOrder()
{
Amount = 100,
Article = "Car",
Region = "EMEA"
};
BrokeredMessage POMessage = new BrokeredMessage(order);
POMessage.Properties["Region"] = order.Region;
Console.WriteLine("Sending initial message to EMEA");
myTopicClient.Send(POMessage);
while (Console.ReadLine() != "q")
{
POMessage = new BrokeredMessage(order);
if (DateTime.Now.Ticks % 2 == 0)
{
Console.WriteLine("Sending another message to EMEA");
POMessage.Properties["Region"] = "EMEA";
}
else
{
Console.WriteLine("Sending another message to USA");
POMessage.Properties["Region"] = "USA";
}
myTopicClient.Send(POMessage);
}
Pressing
The next blog post demonstrates how to redistribute messages based on their country by using Auto-forwarding feature of the Service Bus.
Thursday, December 6, 2012
Win A Free Copy of Packt's Windows Azure programming patterns for Start-ups e-book
We are pleased to announce that we have teamed up with Packt
Publishing and are organizing a give away especially for you. All you need to
do is just comment below the post and win a free copy of Windows Azure
programming patterns for Start-ups. Two lucky winners stand a chance to win an
e-copy of the book. Keep reading to find out how you can be one of the Lucky
One.
Overview of Windows
Azure programming patterns for Start-ups eBook
·
Explore the different features of Windows Azure and its unique
concepts.
·
Get to know the Windows Azure platform by code snippets and
samples by a single start-up scenario throughout the whole book.
·
A clean example scenario demonstrates the different Windows Azure
features.
How to
Enter?
Simply
post your expectations from this book in comments section below. You could be
one of the 2 lucky participants to win the e-copy.
DeadLine:
The contest will close on 21/12/2012 . Winners will be
contacted by email, so be sure to use your real email address when you comment!
Subscribe to:
Posts (Atom)