Skip to content

Instantly share code, notes, and snippets.

@clemensv
Created February 21, 2013 12:35
Show Gist options
  • Save clemensv/5004431 to your computer and use it in GitHub Desktop.
Save clemensv/5004431 to your computer and use it in GitHub Desktop.
I should talk about this more. SQL filters and rules on Service Bus Topics subscriptions. SQL action syntax http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.servicebus.messaging.sqlruleaction.sqlexpression.aspx
namespace SqlFiltersAndRules
{
using System;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
class Program
{
static void Main(string[] args)
{
var nsm = NamespaceManager.Create();
string ruletestName = "ruletest";
if (nsm.TopicExists(ruletestName))
{
nsm.DeleteTopic(ruletestName);
}
nsm.CreateTopic(ruletestName);
nsm.CreateSubscription(ruletestName, "sub1", new FalseFilter());
var sc = SubscriptionClient.Create(ruletestName, "sub1");
sc.AddRule(new RuleDescription
{
Name = "rule1",
Filter = new SqlFilter("sys.SequenceNumber % 2 = 0"),
Action = new SqlRuleAction("SET ThisMessageIs='Even'; SET FullName = FirstName + ' ' + LastName;")
});
sc.AddRule(new RuleDescription
{
Name = "rule2",
Filter = new SqlFilter("sys.SequenceNumber % 2 <> 0"),
Action = new SqlRuleAction("SET ThisMessageIs='Odd'; SET FullName = LastName + ' ' + FirstName;")
});
var tc = TopicClient.Create(ruletestName);
tc.Send( new BrokeredMessage { Properties = {{"FirstName", "Clemens"}, {"LastName", "Vasters"}}});
tc.Send(new BrokeredMessage { Properties = { { "FirstName", "Abhishek" }, { "LastName", "Lal" } } });
tc.Send(new BrokeredMessage { Properties = { { "FirstName", "Ziv" }, { "LastName", "Rafalovich" } } });
tc.Send(new BrokeredMessage { Properties = { { "FirstName", "Ruppert" }, { "LastName", "Koch" } } });
for (int i = 0; i < 4; i++)
{
var msg = sc.Receive();
Console.WriteLine("{0} {1}", msg.Properties["ThisMessageIs"], msg.Properties["FullName"]);
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment