Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using filtered subscriptions in Azure Service Bus
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Runtime.Serialization.dll</Reference>
<NuGetReference>WindowsAzure.ServiceBus</NuGetReference>
<Namespace>Microsoft.ServiceBus</Namespace>
<Namespace>Microsoft.ServiceBus.Messaging</Namespace>
</Query>
void Main()
{
string connectionString = Util.GetPassword("Test Azure Service Bus Connection String");
const string topicName = "ExampleTestTopic";
const string subscriptionName = "AllMessages";
const string sub1Name = "Filtered1";
const string sub2Name = "Filtered2";
// PART 1 - CREATE THE TOPIC
Console.WriteLine("Creating the topic");
var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.TopicExists(topicName))
{
// Configure Topic Settings.
var td = new TopicDescription(topicName);
td.MaxSizeInMegabytes = 1024;
td.DefaultMessageTimeToLive = TimeSpan.FromMinutes(5);
namespaceManager.CreateTopic(td);
}
// PART 2 - CREATE ALL SUBSCRIPTIONS SUBSCRIPTION
if (!namespaceManager.SubscriptionExists(topicName, subscriptionName))
{
namespaceManager.CreateSubscription(topicName, subscriptionName);
}
if (namespaceManager.SubscriptionExists(topicName, sub1Name))
{
Console.WriteLine("Deleting subscription {0}", sub1Name);
namespaceManager.DeleteSubscription(topicName, sub1Name);
}
Console.WriteLine("Creating subscription {0}", sub1Name);
namespaceManager.CreateSubscription(topicName, sub1Name, new SqlFilter("From LIKE '%Smith'"));
if (namespaceManager.SubscriptionExists(topicName, sub2Name))
{
Console.WriteLine("Deleting subscription {0}", sub2Name);
namespaceManager.DeleteSubscription(topicName, sub2Name);
}
Console.WriteLine("Creating subscription {0}", sub2Name);
namespaceManager.CreateSubscription(topicName, sub2Name, new SqlFilter("sys.Label='important'"));
// PART 3 - SEND SOME MESSAGES
var body = "Hello World";
var message1 = new BrokeredMessage(body);
message1.Properties["From"] = "Ian Wright";
var message2 = new BrokeredMessage("Second message");
message2.Properties["From"] = "Alan Smith";
message2.Label = "important";
var message3 = new BrokeredMessage("Third message");
message3.Properties["From"] = "Kelly Smith";
message3.Label = "information";
var client = TopicClient.CreateFromConnectionString(connectionString, topicName);
client.Send(message1);
client.Send(message2);
client.Send(message3);
// PART 4 - RECEIVE MESSAGES
Console.WriteLine("Waiting for messages");
// Configure the callback options.
var options = new OnMessageOptions();
var subClient =
SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);
subClient.OnMessage(m => MessageReceived("ALL", m), options);
var subClient1 =
SubscriptionClient.CreateFromConnectionString(connectionString, topicName, sub1Name);
subClient1.OnMessage(m => MessageReceived("SUB1", m), options);
var subClient2 =
SubscriptionClient.CreateFromConnectionString(connectionString, topicName, sub2Name);
subClient2.OnMessage(m => MessageReceived("SUB2", m), options);
Thread.Sleep(5000);
client.Close();
subClient.Close();
subClient1.Close();
subClient2.Close();
Console.WriteLine("Done");
}
void MessageReceived(string subscriptionName, BrokeredMessage message)
{
// Process message from queue.
Console.WriteLine("{0} '{1}' Label: '{2}' From: '{3}'", subscriptionName,
message.GetBody<string>(),
message.Label,
message.Properties["From"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment