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查看结果:
Windows Azure 系列-- Azure Queue的操作的更多相关文章
- Windows Azure 系列-- Azure Redis Cache的配置和使用
假设还没有配置Azure Power shell 能够參照这里进行配置:http://blog.csdn.net/lan_liang/article/details/46850221 打开Azure ...
- 初码-Azure系列-迁移PHP应用至Azure的一些实践记录和思考
最近客户在逐步迁移应用从阿里云到Azure,这次又轮到一个PHP+MySQL应用了,顺便也记一下流水账. 需求:迁移部署在阿里云上的ECS服务器(系列2,IO优化+2核4G+50G的SSD云盘+10M ...
- 初码-Azure系列-存储队列的使用与一个Azure小工具(蓝天助手)
初码Azure系列文章目录 将消息队列技术模型简化,并打造成更适合互联网+与敏捷开发的云服务模式,好像已经是行业趋势,阿里云也在推荐使用消息服务(HTTP协议为主)而来替代消息队列(TCP协议.MQT ...
- 初码-Azure系列-记一次MySQL数据库向Azure的迁移
初码Azure系列文章目录 还在继续给客户迁移不同的系统到Azure,这一次是一个系统的MySQL数据库要迁移,将迁移过程记录一下 原系统环境 数据库版本:MySQL Community Editio ...
- 初码-Azure系列-文章目录
系统迁移 初码-Azure系列-记一次MySQL数据库向Azure的迁移 初码-Azure系列-迁移PHP应用至Azure的一些实践记录和思考 初码-Azure系列-记一次从阿里云到Azure的迁移和 ...
- 初码-Azure系列-如何在控制面板中选择中文版操作系统
之前在文章<初码-Azure系列-记一次从阿里云到Azure的迁移和部署>中说到,默认的Windows Server 2016操作系统是英文版,后来摸索出中文版的方法,如下:
- 微软云消息队列 Azure service bus queue
前言 第一次使用消息队列,遇到了一些问题:同一个消息有多次出列.是一个消息只入列一次,还是多次?还是因为出列问题,出列了多次? Microsoft Azure service bus queue Az ...
- 如何在ASP.NET Core中使用Azure Service Bus Queue
原文:USING AZURE SERVICE BUS QUEUES WITH ASP.NET CORE SERVICES 作者:damienbod 译文:如何在ASP.NET Core中使用Azure ...
- Windows 上安装 Azure PowerShell及Azure PowerShell部署虚拟机
一.Azure PowerShell部署 1.使用 PowerShellGet 在 Windows 上安装 Azure PowerShell 从 Azure PowerShell 版本 6.0 开 ...
随机推荐
- 【前端开发】禁止微信内置浏览器调整字体大小的方法js
微信webview内置了调整字体大小的功能,用户可以根据实际情况进行调节.但是很多移动端页面的开发都是使用rem作为单位的,字体大小改变以后,会出现页面布局错乱的情况,因此希望能够禁止微信的字体放大功 ...
- 数据库-mysql安装
MySQL 安装 所有平台的Mysql下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. Linux/UNIX上安装Mysql Linux ...
- Isolate randomforest检测异常点的非监督方法
由于异常数据的两个特征(少且不同: few and different) 异常数据只占很少量; 异常数据特征值和正常数据差别很大. iTree的构成过程如下: l 随机选择一个属性Attr: l ...
- redis tutorail
命令 set get incr expire 秒 ttl -1 不会过期 list : lpush rpush lpop rpop lrange llen se ...
- Python基础 - Ubuntu+Nginx+uwsgi+supervisor部署Flask应用
网上找了许多讲关于Flask应用部署的文章几乎都是一个helloworld的Demo,按照helloworld来部署都没问题,但实际项目部署时还是遇到了不少问题.在这里简单写下自己成功部署的过程,防止 ...
- Linux学习笔记:644、755、777权限详解
一.问题 1.在Linux或者Android系统下用命令ll或者ls -la的时候会看到前面-rw-rw-r--一串字符,不知道代表什么? 2.新建vi一个文件之后,经常需要chmod 755 fil ...
- Windows开机自动启动pageant,方便使用ssh链接到GitHub
按win +r,输入 shell:startup "C:\Program Files\TortoiseGit\bin\pageant.exe" "d:\GitHubPri ...
- RabbitMQ介绍及安装部署
本节内容: RabbitMQ介绍 RabbitMQ运行原理 RabbitMQ重要术语 三种ExchangeType RabbitMQ集群种类 集群基本概念 镜像模式部署集群 一.RabbitMQ介绍 ...
- sql developer连接mysql
刚刚安装sql developer之后,数据库连接时没有mysql的选项,需要增加一个jar包 mysql-connector-java-6.0.5.zip 工具--->首选项--->数据 ...
- hdoj1233 还是畅通工程(Prime || Kruskal)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1233 思路 最小生成树问题,使用Prime算法或者Kruskal算法解决. 代码 Prime算法: # ...