Azure Learn AZ-204 The Hard Way | 實作 AZ-204 Lab 10 Azure Service Bus Queues


  1. Lab10 - Azure Service Bus Queues
    1. Create Service Bus Queue
    2. Messages Publish Console
    3. Message Reader Console
  2. Azure Azure Service Bus Queues 相關知識
  3. 實戰 Azure Azure Service Bus Queues 心得

實作 AZ-204 證照測驗準備的實驗課程,從實驗課程中精熟 AZ-204 的測驗重點以及工具操作。本次要實驗的是如何使用 Azure Azure Service Bus Queues。

logo

Lab10 - Azure Service Bus Queues

詳細的實驗流程可以參考微軟在 Github上的專案

圖片來源:微軟

Create Service Bus Queue

建立 Service Bus Queue sbnamespacecs 並從 Shared access policies 複製 Primary Connection String 待會的程式端需要使用,

完成後建立一個 Queue messagequeue

Messages Publish Console

dotnet new console --name MessagePublisher --output .
dotnet add package Azure.Messaging.ServiceBus --version 7.2.1
dotnet build

將 queue 的 Connection String 以及 queueName 輸入。

Program.cs

using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;

namespace MessagePublisher
{
    public class Program
    {
        private const string storageConnectionString = "<storage-connection-string>";
        private const string queueName = "messagequeue";
        private const int numOfMessages = 3;

        static ServiceBusClient client;
        static ServiceBusSender sender;

        public static async Task Main(string[] args)
        {
            client = new ServiceBusClient(storageConnectionString);
            sender = client.CreateSender(queueName);

            using ServiceBusMessageBatch messageBatch = 
              await sender.CreateMessageBatchAsync();

            for (int i = 1; i <= numOfMessages; i++)
            {
                if (!messageBatch.TryAddMessage(new ServiceBusMessage($"Message {i}")))
                {
                    throw new Exception(
                      $"The message {i} is too large to fit in the batch."
                    );
                }
            }

            try
            {
                await sender.SendMessagesAsync(messageBatch);
                Console.WriteLine(
                  $"A batch of {numOfMessages} messages has been published to the queue."
                );
            }
            finally
            {
                await sender.DisposeAsync();
                await client.DisposeAsync();
            }
        }
    }
}

執行 Console 將訊息發佈到 Event Bus Queue

dotnet run

從 Event Buq Queue 可以觀察到相關資訊:

Message Reader Console

dotnet new console --name MessageReader --output .
dotnet add package Azure.Messaging.ServiceBus --version 7.2.1
dotnet build

Program.cs

using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;

namespace MessagePublisher
{
    public class Program
    {
        private const string storageConnectionString = "*";
        private const string queueName = "messagequeue";
        private const int numOfMessages = 3;

        static ServiceBusClient client;
        static ServiceBusSender sender;

        public static async Task Main(string[] args)
        {
            client = new ServiceBusClient(storageConnectionString);
            sender = client.CreateSender(queueName);

            using ServiceBusMessageBatch messageBatch = 
              await sender.CreateMessageBatchAsync();

            for (int i = 1; i <= numOfMessages; i++)
            {
                if (!messageBatch.TryAddMessage
                  (new ServiceBusMessage($"Message {i}")))
                {
                    throw new Exception($"The message {i} is too large to fit in the batch.");
                }
            }

            try
            {
                await sender.SendMessagesAsync(messageBatch);
                Console.WriteLine($"A batch of {numOfMessages} messages has been published to the queue.");
            }
            finally
            {
                await sender.DisposeAsync();
                await client.DisposeAsync();
            }
        }
    }
}

執行後可以將之前的訊息全部讀出:

Azure Azure Service Bus Queues 相關知識

實戰 Azure Azure Service Bus Queues 心得