إنشاء وإدارة تخزين Azure Queue والرسائل باستخدام .NET

مكتمل

في هذه الوحدة، نقوم بتغطية كيفية إنشاء قوائم الانتظار وإدارة الرسائل في Azure Queue Storage من خلال عرض أجزاء التعليمات البرمجية من مشروع .NET.

تعتمد أمثلة التعليمات البرمجية على حزم NuGet التالية:

إنشاء عميل خدمة «قائمة الانتظار»

تمكنك الفئة 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();
}