Bhavya

Code -> Break -> Fix -> Blog

Implementing Recipient List Pattern using Windows Azure Service Bus

Leave a comment

Continuing the implementation of Message Routing Pattern, next inline is Recipient List Pattern (You can also visit my previous post for Dynamic Router Pattern)

Again as posted by Stephen Kaufman,

This pattern is related to the Content Based Router pattern in that the CBR pattern routes messages based on the message content.  The Recipient List pattern provides a means for the message to contain a list of one or more recipients to receive the message.  This differs from CBR in that we may want to submit a message to multiple order fulfillment suppliers in order to receive back prices thus allowing for the comparison of responses and the selection of the cheapest supplier.

Below is the implementation:

Person Class:


/// <summary>
/// Class Person
/// </summary>
[Serializable]
public class Person
{
/// <summary>
/// Initializes a new instance of the <see cref="Person" /> class.
/// </summary>
public Person()
{
}

/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
public string Name
{
get;
set;
}

/// <summary>
/// Gets or sets the hobbies.
/// comma separated hobby names.
/// </summary>
/// <value>The hobbies.</value>
public string Hobbies
{
get;
set;
}

/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return Name;
}
}

Fields:


static string IssueName = string.Empty;
static string IssueKey = string.Empty;
static string ServicebusNamespace = string.Empty;

static NamespaceManager namespaceManager;
static MessagingFactory messagingFactory;
static TopicDescription topicDescription;

Utilities:


static TopicDescription CreateTopic(string topicName)
{
TopicDescription topicDescription = new TopicDescription(topicName);
topicDescription.EnableFilteringMessagesBeforePublishing = true;

//Check if already exists.
if (namespaceManager.TopicExists(topicDescription.Path))
{
namespaceManager.DeleteTopic(topicDescription.Path);
}

Console.WriteLine("Creating Topic : " + topicName);
return namespaceManager.CreateTopic(topicDescription);
}

static void CreateSubscription(Dictionary<string, string> subscription)
{

if (null != topicDescription)
{
foreach (KeyValuePair<string, string> item in subscription)
{
if (string.IsNullOrEmpty(item.Value))
{
namespaceManager.CreateSubscription(topicDescription.Path, item.Key);
}
else
{
namespaceManager.CreateSubscription(topicDescription.Path, item.Key, new SqlFilter(item.Value));
}
}
}
}

static void SendMessage(Person message, KeyValuePair<string, object> item)
{
TopicClient topicClient = messagingFactory.CreateTopicClient(topicDescription.Path);

BrokeredMessage brokeredMessage = new BrokeredMessage(message);

if (null != item.Key && null != item.Value)
{
brokeredMessage.Properties.Add(item);
}

topicClient.Send(brokeredMessage);
}

static Person ReceiveMessage(string subscriptionName)
{
SubscriptionClient subscriptionClient = messagingFactory.CreateSubscriptionClient(topicDescription.Path, subscriptionName, ReceiveMode.ReceiveAndDelete);

BrokeredMessage receivedMessage = subscriptionClient.Receive(new TimeSpan(0, 0, 3));

return (null != receivedMessage) ? receivedMessage.GetBody<Person>() : null;
}

Main implementation:


static void Main(string[] args)
{
#region User details check
if (string.IsNullOrEmpty(IssueName) || string.IsNullOrEmpty(IssueKey) || string.IsNullOrEmpty(ServicebusNamespace))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("One of the below 3 values are missing...");
Console.WriteLine("ServicebusNamespace");
Console.WriteLine("IssuerName");
Console.WriteLine("IssuerKey");
Console.ResetColor();
Console.WriteLine("\nPress any key to receive the message...");
Console.ReadKey();
return;
}
#endregion

#region Create Topic & Subscription
Uri serviceBusUri = ServiceBusEnvironment.CreateServiceUri("sb", ServicebusNamespace, string.Empty);
TokenProvider tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(IssueName, IssueKey);

namespaceManager = new NamespaceManager(serviceBusUri, tokenProvider);
messagingFactory = MessagingFactory.Create(serviceBusUri, tokenProvider);

topicDescription = CreateTopic("RecipientListTopic");

Dictionary<string, string> subscriptionItems = new Dictionary<string, string>();
subscriptionItems.Add("Hobby1", "Hobby LIKE '%Hobby1%'");
subscriptionItems.Add("Hobby2", "Hobby LIKE '%Hobby2%'");
subscriptionItems.Add("Hobby3", "Hobby LIKE '%Hobby3%'");
subscriptionItems.Add("Hobby4", "Hobby LIKE '%Hobby4%'");

CreateSubscription(subscriptionItems);
#endregion

#region Create Messages
List<Person> personList = new List<Person>();
personList.Add(new Person() { Name = "Sam", Hobbies = "Hobby1,Hobby3" });
personList.Add(new Person() { Name = "Vicky", Hobbies = "Hobby2,Hobby4" });
personList.Add(new Person() { Name = "Graham", Hobbies = "Hobby1,Hobby2" });
personList.Add(new Person() { Name = "Vivin", Hobbies = "Hobby1,Hobby2,Hobby3,Hobby4" });
personList.Add(new Person() { Name = "Sammy", Hobbies = "Hobby3" });
#endregion

#region Send Message
foreach (Person item in personList)
{
KeyValuePair<string, object> property = new KeyValuePair<string, object>("Hobby", item.Hobbies);
SendMessage(item, property);
Console.WriteLine("Item " + item.Name + " sent to " + topicDescription.Path);
}
#endregion

Console.WriteLine("Press any key to receive the message...");
Console.ReadKey();

#region Receive Message
Console.WriteLine("Hobby1:");
while (true)
{
var hobby1 = ReceiveMessage("Hobby1");
if (null != hobby1)
{
Console.WriteLine(hobby1.ToString());
}
else
{
break;
}
}

Console.WriteLine("Hobby2:");
while (true)
{
var hobby2 = ReceiveMessage("Hobby2");
if (null != hobby2)
{
Console.WriteLine(hobby2.ToString());
}
else
{
break;
}
}

Console.WriteLine("Hobby3:");
while (true)
{
var hobby3 = ReceiveMessage("Hobby3");
if (null != hobby3)
{
Console.WriteLine(hobby3.ToString());
}
else
{
break;
}
}

Console.WriteLine("Hobby4:");
while (true)
{
var hobby4 = ReceiveMessage("Hobby4");
if (null != hobby4)
{
Console.WriteLine(hobby4.ToString());
}
else
{
break;
}
}
#endregion

Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}

~BS

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s