[Windows Azure] How to use the Queue Storage Service
How to use the Queue Storage Service
This guide will show you how to perform common scenarios using the Windows Azure Queue storage service. The samples are written in C# code and use the Windows Azure Storage Client for .NET. The scenarios covered include inserting, peeking,getting, and deleting queue messages, as well as creating and deleting queues. For more information on queues, refer to the Next steps section.
Table of contents
- What is Queue Storage
- Concepts
- Create a Windows Azure Storage Account
- Setup a Windows Azure Storage connection string
- How to: Programmatically access queues using .NET
- How to: Create a queue
- How to: Insert a message into a queue
- How to: Peek at the next message
- How to: Change the contents of a queued message
- How to: Dequeue the next message
- How to: Leverage additional options for dequeuing messages
- How to: Get the queue length
- How to: Delete a queue
- Next steps
What is Queue Storage
Windows Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64KB in size, a queue can contain millions of messages, up to the 100TB total capacity limit of a storage account. Common uses of Queue storage include:
- Creating a backlog of work to process asynchronously
- Passing messages from a Windows Azure Web role to a Windows Azure Worker role
Concepts
The Queue service contains the following components:
URL format: Queues are addressable using the following URL format:
http://<storage account>
.queue.core.windows.net/<queue>
The following URL addresses one of the queues in the diagram:
http://myaccount.queue.core.windows.net/imagesToDownloadStorage Account: All access to Windows Azure Storage is done through a storage account. A storage account is the highest level of the namespace for accessing queues. The total size of blob, table, and queue contents in a storage account cannot exceed 100TB.
Queue: A queue contains a set of messages. All messages must be in a queue.
Message: A message, in any format, of up to 64KB.
Create a Windows Azure Storage account
To use storage operations, you need a Windows Azure storage account. You can create a storage account by following these steps. (You can also create a storage account using the REST API.)
Log into the Windows Azure Management Portal.
At the bottom of the navigation pane, click NEW.
Click DATA SERVICES, then STORAGE, and then click QUICK CREATE.
In URL, type a subdomain name to use in the URI for the storage account. The entry can contain from 3-24 lowercase letters and numbers. This value becomes the host name within the URI that is used to address Blob, Queue, or Table resources for the subscription.
Choose a Region/Affinity Group in which to locate the storage. If you will be using storage from your Windows Azure application, select the same region where you will deploy your application.
Optionally, you can enable geo-replication.
Click CREATE STORAGE ACCOUNT.
Setup a Windows Azure Storage Connection String
The Windows Azure Storage Client Library for .NET supports using a storage connection string to configure endpoints and credentials for accessing storage services. You can put your storage connection string in a configuration file, rather than hard-coding it in code:
- When using Windows Azure Cloud Services, it is recommended you store your connection string using the Windows Azure service configuration system (
*.csdef
and*.cscfg
files). - When using Windows Azure Web Sites, Windows Azure Virtual Machines, or building .NET applications that are intentded to run outside of Windows Azure, it is recommended you store your connection string using the .NET configuration system (e.g.
web.config
orapp.config
file).
In both cases, you can retrieve your connection string using the CloudConfigurationManager.GetSetting
method, as shown later in this guide.
Configuring your connection string when using Cloud Services
The service configuration mechanism is unique to Windows Azure Cloud Services projects and enables you to dynamically change configuration settings from the Windows Azure Management Portal without redeploying your application.
To configure your connection string in the Windows Azure service configuration:
Within the Solution Explorer of Visual Studio, in the Roles folder of your Windows Azure Deployment Project, right-click your web role or worker role and click Properties.
Click the Settings tab and press the Add Setting button.
A new Setting1 entry will then show up in the settings grid.
In the Type drop-down of the new Setting1 entry, choose Connection String.
Click the ... button at the right end of the Setting1 entry. The Storage Account Connection String dialog will open.
Choose whether you want to target the storage emulator (the Windows Azure storage simulated on your local machine) or an actual storage account in the cloud. The code in this guide works with either option. Enter the Primary Access Key value copied from the earlier step in this tutorial if you wish to store queue data in the storage account we created earlier on Windows Azure.
Change the entry Name from Setting1 to a "friendlier" name like StorageConnectionString. You will reference this connection string later in the code in this guide.
Configuring your connection string using .NET configuration
If you are writing an application that is not a Windows Azure cloud service, (see previous section), it is recommended you use the .NET configuration system (e.g. web.config
or app.config
). This includes Windows Azure Web Sites or Windows Azure Virtual Machines, as well as applications designed to run outside of Windows Azure. You store the connection string using the<appSettings>
element as follows:
<configuration><appSettings><addkey="StorageConnectionString"value="DefaultEndpointsProtocol=https;AccountName=[AccountName];AccountKey=[AccountKey"/></appSettings></configuration>
Read Configuring Connection Strings for more information on storage connection strings.
You are now ready to perform the how-to tasks in this guide.
How to: Programmatically access queues using .NET
Obtaining the assembly
You can use NuGet to obtain the Microsoft.WindowsAzure.Storage.dll
assembly. Right-click your project in Solution Explorer and choose Manage NuGet Packages. Search online for "WindowsAzure.Storage" and click Install to install the Windows Azure Storage package and dependencies.
Namespace declarations
Add the following code namespace declarations to the top of any C# file in which you wish to programmatically access Windows Azure Storage:
usingMicrosoft.WindowsAzure.Storage;usingMicrosoft.WindowsAzure.Storage.Auth;usingMicrosoft.WindowsAzure.Storage.Queue;
Make sure you reference the Microsoft.WindowsAzure.Storage.dll
assembly.
Retrieving your connection string
You can use the CloudStorageAccount type to represent your Storage Account information. If you are using a Windows Azure project template and/or have a reference to Microsoft.WindowsAzure.CloudConfigurationManager, you can you use theCloudConfigurationManager type to retrieve your storage connection string and storage account information from the Windows Azure service configuration:
CloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
If you are creating an application with no reference to Microsoft.WindowsAzure.CloudConfigurationManager and your connection string is located in the web.config
or app.config
as show above, then you can use ConfigurationManager to retrieve the connection string. You will need to add a reference to System.Configuration.dll to your project, and add another namespace declaration for it:
usingSystem.Configuration;...CloudStorageAccount storageAccount =CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
ODataLib dependencies
ODataLib dependencies in the Storage Client Library for .NET are resolved through the ODataLib (version 5.0.2) packages available through NuGet and not WCF Data Services. The ODataLib libraries can be downloaded directly or referenced by your code project through NuGet. The specific ODataLib packages are OData, Edm, and Spatial.
How to: Create a queue
A CloudQueueClient object lets you get reference objects for queues. The following code creates a CloudQueueClient object. All code in this guide uses a storage connection string stored in the Windows Azure application's service configuration. There are also other ways to create a CloudStorageAccount object. See CloudStorageAccount documentation for details.
// Retrieve storage account from connection stringCloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the queue clientCloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
Use the queueClient object to get a reference to the queue you want to use. You can create the queue if it doesn't exist.
// Retrieve a reference to a queueCloudQueue queue = queueClient.GetQueueReference("myqueue");// Create the queue if it doesn't already exist
queue.CreateIfNotExists();
How to: Insert a message into a queue
To insert a message into an existing queue, first create a new CloudQueueMessage. Next, call the AddMessage method. ACloudQueueMessage can be created from either a string (in UTF-8 format) or a byte array. Here is code which creates a queue (if it doesn't exist) and inserts the message 'Hello, World':
// Retrieve storage account from connection string.CloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the queue client.CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();// Retrieve a reference to a queue.CloudQueue queue = queueClient.GetQueueReference("myqueue");// Create the queue if it doesn't already exist.
queue.CreateIfNotExists();// Create a message and add it to the queue.CloudQueueMessage message =newCloudQueueMessage("Hello, World");
queue.AddMessage(message);
How to: Peek at the next message
You can peek at the message in the front of a queue without removing it from the queue by calling the PeekMessage method.
// Retrieve storage account from connection stringCloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the queue clientCloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();// Retrieve a reference to a queueCloudQueue queue = queueClient.GetQueueReference("myqueue");// Peek at the next messageCloudQueueMessage peekedMessage = queue.PeekMessage();// Display message.Console.WriteLine(peekedMessage.AsString);
How to: Change the contents of a queued message
You can change the contents of a message in-place in the queue. If the message represents a work task, you could use this feature to update the status of the work task. The following code updates the queue message with new contents, and sets the visibility timeout to extend another 60 seconds. This saves the state of work associated with the message, and gives the client another minute to continue working on the message. You could use this technique to track multi-step workflows on queue messages, without having to start over from the beginning if a processing step fails due to hardware or software failure. Typically, you would keep a retry count as well, and if the message is retried more than n times, you would delete it. This protects against a message that triggers an application error each time it is processed.
// Retrieve storage account from connection string.CloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the queue client.CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();// Retrieve a reference to a queue.CloudQueue queue = queueClient.GetQueueReference("myqueue");// Get the message from the queue and update the message contents.CloudQueueMessage message = queue.GetMessage();
message.SetMessageContent("Updated contents.");
queue.UpdateMessage(message,TimeSpan.FromSeconds(0.0),// Make it visible immediately.MessageUpdateFields.Content|MessageUpdateFields.Visibility);
How to: De-queue the next message
Your code de-queues a message from a queue in two steps. When you call GetMessage, you get the next message in a queue. A message returned from GetMessage becomes invisible to any other code reading messages from this queue. By default, this message stays invisible for 30 seconds. To finish removing the message from the queue, you must also call DeleteMessage. This two-step process of removing a message assures that if your code fails to process a message due to hardware or software failure, another instance of your code can get the same message and try again. Your code calls DeleteMessage right after the message has been processed.
// Retrieve storage account from connection stringCloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the queue clientCloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();// Retrieve a reference to a queueCloudQueue queue = queueClient.GetQueueReference("myqueue");// Get the next messageCloudQueueMessage retrievedMessage = queue.GetMessage();//Process the message in less than 30 seconds, and then delete the message
queue.DeleteMessage(retrievedMessage);
How to: Leverage additional options for de-queuing messages
There are two ways you can customize message retrieval from a queue. First, you can get a batch of messages (up to 32). Second, you can set a longer or shorter invisibility timeout, allowing your code more or less time to fully process each message. The following code example uses the GetMessages method to get 20 messages in one call. Then it processes each message using a foreach loop. It also sets the invisibility timeout to five minutes for each message. Note that the 5 minutes starts for all messages at the same time, so after 5 minutes have passed since the call to GetMessages, any messages which have not been deleted will become visible again.
// Retrieve storage account from connection string.CloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the queue client.CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();// Retrieve a reference to a queue.CloudQueue queue = queueClient.GetQueueReference("myqueue");foreach(CloudQueueMessage message in queue.GetMessages(20,TimeSpan.FromMinutes(5))){// Process all messages in less than 5 minutes, deleting each message after processing.
queue.DeleteMessage(message);}
How to: Get the queue length
You can get an estimate of the number of messages in a queue. The FetchAttributes method asks the Queue service to retrieve the queue attributes, including the message count. The ApproximateMethodCount property returns the last value retrieved by the FetchAttributes method, without calling the Queue service.
// Retrieve storage account from connection string.CloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the queue client.CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();// Retrieve a reference to a queue.CloudQueue queue = queueClient.GetQueueReference("myqueue");// Fetch the queue attributes.
queue.FetchAttributes();// Retrieve the cached approximate message count.int? cachedMessageCount = queue.ApproximateMessageCount;// Display number of messages.Console.WriteLine("Number of messages in queue: "+ cachedMessageCount);
How to: Delete a queue
To delete a queue and all the messages contained in it, call the Delete method on the queue object.
// Retrieve storage account from connection string.CloudStorageAccount storageAccount =CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the queue client.CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();// Retrieve a reference to a queue.CloudQueue queue = queueClient.GetQueueReference("myqueue");// Delete the queue.
queue.Delete();
Next steps
Now that you've learned the basics of queue storage, follow these links to learn how to do more complex storage tasks.
- View the queue service reference documentation for complete details about available APIs:
- Learn about more advanced tasks you can perform with Windows Azure Storage at Storing and Accessing Data in Windows Azure.
- View more feature guides to learn about additional options for storing data in Windows Azure.
- Use Table Storage to store structured data.
- Use Blob Storage to store unstructured data.
- Use SQL Database to store relational data.
[Windows Azure] How to use the Queue Storage Service的更多相关文章
- [Windows Azure] How to use the Table Storage Service
How to use the Table Storage Service version 1.7 version 2.0 This guide will show you how to perform ...
- [AWS vs Azure] 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage
这几天Nasuni公司出了一份报告,分析了各个云厂商的云存储的性能,包括Amazon S3,Azure Blob Storage, Google Drive, HP以及Rackspace.其中性能上A ...
- [Windows Azure] What is a Storage Account?
What is a Storage Account? A storage account gives your applications access to Windows Azure Blob, T ...
- [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5
.NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5 This tutorial series sh ...
- Azure Functions(三)集成 Azure Queue Storage 存储消息
一,引言 接着上一篇文章继续介绍 Azure Functions,今天我们将尝试绑定 Queue Storage,将消息存储到 Queue 中,并且学会适用于 Azure Functions 的 Az ...
- Windows Azure Storage (18) 使用HTML5 Portal的Azure CDN服务
<Windows Azure Platform 系列文章目录> Update:2015-04-15 如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的文档:Azu ...
- 最全的Windows Azure学习教程汇总
Windows Azure 是微软基于云计算的操作系统,能够为开发者提供一个平台,帮助开发可运行在云服务器.数据中心.Web 和 PC 上的应用程序. Azure 是一种灵活和支持互操作的平台,能够将 ...
- Configuring a Windows Azure Project
A Windows Azure project includes two configuration files: ServiceDefinition.csdef and ServiceConfigu ...
- 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序
原文 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 Jim ...
随机推荐
- Linux下ls与cp命令
Linux熟练的应用命令,才可以随心所欲~ ls 注意: ls -1 //每次只列出1个文件 cp 注意: cp -u xxx xxx //注意修改时间的先后
- django日志使用TimeRotateFileHandler
如果使用django的settings.py设置日志会产生一些问题. 问题描述 报错信息如下: Traceback (most recent call last): File "C:\Pyt ...
- http状态--status[查询的资料备注]
HTTP 状态消息 当浏览器从 web 服务器请求服务时,可能会发生错误. 从而有可能会返回下面的一系列状态消息: 1xx: 信息 消息: 描述: 100 Continue 服务器仅接收到部分请求,但 ...
- POJ 3905 Perfect Election (2-Sat)
Perfect Election Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 438 Accepted: 223 De ...
- webpack window 处理图片和其他静态文件
安装url-loader npm install url-loader --save-dev 配置config文件 { test: /\.(png|jpg)$/, load ...
- Docker 简单查看name和ip
原文地址:https://blog.csdn.net/wen_1108/article/details/78282268 查看docker name: sudo docker inspect -f=' ...
- 【struts2】Struts2的异常处理
在Action中execute方法声明为:public String execute() throws Exception,这样,Action可以抛出任何Exception. 1)自己实现异常处理 我 ...
- 【iOS开发-36】Bundle Identifier的中文字符变成-的问题
在创建新项目时,Bundle Identifier=Organization Identifier+Product Name.可是它们对中文的识别统一变成短横线 - . 所以在创建多个项目的时候,须要 ...
- docker 和 vagrant 作为程序发布 和 开发的独立而统一的运行环境
docker 和 vagrant 作为程序发布 和 开发的运行环境,可以提供打包程序,并使得程序运行在一个独立的虚拟环境中,避免程序发布到客户机之后,环境不一致导致的诸多问题. refer: ...
- /usr 的由来及/usr目录结构 [转]
一直好奇包罗众多程序的usr目录为什么叫usr呢?usr到底是什么的缩写,终于找到比较靠谱的答案了 /usr 的由来及/usr目录结构 作者 AN SHEN| 发布于 2011-11-05 在 lin ...