Azure Service Bus 運算子

Azure Service Bus 是一個完全託管的企業級訊息代理程式,具有訊息佇列和發布-訂閱主題(在命名空間中)。Service Bus 用於將應用程式和服務彼此解耦。Service Bus 在實體(例如命名空間、佇列和主題)上執行操作。

Service Bus REST API 提供了用於處理以下資源的操作
  • Azure Resource Manager

  • Service Bus 服務

Azure Service Bus 佇列運算子

Azure Service Bus 運算子有助於與基於 Azure Bus 佇列的操作互動,例如在佇列中建立、刪除、傳送和接收訊息。

建立 Azure Service Bus 佇列

若要使用特定參數建立 Azure service bus 佇列,您可以使用 AzureServiceBusCreateQueueOperator

以下是使用此運算子執行 Azure Service Bus 建立佇列的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

create_service_bus_queue = AzureServiceBusCreateQueueOperator(
    task_id="create_service_bus_queue",
    queue_name=QUEUE_NAME,
)

傳送訊息至 Azure Service Bus 佇列

若要將訊息或訊息列表或批次訊息傳送至 Azure Service Bus 佇列。您可以使用 AzureServiceBusSendMessageOperator

以下是使用此運算子執行 Azure Service Bus 傳送訊息至佇列的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

send_message_to_service_bus_queue = AzureServiceBusSendMessageOperator(
    task_id="send_message_to_service_bus_queue",
    message=MESSAGE,
    queue_name=QUEUE_NAME,
    batch=False,
)

接收 Azure Service Bus 佇列訊息

若要在佇列中接收訊息或訊息列表或批次訊息,您可以使用 AzureServiceBusReceiveMessageOperator

以下是使用此運算子執行 Azure Service Bus 建立佇列的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

receive_message_service_bus_queue = AzureServiceBusReceiveMessageOperator(
    task_id="receive_message_service_bus_queue",
    queue_name=QUEUE_NAME,
    max_message_count=20,
    max_wait_time=5,
)

刪除 Azure Service Bus 佇列

若要刪除 Azure service bus 佇列,您可以使用 AzureServiceBusDeleteQueueOperator

以下是使用此運算子執行 Azure Service Bus 刪除佇列的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

delete_service_bus_queue = AzureServiceBusDeleteQueueOperator(
    task_id="delete_service_bus_queue", queue_name=QUEUE_NAME, trigger_rule="all_done"
)

Azure Service Bus 主題運算子

Azure Service Bus 基於主題的運算子有助於與 service bus 命名空間中的主題互動,並有助於主題的建立、刪除操作。

建立 Azure Service Bus 主題

若要使用特定參數建立 Azure service bus 主題,您可以使用 AzureServiceBusTopicCreateOperator

以下是使用此運算子執行 Azure Service Bus 建立主題的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

create_service_bus_topic = AzureServiceBusTopicCreateOperator(
    task_id="create_service_bus_topic", topic_name=TOPIC_NAME
)

刪除 Azure Service Bus 主題

若要刪除 Azure service bus 主題,您可以使用 AzureServiceBusTopicDeleteOperator

以下是使用此運算子執行 Azure Service Bus 刪除主題的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

delete_asb_topic = AzureServiceBusTopicDeleteOperator(
    task_id="delete_asb_topic",
    topic_name=TOPIC_NAME,
)

Azure Service Bus 訂閱運算子

Azure Service Bus 基於訂閱的運算子有助於與 service bus 命名空間中的主題訂閱互動,並有助於主題下訂閱的建立、刪除操作。

建立 Azure Service Bus 訂閱

若要使用特定參數建立 Azure service bus 主題訂閱,您可以使用 AzureServiceBusSubscriptionCreateOperator

以下是使用此運算子執行 Azure Service Bus 建立訂閱的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

create_service_bus_subscription = AzureServiceBusSubscriptionCreateOperator(
    task_id="create_service_bus_subscription",
    topic_name=TOPIC_NAME,
    subscription_name=SUBSCRIPTION_NAME,
)

更新 Azure Service Bus 訂閱

若要使用特定參數更新已建立的 Azure service bus 主題訂閱,您可以使用 AzureServiceBusUpdateSubscriptionOperator

以下是使用此運算子執行 Azure Service Bus 更新訂閱的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

update_service_bus_subscription = AzureServiceBusUpdateSubscriptionOperator(
    task_id="update_service_bus_subscription",
    topic_name=TOPIC_NAME,
    subscription_name=SUBSCRIPTION_NAME,
    max_delivery_count=5,
)

接收 Azure Service Bus 訂閱訊息

若要從特定主題下的 Service Bus 訂閱接收批次訊息,您可以使用 ASBReceiveSubscriptionMessageOperator

以下是使用此運算子執行 Azure Service Bus 接收訂閱訊息的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

receive_message_service_bus_subscription = ASBReceiveSubscriptionMessageOperator(
    task_id="receive_message_service_bus_subscription",
    topic_name=TOPIC_NAME,
    subscription_name=SUBSCRIPTION_NAME,
    max_message_count=10,
)

刪除 Azure Service Bus 訂閱

若要刪除 Azure service bus 主題訂閱,您可以使用 AzureServiceBusSubscriptionDeleteOperator

以下是使用此運算子執行 Azure Service Bus 刪除主題下訂閱的範例。

tests/system/microsoft/azure/example_azure_service_bus.py[原始碼]

delete_service_bus_subscription = AzureServiceBusSubscriptionDeleteOperator(
    task_id="delete_service_bus_subscription",
    topic_name=TOPIC_NAME,
    subscription_name=SUBSCRIPTION_NAME,
    trigger_rule="all_done",
)

參考

如需更多資訊,請參閱 Microsoft 文件

這個條目有幫助嗎?