https://learn.microsoft.com/ja-jp/python/api/overview/azure/servicebus?view=azure-python
-- 1. 前作業
az login --use-device-code
az account show
az version
az configure --list-defaults
az configure --defaults location=japaneast
az configure --list-defaults
az group create \
--name rg9999999 \
--location japaneast
az group list
-- 2. Service Bus 名前空間を作成
az servicebus namespace create \
--resource-group rg9999999 \
--name ns123 \
--location japaneast \
--sku Standard
az servicebus namespace list \
--resource-group rg9999999
-- 3. 名前空間にトピックを作成
az servicebus topic create \
--resource-group rg9999999 \
--namespace-name ns123 \
--name topic01
az servicebus topic list \
--resource-group rg9999999 \
--namespace-name ns123
-- 4. サブスクリプションを作成
az servicebus topic subscription create \
--resource-group rg9999999 \
--namespace-name ns123 \
--topic-name topic01 \
--name ss01
az servicebus topic subscription list \
--resource-group rg9999999 \
--namespace-name ns123 \
--topic-name topic01
-- 5. 名前空間のプライマリ接続文字列を取得
az servicebus namespace authorization-rule keys list \
--resource-group rg9999999 \
--namespace-name ns123 \
--name RootManageSharedAccessKey \
--query primaryConnectionString \
--output tsv
-- 6. メッセージをトピックに送信する
vim a.py
from azure.servicebus import ServiceBusClient, ServiceBusMessage
CONNECTION_STR = "Endpoint=sb://ns123.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
TOPIC_NAME = "topic01"
SUBSCRIPTION_NAME = "ss01"
def send_single_message(sender):
message = ServiceBusMessage("Single Message")
sender.send_messages(message)
print("Sent a single message")
servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True)
with servicebus_client:
sender = servicebus_client.get_topic_sender(topic_name=TOPIC_NAME)
with sender:
send_single_message(sender)
print("Done sending messages")
print("-----------------------")
with servicebus_client:
receiver = servicebus_client.get_subscription_receiver(topic_name=TOPIC_NAME, subscription_name=SUBSCRIPTION_NAME, max_wait_time=5)
with receiver:
for msg in receiver:
print("Received: " + str(msg))
receiver.complete_message(msg)
-- 7. アプリを実行する
pip3 install azure-servicebus
python3 a.py
-- 8. クリーンアップ
az servicebus topic subscription delete \
--resource-group rg9999999 \
--namespace-name ns123 \
--topic-name topic01 \
--name ss01
az servicebus topic delete \
--resource-group rg9999999 \
--namespace-name ns123 \
--name topic01
az servicebus namespace delete \
--resource-group rg9999999 \
--name ns123
az group list
az group delete \
--name rg9999999 \
--yes