zoukankan      html  css  js  c++  java
  • 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查看结果:



  • 相关阅读:
    AWS IoT Greengrass:配置安装 AWS CLI
    AWS IoT Greengrass:如何使用 AWS 管理控制台配置本地资源访问
    AWS IoT Greengrass 入门-模块6: 访问其他 AWS 服务
    AWS IoT Greengrass 入门-模块5:与设备影子交互
    AWS IoT Greengrass 入门-模块4:在 AWS IoT Greengrass 组中与设备交互
    AWS IoT Greengrass 入门-模块3(第 2 部分):AWS IoT Greengrass 上的 Lambda 函数
    Nginx 启动报错 (nginx: error while loading shared libraries: XXX: cannot open shared object file: No such file or directory ) 的解决办法
    自己制作一个简单的操作系统二[CherryOS]
    Linux-误删apt-get以及把aptitude换回
    完美解决phpstudy安装后mysql无法启动(无需删除原数据库,无需更改任何配置,无需更改端口)直接共存
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5390615.html
Copyright © 2011-2022 走看看