Windows Azure 系列-- Azure Queue的操作

來源:互聯網
上載者:User

標籤:


- Storage Account, 和之前介紹的Azure Table和AzureBlob一樣,你需要一個StorageAccount,只需要建立1次AzureStorageAccount就好了,它們3個是共用的。


建立好之後,就可以使用以下屬性來訪問Azure的Storage了:


private static CloudStorageAccount StorageAccount        {            get            {                var creds = new StorageCredentials(AccountName, Key);                var account = new CloudStorageAccount(creds, useHttps: true);                return account;            }        }

- 建立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();        }


需要注意的就是Q的名字,全部小寫。


- 入隊


/// <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);        }


代碼邏輯很簡單,就是向Queue中添加訊息。不過要注意,這裡只是為了示範沒有考慮多線程環境以及並發情形,具體情境中為了不阻塞線程,你通過需要使用Asyn版本的方法,即:
queue.AddMessageAsync(message);




- 拿取指定數量的訊息


/// <summary>        /// peek a number of messages from Q        /// </summary>        /// <param name="count"></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();        }




- 出隊
/// <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 30 seconds, and then delete the message            queue.DeleteMessage(retrievedMessage);            return retrievedMessage.AsString;        }






完整的測試代碼:


[TestMethod]        public void AzureQ_Test()        {            // - create Q            AzureQueueManager.CreateIfNotExist();            // - Add 5 messages to Q            for (int i = 0; i < 5; i++)            {               AzureQueueManager.AddMsg(string.Format("hello_{0}",i));                }            // peek all messages , Assert 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 has 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");        }




測試邏輯在注釋中已經全部說明


最後,使用Azure Storage Explorer查看結果:



Windows Azure 系列-- Azure Queue的操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.