[英]How to poll the azure service bus queue for messages?
如何輪詢 azure 服務總線以持續檢查消息? 這是我從隊列中接收消息的方式。
from azure.servicebus import QueueClient
client = QueueClient.from_connection_string(
q_string,
q_name)
msg = None
with client.get_receiver() as queue_receiver:
messages = queue_receiver.fetch_next(max_batch_size=1, timeout=3)
if len(messages) > 0:
msg = messages[0]
print(f"Received {msg.message}")
return msg
我想不斷地尋找消息然后處理它。
正如 George chen 所說,我認為服務總線隊列觸發器符合您的要求。
_init_.py:
import logging
import azure.functions as func
def main(msg: func.ServiceBusMessage):
logging.info('Python ServiceBus queue trigger processed message: %s',
msg.get_body().decode('utf-8'))
function.json:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "msg",
"type": "serviceBusTrigger",
"direction": "in",
"queueName": "test",
"connection": "str"
}
]
}
env var:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=lti/ThmF+mw9BebOacp9gVazIh76Q39ecikHSCkaTcGK5hmInspX+EkjzpNmvCPWsnvapWziHQHL+kKt2V+lZw==;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "python",
"str": "Endpoint=sb://bowmantest.servicebus.xxxxxxx"
}
}
如果在 azure 上,則 env var 位於配置設置上,而不是 local.settings.json。
執行此操作時,觸發器將幫助您捕獲服務總線隊列中的信息。(即時)
您可以在 v7.0.0 中使用 ServiceBusReceiver 持久接收消息
from azure.servicebus import ServiceBusClient
import os
connstr = os.environ['SERVICE_BUS_CONNECTION_STR']
queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']
with ServiceBusClient.from_connection_string(connstr) as client:
# max_wait_time specifies how long the receiver should wait with no incoming
# messages before stopping receipt.
# Default is None; to receive forever.
with client.get_queue_receiver(queue_name, max_wait_time=30) as receiver:
msg = receiver.next() # it's a generator
# If it is desired to halt receiving early, one can break out of the loop here safely.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.