Passing Objects via the Azure Message Queue
I have recently been taking a look at Windows Azure Storage, and the use of Blob’s, Queues and Tables.
Queues are primarily used for passing small (approx 8kb) messages from the web role of a Azure application to the back end worker role that can then be used to fire off additional processing within the cloud.
Using the current StorageClient library that is supplied with the CTP of Azure you can only pass strings as messages from a web role to a worker role by doing the following:
1: Message message = new Message("simple text");
2: RoleManager.WriteToLog("Information", string.Format("Message '{0}' added to the queue", message.ContentAsString()));
3: queue.PutMessage(message);
However, this got me thinking that we will most likely be wanting to pass much more across to the worker role than just simple strings. Allowing more complex computation, such as passing custom objects to the cloud that can then be used by the worker.
To show how this can be done, I have put a simple demo together that pass a DateTime object from the web role to the worker, using XML serialisation.
1: MemoryStream m = new MemoryStream();
First we must define a MemoryStream that the XML Serializer will send the serialized XML to much like we would if we were wanting to write a serialized object to file.
1: XmlSerializer xs = new XmlSerializer(typeof(DateTime));
2: xs.Serialize(m, new DateTime(1986, 12, 04));
Once the DateTime object has been serialized to XML we can convert the stream from a stream object, first to a byte array and then to a string. This is done using the Encoding namespace and ASCII encoding.
1: string dateTimeObject = Encoding.ASCII.GetString(m.ToArray());
We then use XML string from the MemoryStream and add this as the content of a Message that can now be added to the queue.
1: Message message = new Message(dateTimeObject);
2: RoleManager.WriteToLog("Information", string.Format("Message '{0}' added to the queue", message.ContentAsString()));
3: queue.PutMessage(message);
Once this has been done on the web role, the object can then be taken off the queue and processed by the Worker role by doing the reverse and casting the string back into an instance of a DateTime object.
1: MemoryStream m = new MemoryStream(Encoding.ASCII.GetBytes(message.ContentAsString()));
2: XmlSerializer xs = new XmlSerializer(typeof(DateTime));
3: DateTime birthday = (DateTime)xs.Deserialize(m);
4: RoleManager.WriteToLog("Information", birthday.Year.ToString());
5: queue.DeleteMessage(message);
We can now pass any object that can be serialized from the web role straight to the worker role to be processed.
Technorati Tags: Azure,Cloud Computing,Windows Azure,Azure Storage,Queues
Comments
Anonymous
February 07, 2009
PingBack from http://www.clickandsolve.com/?p=4649Anonymous
February 07, 2009
Nice post; this is a great use of queues! Note that you can also create queue messages of binary data, so you needn't restrict yourself to string representations.