Azure实战:如何创建一个Cloud 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;
}
}
}
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);
MessageQueue messageQueue = queueStorage.GetQueue(XML_PAYLOAD_QUEUE_NAME);
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);
MessageQueue messageQueue = queueStorage.GetQueue(XML_PAYLOAD_QUEUE_NAME);
0
相关文章