Azure Queue Storage Basic usage--Azure Storage queue

Source: Internet
Author: User

Azure Storage is the cloud storage solution provided by Microsoft Azure Cloud and currently supports storage types such as Blob, Queue, File, and Table.

The basic usage of file Storage is described inAzure file Storage basic usage , and this article describes the main ways to use Queue Storage.

What is Queue Storage?

Azure Queue Storage is a storage service that stores a large number of messages that can be accessed from anywhere through Http/https. Each message has a maximum of 64K, and the amount of data in the message is almost unlimited (unless the total capacity of your Storage account is exceeded).

Here is a typical scenario for the Queue Storage:

    1. Create a queue of unhandled tasks to handle these tasks asynchronously.
    2. The message is passed from the Web role to the worker role for processing.
structure of the Azure Queue Storage

Describes the basic organizational structure of the Queue Storage:

    • Azure Storage Account:

The Storage account is a namespace used to manage Azure Storage and is primarily used to control access to and billing for stored data. Access control for Blob, queue, File, and Table these Azure-provided storage services is done through the Storage account, so you'll need to create your Storage account first to use Queue Storage.

    • Queue:

Each Queue is a set of messages, and each message must belong to a character in the Queue,queue name must be lowercase.

    • Message:

The maximum length of each message is 7 days for 64kb,message to stay in the Queue.

    • URL format:

The Queue's URL address format is:

Http://<storage account>.queue.core.windows.net/<queuename>

The following is a more realistic example:

Http://nickstorage.queue.core.windows.net/app1tasks

If you are not yet familiar with the use of Azure Storage account and how to access Azure Storage through the Windowsazure.storage Library, refer to the previous article, "Azure Table Storage basic Usage " The introduction.

To facilitate viewing the results of C # code execution, this article uses an Azure Storage client tool published by MS:Microsoft Azure Storage Explorer, which is referred to as Storage Explorer. Here is one of the Queue Storage:

Next we use C # code to describe how to manipulate the Queue Storage.

Create a Queue

Let's start by creating a Queue named "App2tasks":

//The Cloudstorageaccount class represents an Azure Storage account, and we need to create an instance of it before we can access the resources that belong to it. //Note the xxx and yyy in the connection string, respectively, corresponding to the storage account name and key in Access keys. Cloudstorageaccount Storageaccount = Cloudstorageaccount.parse ("Defaultendpointsprotocol=https; accountname=xxx; ACCOUNTKEY=YYY");//The Cloudqueueclient class is a logical representation of the Windows Azure Queue Service Client and we need to use it to configure and perform operations on the queue Storage. Cloudqueueclient queueclient =storageaccount.createcloudqueueclient ();//Cloudqueue represents a Queue object, and the vast majority of operations are done through this object. Cloudqueue queue = Queueclient.getqueuereference ("App2tasks");//Create a Queue with the name "App2tasks" if it does not exist. Queue. Createifnotexists ();

Execute the above code, and then view the results in the Storage Explorer:

insert a message into the Queue

There must be one or more programs in a real-world scenario that produce a Message and insert it into a Queue, so let's look at how C # is implemented:

string current = DateTime.Now.ToString (); // inserts the message into the queue.  New cloudqueuemessage ("" + current ), queue. AddMessage (message);

Call the above code several times to see how the results are:

I've added three messages to the Queue through three calls, and note the time they were inserted, 11:33:45,11:33:57, and 11.34:16. In the following description I call them the first message, the second message, and the third message, respectively.

viewing messages in a Queue

Since the queue, there must be a team head and tail, the message from the team head out of the team, from the end of the team. Then can you look at the team header (that is, the next message to be processed, here just to see the message is not to be processed)? Of course:

// always take the message to the team head, no news out of the team.  //cloudqueuemessage peekedmessage = queue. PeekMessage ();

The PeekMessage method always takes the message at the head of the team and does not change the status of the queue!

to view the length of a Queue

It's a good idea to check the length of a queue frequently because you need to avoid some of the problems caused by the long queue:

// gets the properties of the Queue.  queue. Fetchattributes (); int cachedmessagecount = queue. Approximatemessagecount;
update messages in a Queue

What if a message has been added to the Queue but needs to update its contents? We can find this message and then update its contents:

Cloudqueuemessage message = queue. GetMessage (); // Execution of GetMessage (), the message of the team header becomes invisible. message. Setmessagecontent ("Updated contents. " ); queue. Updatemessage (Message,    timespan.fromseconds (60.0),     |  messageupdatefields.visibility); // after the 60s of the message content is updated, the message is re-visible, but at the end of the queue. 

After executing the above code, we found that the "first message" was missing in Storage Explorer. After 60 seconds it re-appears in the Storage Explorer, but its content has changed and the position has become the tail of the team:

At this point we can only recognize it through the ID is the previous "first message", before "second message", "The third message" position has also changed accordingly.

working with messages in a Queue

How do I handle messages in a Queue? Our program should generally follow the following logic:

    1. Use the GetMessage method to remove a message from the queue, at which point the message is not visible for 30 seconds (this is a constant user can set, the default is 30 seconds);
    2. processing messages;
    3. After the normal processing is complete, call the Delete method to delete the message;
    4. If the message is not processed properly (the Delete method is not called), the message will appear again at the end of the queue after 30 seconds.

Code logic similar to the following:

// Execution of GetMessage (), the message of the team header becomes invisible. cloudqueuemessage message = queue. GetMessage (); Try {    // process message     //  If you do not delete this message within 30s, it will appear again at the end of the line.     // so the process of correctly handling a message is to delete the message after processing is complete     queue. DeleteMessage (message);} Catch // (Message handling Exception) { }
Delete a message in a Queue

In addition to removing the message from the queue after the normal processing of the message, we can also find a message and delete it directly, essentially the same as after processing and then deleting it.

Summary

The Queue Storage provides a good solution for decoupling between applications, so that the message creator and the message processor can not know each other's existence. Adds a powerful tool to our handling of this kind of problem.

Azure Queue Storage Basic usage--Azure Storage queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.