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 开 ...
随机推荐
- Nginx - keepliave 相关知识点
目录 - 1. 前言- 2. keepalive 介绍- 3. Nginx 与 keepalive 的关系 - 3.1 Nginx - keepalive_timeout - 3.2 Ng ...
- [转] Cacti+Nagios监控平台完美整合
Cacti+Nagios监控平台完美整合 http://os.51cto.com/art/201411/458006.htm 整合nagios+cacti+微信.飞信实现网络监控报警 http://b ...
- 洛谷P2822组合数问题
传送门啦 15分暴力,但看题解说暴力分有30分. 就是找到公式,然后套公式.. #include <iostream> #include <cstdio> #include & ...
- loaded some nib but the view outlet was not set(转载)
当使用 initWithNibName 函数, 并使用 由nib文件生成的ViewController 的view属性时候,遇到这个问题. //load loc.xib UIViewControlle ...
- 解决insert语句插入时,需要写列值的问题
今天发现解决这个问题其实很简单,闲话不多谈,我直接附上语句 ) select @s = isnull(@s+',', '') + [name] from syscolumns where id = o ...
- nginx+keepalived高可用服务器宕机解决方案
http://blog.51cto.com/gdutcxh/2109841 https://blog.csdn.net/winsonyuan/article/details/52784988
- Linux下光盘镜像生成和刻录
mkiosfs命令如在/root/下有文件file1 file2 file3maiosfs -o img.ios file1 file2 file3该命令将file1 file2 file3放入到im ...
- s12-day01-work01用户登录接口
README # README.md # day001-work-1 @南非波波 功能实现:登录接口 流程图:  程序实现: 1. ...
- 【LOJ】#2672. 「NOI2012」魔幻棋盘
题解 代码7.1KB,人傻代码长 恶心死我了这代码真的把我写恶心了= = 想一想就知道这个东西--维护到中心的差分,然后用二维线段树维护一下矩形的gcd 嗯,我说完了,你写吧. 首先这个二维线段树的单 ...
- Django实战(8):对比RoR与Django的输入校验机制
rails有一个”简洁.完美的验证机制,无比强大的表达式和验证框架“.在<Agile Web Development with Rails 4th>一书的7.1节向我们展示了如何验证Pro ...