How to poll the azure service bus queue for messages?

一曲冷凌霜 提交于 2020-06-01 05:48:25

问题


How do I poll the azure service bus to continuously check for the messages? 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.


回答1:


As George chen says, I think service bus queue trigger meets your requirement.

_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.

When you do this, the trigger will help you to capture the information in the service bus queue.(instant)



来源:https://stackoverflow.com/questions/61927623/how-to-poll-the-azure-service-bus-queue-for-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!