สร้างและจัดการ Azure Queue Storage และข้อความโดยใช้ .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();
}