0
votes

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.

2
What about the service bus trigger function?docs.microsoft.com/en-us/azure/azure-functions/…George Chen
@GeorgeChen I am working without a topic. It is a simple queue.Amanda
You could have a try, it supports to configure the topic and the queue name.docs.microsoft.com/en-us/azure/azure-functions/…George Chen
@Amanda Even you choose basic price tier Service bus and dont have topic, you can also use azure function service bus queue trigger.Cindy Pau
@Amanda I think azure function service bus queue trigger meets your requirements.Cindy Pau

2 Answers

0
votes

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.

enter image description here

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

0
votes

You can use the ServiceBusReceiver to receive messages persistently in v7.0.0

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.