8 March 2010 0 Comments

Windows Azure入门教学系列 (五):使用Queue Storage

本文是 Windows Azure 入门教学 的第五篇文章。 本文将会介绍如何使用 Queue Storage 。Queue Storage提供给我们一个云端的队列。我们可以用Queue Storage来进行进程间的相互通信(包括运行在不同机器上的进程之间的通信)。一个使用Queue Storage经典的场景是,在一个Web应用程序中,用户通过表单递交给服务器数据,服务器收到数据后将进行处理,而这一处理将花费很多时间。这种情况下,服务器端通过Queue Storage可以把用户递交的信息存储在队列中,后台再运行一个程序从队列中取得数据进行信息的处理。以往如果程序时运行在Windows操作系统上,那么我们可以使用 MSMQ 来做类似的工作。而Queue Storage的出现为我们提供了另一种选择。特别是在非Windows操作系统上,我们依然可以使用Queue Storage的 REST API来很方便地使用它。有关Queue Storage REST API的详细信息可以参考: http://msdn.microsoft.com/en-us/library/dd179363.aspx 为了方便.NET开发人员,我们在 SDK 中提供了 Microsoft.WindowsAzure.StorageClient 类来帮助发送 REST 请求。 要继续本教学,请确保自己的机体上安装了最新版的 Windows Azure Tools for Microsoft Visual Studio 。当前的最新版( 2010 年 2 月)可以从以下站点下载: http://www.microsoft.com/downloads/details.aspx?familyid=5664019E-6860-4C33-9843-4EB40B297AB6&displaylang=en 步骤一:创建解决方案和项目 由于我们要在本地模拟环境下测试 Blob Storage, 首先,请确保 Development Storage 的管理器程序已经启动。我们可以找到管理器的进程手动启动或者让 Visual Studio 帮助我们启动他。你可以参考第一篇教学文章来启动他: http://blogs.msdn.com/azchina/archive/2010/02/09/windows-azure-webrole.aspx 右击工具栏中 Development Fabric 的图标,选择 ”Show Development Storage UI” 。弹出如下图所示的窗口: 我们要关注的是 Service management 中Queue所在的一行。要确保 Status 为 Running 。 确认完毕后启动 Visual Studio ,并且新建两个 Console 项目。我们将演示如何在一个Console程序中往Queue Storage中添加信息然后另外一个Console程序如何读取并处理信息。 步骤二:添加SDK程序集引用 在两个Console项目中均添加对 C:Program FilesWindows Azure SDKv1.1refMicrosoft.WindowsAzure.StorageClient.dll的引用。该路径为SDK默认安装路径,如果你不能在这个路径中找到Microsoft.WindowsAzure.StorageClient.dll请从SDK安装路径中寻找。 步骤三:添加 代码 首先在两个项目中的Program.cs中均引用命名空间: using System.Threading; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; 然后在其中一个项目(为了叙述方便,后面称之为Client项目)的Main方法中加入如下代码,我们将用它来向Queue Storage中添加信息。 static void Main( string [] args)         {             var storageAccount = CloudStorageAccount .DevelopmentStorageAccount;             var queueStorage = storageAccount.CreateCloudQueueClient();             // 检查名为 helloworldqueue 的队列是否被创建,如果没有,创建它             var queue = queueStorage.GetQueueReference( “helloworldqueue” );             queue.CreateIfNotExist();             Console .WriteLine( “Client is running…” );             while ( true ) {                 // 插入数据到队列中                 queue.AddMessage( new CloudQueueMessage ( string .Format( “client sent information: {0}” , DateTime .UtcNow.ToString())));                 // 每次插入数据后线程休息 3 秒                 Thread .Sleep(3000);             }         } 接着在另外一个项目(为了叙述方便,后面称之为Server项目)的Main方法中加入如下代码,我们将用它来从Queue Storage中读取信息和进行处理。 static void Main( string [] args)         {              var storageAccount = CloudStorageAccount .DevelopmentStorageAccount;             var queueStorage = storageAccount.CreateCloudQueueClient();             // 检查名为 helloworldqueue 的队列是否被创建,如果没有,创建它             var queue = queueStorage.GetQueueReference( “helloworldqueue” );             queue.CreateIfNotExist();             Console .WriteLine( “Server is running…” );             while ( true )             {                 // 从队列中读取一条信息                 // 收到信息后可以根据收到的信息做处理,为了演示方便我们这里只是把信息显示出来                 // 在云端发送消息后这条消息将对于后续的请求不可见,但是并未被删除。我们需要显示删除它。                 // 否则在一段时间后该消息将重新可见。这一设计的好处是确保了所有消息都能够被处理。                 // 如果程序在收到消息后处理消息前就异常终止了那么数据依然在一段时间后可以被重新处理。                 // 详情请参考 MSDN 文档                 var message = queue.GetMessage();                 if (message != null )                 {                     Console .WriteLine( string .Format( “Message retrieved: {0}” , message.AsString));                     // 处理完数据后必须显示删除消息                     queue.DeleteMessage(message);                 }                  // 每次读取数据后线程休息 3 秒                 Thread .Sleep(3000);             } 步骤四:观察并分析代码 步骤三中的代码中,首先我们通过CloudStorageAccount.DevelopmentStorageAccount来说明我们使用的本地的Development Storage自带账户而不是真正的云端存储服务账户。(如果要用真实账户可以使用 CloudStorageAccount.Parse(“DefaultEndpointsProtocol=http s ;AccountName= [用户名] ;AccountKey= [密码] “); //DefaultEndpointsProtocol=https 可以改成 DefaultEndpointsProtocol=http 表示用 HTTP 而不是 HTTPS 来 实例化对象)然后通过该账户类来实例化一个Queue客户端类。这两步是使用SDK中StorageClient程序集来调用Queue Storage服务的必要步骤。 Client项目中的代码相对简单。大约每隔3秒向Queue Storage中插入一条数据。Server端的项目中我们则大约每隔3秒从Queue Storage中读取一条数据。处理完毕后将之删除。对于为什么必须在读取后显示删除的问题请参考注释部分。 步骤五: 运行 程序 先运行Client程序然后运行Server个程序。如果一切正常,你将会看到Server项目的Console程序输出收到的消息: 注意由于是本地模拟,Client和Server程序需要要在同一台机器上运行。但是如果使用真实账户的话,Client和Server程序就可以跨越机器进行通信了。

Go here to read the rest: 
Windows Azure入门教学系列 (五):使用Queue Storage

If you liked this post, buy me a Coffee.

Leave a Reply