当前位置: 首页 > news >正文

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


代码逻辑很简单,就是向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查看结果:



相关文章:

  • Build the Hack CPU with Verilog
  • 使用Service Bus Topic 实现简单的聊天室
  • 使用Service Bus + SignalR 实现聊天室
  • 中移动OMS系统展望..
  • Asp.net MVC4 + signalR 聊天室实现
  • JSF的国际化
  • javascript 替换浏览器Tab的title实现消息通知提示
  • 很好很强大 中移动OMS开源操作系统使用感想
  • [Windows编程] 获取系统CPU 个数
  • 算法练习之DP 求LCM (最长公共子序列)
  • C#中的特性Attribute
  • 算法练习 -- DP 查找和为指定数字的数组
  • 2009英雄会后记:最出彩是创业 最关注是产品 最可惜是创富
  • 算法练习--- DP 求解最长上升子序列(LIS)
  • Bellman ford 最短路径算法
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • Bootstrap JS插件Alert源码分析
  • C++类中的特殊成员函数
  • Hexo+码云+git快速搭建免费的静态Blog
  • Linux Process Manage
  • node.js
  • Promise面试题2实现异步串行执行
  • vue-cli3搭建项目
  • vue-loader 源码解析系列之 selector
  • vue中实现单选
  • 大快搜索数据爬虫技术实例安装教学篇
  • 构造函数(constructor)与原型链(prototype)关系
  • 好的网址,关于.net 4.0 ,vs 2010
  • 解决iview多表头动态更改列元素发生的错误
  • 线上 python http server profile 实践
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • ​Python 3 新特性:类型注解
  • #《AI中文版》V3 第 1 章 概述
  • #NOIP 2014# day.2 T2 寻找道路
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (11)MATLAB PCA+SVM 人脸识别
  • (14)Hive调优——合并小文件
  • (k8s中)docker netty OOM问题记录
  • (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  • (规划)24届春招和25届暑假实习路线准备规划
  • (力扣)循环队列的实现与详解(C语言)
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (算法)求1到1亿间的质数或素数
  • (转)负载均衡,回话保持,cookie
  • (转载)利用webkit抓取动态网页和链接
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .Net的DataSet直接与SQL2005交互
  • .net实现客户区延伸至至非客户区
  • .NET运行机制
  • /bin/bash^M: bad interpreter: No such file ordirectory
  • @media screen 针对不同移动设备
  • [AutoSAR 存储] 汽车智能座舱的存储需求