2010-12-24 14:00:38 来源:it168
Azure Queue提供了一个简单的异步工作调度机制,通过Azure Queue可以连接云应用程序的不同组件,Azure Queue的优点是高可用,持久,性能好,它提供了REST接口,允许非C#语言编写的应用程序从任何地方访问Queue,让云应用程序和本地应用程序集成不再困难,在云应用程序和本地应用程序都可以使用Azure,Queue,主要用途有两个:
· 消息通信总线
· 组件或功能模块解耦
可从W orker 角色或Web角色创建Queue,为了体验Worker角色和Web角色之间的协作,我们从Worker角色创建Queue。
在云服务解决方案中增加一个Worker角色
如图1所示,增加一个Worker角色并与服务关联起来,这个Worker角色定义的职责包括:
· 从配置检索账号信息
· 从云存储创建一个Queue存储容器
在Queue存储中创建Queue
图 1 为Queue初始化插入一个Worker角色
从Worker角色的启动处理程序创建Queue存储容器和Queue
图2显示了Azure Queue存储的数据对象模型, AcctQueueContainerMap 表用于映射Queue名到一个唯一的Queue ID,这个ID是 QueueContainer 和 Message 表的外键。
图 2 Azure Queue数据对象模型
注意Queue的名字只能是字母和数字,并且是大小写敏感的,只接受小写的名称。
创建Queue前,必须先实例化 QueueStorage , QueueStorage 接受一个账户信息作为参数,在开发环境中,账号信息可以硬编码进配置文件中,下面的代码显示了为Queue存储的配置文件内容,包括账号和HTTP端口:
<appSettings>
<add key = "AccountName" value="devstoreaccount1"/>
<add key = "AccountSharedKey" value="<ACCOUNT_KEY>"/>
<add key="QueueStorageEndpoint" value="http://127.0.0.1:10001" />
</appSettings>
使用配置文件中的账号信息创建Queue
[page]下面的代码截取自Worker角色项目的实现,其中粗体部分显示了如何创建一个Queue。
using System;
using System.Threading;
using Microsoft.ServiceHosting.ServiceRuntime;
using Microsoft.Samples.ServiceHosting.StorageClient;
using System.IO;
using System.Configuration;
using System.Net;
using System.Xml;
namespace CloudTableStorageService_WorkerRole
{
public class WorkerRole : RoleEntryPoint
{
public const string XML_PAYLOAD_Queue_NAME = "createxmlmessageQueue";
public const string XML_CONTAINER_NAME = "xmlpayload";
private Stream CreateXmlStreamBlob(byte [] byteData)
{
return new MemoryStream(byteData);
}
public override void Start()
{
QueueStorage QueueStorage =
QueueStorage.Create(StorageAccountInfo
.GetDefaultQueueStorageAccountFromConfiguration());
MessageQueue Queue = QueueStorage.GetQueue(XML_PAYLOAD_Queue_NAME);
bool containerAndQueueCreated = false;
while (!containerAndQueueCreated)
{
try
{
Queue.CreateQueue();
containerAndQueueCreated = true;
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ConnectFailure)
{
RoleManager.WriteToLog(
"Error",
string.Format("Connect failure! The most likely reason is that
the local Development Storage tool is not running or your storage account configuration is
incorrect. " +
"Message: '{0}'", e.Message)
);
System.Threading.Thread.Sleep(5000);
}
else
{
throw;
}
}
}
while (true)
{
try
{
Message msg = Queue.GetMessage();
if (msg != null)
{
string path = msg.ContentAsString();
RoleManager.WriteToLog("Information",
string.Format("Done with '{0}'", path));
}
else
{
Thread.Sleep(1000);
}
}
catch (StorageException e)
{
RoleManager.WriteToLog(
"Error",
string.Format("Exception when processing Queue item. Message: '{0}'",
e.Message)
);
}
}
}
public override RoleStatus GetHealthStatus()
{
// This is a sample worker implementation. Replace with your logic.
return RoleStatus.Healthy;
}
}
}
通过编程创建Queue
下面的代码显示了利用C#类代替阅读配置文件中的信息创建一个Queue,这部分代码可以替换前面的粗体部分代码。
string accountName = "devstoreaccount1";
string accountKey = "<ACCOUNT_KEY>";
string address = "http://127.0.0.1:10001";
StorageAccountInfo accountInfo =
new StorageAccountInfo(new Uri(address), null, accountName, accountKey);
QueueStorage QueueStorage = QueueStorage.Create(accountInfo);
免责声明:本网站(http://www.ciotimes.com/)内容主要来自原创、合作媒体供稿和第三方投稿,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证有关资料的准确性及可靠性,读者在使用前请进一步核实,并对任何自主决定的行为负责。本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。