您的位置:首页 > 产品设计 > UI/UE

Windows Azure 系列-- Azure Queue的操作

2015-06-09 12:39 537 查看
- 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查看结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: