简体   繁体   English

如何轮询 azure 服务总线队列的消息?

[英]How to poll the azure service bus queue for messages?

How do I poll the azure service bus to continuously check for the messages?如何轮询 azure 服务总线以持续检查消息? Here is how I receive the message from the queue.这是我从队列中接收消息的方式。

   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

I want to continuously look for the message and then process it.我想不断地寻找消息然后处理它。

As George chen says, I think service bus queue trigger meets your requirement.正如 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"
  }
}

If on azure, the env var is on configuration settings instead of local.settings.json.如果在 azure 上,则 env var 位于配置设置上,而不是 local.settings.json。

在此处输入图像描述

When you do this, the trigger will help you to capture the information in the service bus queue.(instant)执行此操作时,触发器将帮助您捕获服务总线队列中的信息。(即时)

You can use the ServiceBusReceiver to receive messages persistently in v7.0.0您可以在 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.

相关问题 Python Azure 服务总线未从队列中检索消息 - Python Azure Service Bus Not retrieving messages from queue 如何关闭与 azure 服务总线队列的连接? - How to close connection with the azure service bus queue? 如何检查 Azure 服务总线队列是否为空? - How to check the Azure Service Bus queue is empty? 如果 Azure 服务总线队列触发器功能失败,如何将队列消息返回到 Azure 服务总线队列 - How to return queue message to Azure Service Bus Queue if Azure Service Bus Queue Trigger Func Fails 具有优先级的Azure Service Bus队列并处理docker容器以处理队列消息 - Azure Service Bus queue with priority AND Handling docker containers to process the queue messages 使用python在Azure服务总线中实现队列 - Implementation of a queue in Azure service bus using python Azure Service Bus-python客户端消息上的AutoRenewTimeout - Azure Service Bus - AutoRenewTimeout on messages for python client Azure 功能 - 对来自 Azure 服务总线的消息进行分组 - Azure Functions - grouping messages from Azure Service Bus Azure 服务总线队列发送和接收消息的测试 - Test of sending & receiving message for Azure Service Bus Queue azure function 服务总线 python - 读取一堆消息 - azure function service bus python - read bunch of messages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM