问题导读
1.如何访问Azure的Storage?
2.如何拿取指定数量的消息?
3.如何创建Azure Q?
- Storage Account, 和之前介绍的Azure Table和AzureBlob一样,你需要一个StorageAccount,只需要创建1次AzureStorageAccount就好了,它们3个是共享的。
创建好之后,就可以使用以下属性来访问Azure的Storage了:
[mw_shl_code=csharp,true]private static CloudStorageAccount StorageAccount
{
get
{
var creds = new StorageCredentials(AccountName, Key);
var account = new CloudStorageAccount(creds, useHttps: true);
return account;
}
} [/mw_shl_code]
- 创建Azure Q
[mw_shl_code=csharp,true]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();
} [/mw_shl_code]
需要注意的就是Q的名字,全部小写。
- 入队
[mw_shl_code=csharp,true]</span>/// <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);
} [/mw_shl_code]
代码逻辑很简单,就是向Queue中添加消息。不过要注意,这里只是为了演示没有考虑多线程环境以及并发情形,具体场景中为了不阻塞线程,你通过需要使用Asyn版本的方法,即:
[mw_shl_code=csharp,true]queue.AddMessageAsync(message); [/mw_shl_code]
- 拿取指定数量的消息
[mw_shl_code=csharp,true]/// <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();
} [/mw_shl_code]
- 出队
[mw_shl_code=csharp,true]/// <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;
} [/mw_shl_code]
完整的测试代码:
[mw_shl_code=csharp,true][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");
} [/mw_shl_code]
测试逻辑在注释中已经全部说明
最后,使用Azure Storage Explorer查看结果:
|