Windows Azure Series-Operations for Azure queue

Source: Internet
Author: User


-Storage account, just like the azure table and Azureblob described earlier, you need a storageaccount, just create 1 azurestorageaccount, and 3 are shared.


once created, you can use the following properties to access Azure's storage:


private static Cloudstorageaccount Storageaccount        {            get            {                var creds = new Storagecredentials ( AccountName, Key);                var account = new Cloudstorageaccount (creds, usehttps:true);                return account;            }        }

-Create Azure Q


public static void Createifnotexist ()        {            //Create the queue client            cloudqueueclient queueclient = Storageaccount.createcloudqueueclient ();            Cloudqueue queue = queueclient.getqueuereference (ordersqueue);            Create the queue if it doesn ' t already exist            queue. Createifnotexists ();        }


The first thing to note is the name of Q, all lowercase.


-Queue


<summary>//Add MSG to Q///         </summary>/        <param name= "msg" ></param>        public static void Addmsg (String msg)        {            cloudqueueclient queueclient = storageaccount.createcloudqueueclient ( );            Retrieve a reference to a queue.            Cloudqueue queue = queueclient.getqueuereference (ordersqueue);            Create a message and add it to the queue.            Cloudqueuemessage message = new Cloudqueuemessage (msg);            Queue. AddMessage (message);        }


The code logic is simple, which is to add a message to the queue. Note, however, that this is just to demonstrate that there are no multithreaded environments and concurrency scenarios in which you need to use the Asyn version of the method in order not to block threads:
Queue. Addmessageasync (message);




-Fetch a specified number of messages


<summary>///        Peek A number of messages from Q/a </summary>//        <param name= "Count" >& lt;/param>        //<returns></returns> public static ilist<string> Peek (int count)        {            //Create the queue client            Cloudqueueclient queueclient = Storageaccount.createcloudqueueclient ();            Retrieve a reference to a queue            cloudqueue queue = queueclient.getqueuereference (ordersqueue);            Peek at the next message            ienumerable<cloudqueuemessage> peekedmessages = queue. Peekmessages (count);            Return Peekedmessages.select (M = m.asstring). ToList ();        }




-The team
<summary>//        dequeue a msg//</summary>//        <returns></returns>        public static string Dequeuemsg ()        {            var queueclient = storageaccount.createcloudqueueclient ();            Retrieve a reference to a queue            var queue = queueclient.getqueuereference (ordersqueue);            var retrievedmessage = queue. GetMessage ();            Process the message in less than with seconds, and then delete the message            queue. DeleteMessage (retrievedmessage);            return retrievedmessage.asstring;        }






The Complete test code:


[TestMethod] public void azureq_test () {//-Create Q Azurequeuemanager.createifnotex            IST (); -ADD 5 messages to Q for (int i = 0; i < 5; i++) {azurequeuemanager.addmsg (str Ing.                Format ("hello_{0}", i));            }//Peek All messages, Assert that the order is correct var msgs = Azurequeuemanager.peek (5); Assert.istrue (msgs.            Count = = 5);            Assert.istrue (msgs[0] = = "Hello_0");            Assert.istrue (msgs[1] = = "Hello_1");            Assert.istrue (msgs[2] = = "Hello_2");            Assert.istrue (msgs[3] = = "Hello_3");            Assert.istrue (msgs[4] = = "Hello_4");            -Dequeue msg var msg = azurequeuemanager.dequeuemsg ();            Assert.istrue (msg = = "Hello_0");            -Peek all messages, assert the first msg have been dequeued msgs = Azurequeuemanager.peek (5); Assert.istrue (msgs.            Count = = 4);Assert.istrue (msgs[0] = = "Hello_1");            Assert.istrue (msgs[1] = = "Hello_2");            Assert.istrue (msgs[2] = = "Hello_3");        Assert.istrue (msgs[3] = = "Hello_4"); }




The test logic is all explained in the comments


Finally, use Azure Storage Explorer to view the results:



Windows Azure Series-Operations for Azure 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.