{AoT} Azure Of Things – Queues and Topics

I spent the last six months study and developing and I like to say, now we have a lot of things in Azure which we can use to integrate a lot of things in Internet.
The world of Integration is very complex, it is a universe of things, in the first time my problem was not about how to create this or that in Azure, this is quite simple, but how to combine and use all of these things together.
For Ap as Azure power and At as Azure things, I can say that the power of Azure increases with number of Azure things combined together.

sommatory

The power of Azure is directly proportional with the summation of things we combine together.

I like to think about Azure as a little universe full of things and the collision between them is able to create energetic solutions.

AOT

I remember, just only two years ago, how much was complicate to use stuff as queues, topics or blob in Azure, now is very fast and simple, also now there are a lot more things as Event Hub, Stream Analytic, a machine who is able to learn, power features for business Intelligence, an API to write powerful services and more.
The last Microsoft released is the API APPS, the BizTalk Summit in London is a good opportunity to understand more about it and I’m also sure that many guys will start to write about it, me too.
In these last period I liked to play with all the possible things around and I was really surprise by the simplicity to use them.

Create an Azure Queue.. no sooner said than done, a Topic with multiple subscriptions…no sooner said than done, a blob or table storage for any kind of purpose no sooner said than done, no more than ten minutes coding.
For example here the code to manage an Azure Queue, I added the comment inside the code in case you need to reuse it.

[sourcecode language=”csharp”]
private void buttonQueue_Click(object sender, EventArgs e)
{

string connectionString = "Endpoint=sb://[YOUR NAME SPACE].servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[YOUR SHARE ACCESS KEY]";
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.QueueExists("TestQueue"))
{
namespaceManager.CreateQueue("TestQueue");
}
QueueClient Client =
QueueClient.CreateFromConnectionString(connectionString, "TestQueue");

//If you want to use properties
//message.Properties["TestProperty"] = "TestValue";
//message.Properties["Message number"] = i;
byte[] b = Encoding.UTF8.GetBytes("Text to Send");
Client.Send(new BrokeredMessage(b));

////Reciving***********************************
//Callback lambda approach, faster and easy
// Configure the callback options
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

// Callback to handle received messages
Client.OnMessage((message) =>
{
try
{
// Process message from queue, here to change the type for custom class
string bodymessage = message.GetBody<string>();

string propertymessage = message.Properties["TestProperty"].ToString();
// Remove message from queue
message.Complete();
MessageBox.Show(bodymessage);
}
catch (Exception)
{
// Indicates a problem, unlock message in queue
message.Abandon();
}
}, options);
}
[/sourcecode]

What I like more about this code is the improvements that Microsoft is doing inside the code pattern, the using of a lambda approach is the faster way to manage the callbacks and also is the most readable way to manage these kind of situations, I absolutely love it.

I tried to do the same with Amazon SQS (Simple Queue Service) , for some the aspects the approach is similar on the Queue creation but different in the receiving side.

miccode

Personally I prefer the Microsoft approach, I’m sure that exist the way to use the same pattern with Amazon, what I’m meaning is that the base pattern proposed by the Microsoft Framework is faster and simpler.

The Amazon receiving approach is closer to a “flat direct” pattern approach, the Microsoft approach is closer to an “event propagation” pattern and this is faster to use and also optimized for high threading approach.

queue call back

The pattern to create a Microsoft Topic is similar, this is a nice thing because the developer is going to use a same pattern approach for all the stacks, check the code below.
Below you can read the complete sample with some useful comments.

[sourcecode language=”csharp”]
private void buttonTopic_Click(object sender, EventArgs e)
{
string connectionString = "Endpoint=sb://[YOUR NAMESPACE].servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[YOUR SHARED KEY]";

var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.TopicExists("TestTopic"))
{
namespaceManager.CreateTopic("TestTopic");
}
TopicClient Client =
TopicClient.CreateFromConnectionString(connectionString, "TestTopic");

byte[] b = Encoding.UTF8.GetBytes("Message content");
BrokeredMessage message = new BrokeredMessage(b);
message.Properties["MessageNumber"] = 4;
//message.Properties["Message number"] = i;

Client.Send(message);
MessageBox.Show("Sent");

//Receiving***********************************
// Here to create a subscription
//Create a filtered subscription
SqlFilter highMessagesFilter =
new SqlFilter("MessageNumber > 3");

if (!namespaceManager.SubscriptionExists("TestTopic", "HighMessages"))
{
namespaceManager.CreateSubscription("TestTopic",
"HighMessages",
highMessagesFilter);
}

SubscriptionClient subscriptionClientHigh =
SubscriptionClient.CreateFromConnectionString
(connectionString, "TestTopic", "HighMessages");

// Configure the callback options
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

subscriptionClientHigh.OnMessage((brokerMessage) =>
{
try
{
// Process message from subscription
string bodymessage = brokerMessage.GetBody<string>();
string propertymessage = message.Properties["TestProperty"].ToString();

// Remove message from queue
brokerMessage.Complete();
MessageBox.Show(bodymessage);
}
catch (Exception)
{
// Indicates a problem, unlock message in subscription
message.Abandon();
}
}, options);
}
[/sourcecode]

What I really like is the natural and simple approach to create stuff from scratch, it is fast and easy, we can combine things as Topics, Queues, Event Hub and more in very fast way to create very complex Pub/Sub scenarios.

Event Hub is really simple to use, I’m going to put a personal laboratory in GitHub, I will share the content with you soon, I want also to prepare a video about it.

I spent the last six months playing with all the Microsoft technologies, using all of them together we are really able to create amazing things.

Related blog posts