Windows Azure Storage Queue Storage
What is Queue Storage Windows Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64KB in size, a queue can contain millions of messages, up to the 100TB total capacity limit of a storage account.
Queue Common uses of Queue storage include: Creating a backlog of work to process asynchronously Passing messages from a Windows Azure Web role to a Windows Azure Worker role
Queue
Access Queue storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows Azure Storage: using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Auth; using Microsoft.WindowsAzure.Storage.Queue; Microsoft.WindowsAzure.Storage.dll assembly is to be added.
Access Queue storage Retrieving your connection string use the CloudConfigurationManager type to retrieve your storage connection string and storage account information from the Windows Azure service configuration: CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString"));
Access Queue storage A CloudQueueClient type allows you to retrieve objects that represent Queues stored within the Queue Storage Service. The following code creates a CloudQueueClient object using the storage account object we retrieved above: CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
Access Queue storage Create a Queue use a CloudQueueClient object to get a reference to the Queue you want to use. You can create the Queue if it doesn't exist:
Access Queue storage Create a Queue... // Retrieve a reference to a Queue. CloudQueue queue=queueClient.GetTableReference(“messages"); // Create the table if it doesn‘t alreadyexist. queue.CreateIfNotExists();
Access Queue storage To insert a message into an existing queue, first create a new CloudQueueMessage. Next, call the AddMessage method. A CloudQueueMessage can be created from either a string (in UTF-8 format) or a byte array
Access Queue storage The following code creates an encrypted message and inserts into the Queue. protected void Button1_Click(object sender, EventArgs e) { char[] msg = TextBox3.Text.ToCharArray(); for (int i = 0; i < msg.Length; i++) { int ch = (msg[i] + 3); char ch1 = (char)ch; msg[i] = ch1; }
Access Queue storage string cm = new string(msg); cm = cm + "\n"; TextBox1.Text += cm; // Create a message and add it to the queue. CloudQueueMessage message = new CloudQueueMessage(cm); queue.AddMessage(message); }
Access Queue storage To Dequeue Messages from Worker Role first we need to obtain a reference to the queue using the same code used in WebRole1 project, and then dequeue a message using the queue service proxy. public override void Run() { //Code to obtain a reference to the queue //… while (true) CloudQueueMessage m = queue.GetMessage();
Access Queue storage if (m != null){ queue.DeleteMessage(m); char[] msg = m.AsString.ToCharArray(); for (int i = 0; i < msg.Length; i++){ int ch = (msg[i] - 3); char ch1 = (char)ch; msg[i] = ch1; } string cm = new string(msg); CloudQueueMessage message = new CloudQueueMessage(cm); queue.AddMessage(message); Thread.Sleep(10000);
Output