如何在 Azure Service Bus Explorer 中获取最后一条消息属性

问题描述 投票:0回答:1

我当前在尝试使用远程计算机上的 Python 脚本访问 Service Bus Explorer 时遇到问题。我的脚本旨在查看最后一条消息并打印其属性,但它似乎遇到了一些阻碍其功能的障碍。

LinkDetach('ErrorCodes.NotAllowed: 需要会话的实体不可能创建非会话消息接收器。 TrackingId:c41edfdvfaf-7a70-4a88-924d-15ec4e61141d_B30, SystemTracker:G4:38014551:receiver-link-885e6d3fsdf -07c8-4b2d-9017-bfhdgdc0b31,时间戳:2024-02-22T06:01:29 TrackingId:0d0eee73bcdva447cdfdfs59ad9671c18e_G4,SystemTracker:gateway5,时间戳:2024-02-22T06:01:29'

以下是我的脚本:

#!/usr/bin/python3

from azure.servicebus import ServiceBusClient
from azure.identity import DefaultAzureCredential

def get_last_message_properties(namespace_name, topic_name, subscription_name):
try:
#Create a Service Bus client using DefaultAzureCredential
servicebus_client = ServiceBusClient(
fully_qualified_namespace=namespace_name,
credential=DefaultAzureCredential()
)

    # Get a subscription client
    subscription_client = servicebus_client.get_subscription_receiver(
        topic_name=topic_name,
        subscription_name=subscription_name
    )

    # Receive messages from the subscription
    with subscription_client:
        messages = subscription_client.receive(max_message_count=1)
        for message in messages:
            try:
                # Access message properties
                properties = message.properties
                cdId = properties.get("cdId")
                active = properties.get("active")
                alarm = properties.get("alarm")
                print(f"Last message properties: cdId={cdId}, active={active}, alarm={alarm}")
            except Exception as e:
                print(f"An error occurred: {e}")

except Exception as e:
    print(f"An error occurred: {e}")

if __name__ == "__main__":
namespace = "sbdev.servicebus.windows.net"
topic = "topicsystemlog_main"
subscription = "SubCardDBUpdater_20"
get_last_message_properties(namespace, topic, subscription)

预先感谢您的协助!

python azure azureservicebus
1个回答
0
投票

需要会话的实体不可能创建非会话消息接收器。

您的

SubCardDBUpdater_20
订阅已启用会话。当您尝试访问它时,您需要创建接收者并传入会话 ID(
session_id
调用的
servicebus_client.get_subscription_receiver()
参数)。这使您的任务更加复杂,因为它不仅仅是最后一条消息,而是最后一条消息在特定会话中。

© www.soinside.com 2019 - 2024. All rights reserved.