如何持续收听 Azure 服务总线主题?

问题描述

我正在使用 Flask 运行一项服务,当在某个主题上发布消息时,需要触发一些代码。我不确定如何创建订阅名称等等,但我想我可以破解它。我真正不知道的是如何持续收听这个主题并启动过滤器,以便每当满足条件的消息时,它总是被触发。有什么地方可以指引我去吗?

解决方法

你最好使用 Azure Functions,只要有新消息到达主题就会触发:

import azure.functions as func

import logging
import json

def main(msg: func.ServiceBusMessage):
    logging.info('Python ServiceBus queue trigger processed message.')

    result = json.dumps({
        'message_id': msg.message_id,'body': msg.get_body().decode('utf-8'),'content_type': msg.content_type,'expiration_time': msg.expiration_time,'label': msg.label,'partition_key': msg.partition_key,'reply_to': msg.reply_to,'reply_to_session_id': msg.reply_to_session_id,'scheduled_enqueue_time': msg.scheduled_enqueue_time,'session_id': msg.session_id,'time_to_live': msg.time_to_live,'to': msg.to,'user_properties': msg.user_properties,'metadata' : msg.metadata
    })

    logging.info(result)

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=python