Windows Azure 系列-- Azure Queue的操作


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


代码逻辑非常easy,就是向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查看结果:



posted @ 2016-04-14 12:45  mfrbuaa  阅读(335)  评论(0编辑  收藏  举报