使用 .NET 创建和管理 Azure 队列存储和消息
在本单元中,我们将介绍如何通过显示 .NET 项目中的代码片段在 Azure 队列存储中创建队列和管理消息。
代码示例依赖于以下 NuGet 包:
- 适用于 .NET 的 Azure.Core 库:此包为新式 .NET Azure SDK 客户端库提供共享基元、抽象和帮助程序。
- 适用于 .NET 的 Azure.Storage.Common 客户端库:此包提供由其他 Azure 存储客户端库共享的基础结构。
- 适用于 .NET 的 Azure.Storage.Queues 客户端库:此包支持使用 Azure 队列存储来存储客户端访问的消息。
- 适用于 .NET 的 System.Configuration.ConfigurationManager 库:此包提供对客户端应用程序的配置文件的访问权限。
创建队列服务客户端
使用该 QueueClient
类可以检索存储在队列存储中的队列。 下面是创建服务客户端的一种方法:
QueueClient queueClient = new QueueClient(connectionString, queueName);
创建队列
此示例演示如何创建队列(如果它尚不存在):
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
// Create the queue
queueClient.CreateIfNotExists();
在队列中插入消息
若要将消息插入现有队列,请调用 SendMessage
该方法。 消息可以是字符串(采用 UTF-8 格式)或字节数组。 以下代码创建队列(如果队列不存在),并插入消息:
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
// Create the queue if it doesn't already exist
queueClient.CreateIfNotExists();
if (queueClient.Exists())
{
// Send a message to the queue
queueClient.SendMessage(message);
}
查看下一条消息
可以通过调用 PeekMessages
该方法来查看队列中的消息,而无需将其从队列中删除。 如果未传递 maxMessages
参数的值,则默认值是查看一条消息。
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
// Peek at the next message
PeekedMessage[] peekedMessage = queueClient.PeekMessages();
}
更改排队消息的内容
可以直接在队列中更改消息内容。 如果消息表示工作任务,则可以使用此功能更新工作任务的状态。 以下代码使用新内容更新队列消息,并将可见性超时设置为再延长 60 秒。 这会保存与消息关联的工作状态,并给客户端另一分钟以继续处理消息。
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
// Get the message from the queue
QueueMessage[] message = queueClient.ReceiveMessages();
// Update the message contents
queueClient.UpdateMessage(message[0].MessageId,
message[0].PopReceipt,
"Updated contents",
TimeSpan.FromSeconds(60.0) // Make it invisible for another 60 seconds
);
}
取消对下一条消息的排队
通过两个步骤将消息从队列中移除。 调用 ReceiveMessages
时,将得到队列中的下一条消息。 对从此队列读取消息的任何其他代码而言,从 ReceiveMessages
返回的消息将不可见。 默认情况下,此消息保持 30 秒不可见。 若要完成从队列中删除消息,还必须调用 DeleteMessage
。 删除消息的这两个步骤可确保如果代码由于硬件或软件故障而无法处理消息,则代码的另一个实例可以获取相同的消息,然后重试。 代码在处理消息后会立即调用 DeleteMessage
。
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
// Get the next message
QueueMessage[] retrievedMessage = queueClient.ReceiveMessages();
// Process (i.e. print) the message in less than 30 seconds
Console.WriteLine($"Dequeued message: '{retrievedMessage[0].Body}'");
// Delete the message
queueClient.DeleteMessage(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
}
获取队列长度
您可以获取队列中消息数量的估计值。 GetProperties
方法返回包括消息计数在内的队列属性。 ApproximateMessagesCount
属性包含队列中的大致消息数。 该数字不低于队列中的实际消息数,但可能高于实际消息数。
/// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
QueueProperties properties = queueClient.GetProperties();
// Retrieve the cached approximate message count.
int cachedMessagesCount = properties.ApproximateMessagesCount;
// Display number of messages.
Console.WriteLine($"Number of messages in queue: {cachedMessagesCount}");
}
删除队列
若要删除队列及其包含的所有消息,请对队列对象调用 Delete
方法。
/// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
// Delete the queue
queueClient.Delete();
}