Back to snippets

azure_servicebus_async_batch_send_receive_queue_messages.py

python

This quickstart demonstrates how to send a batch of messages and receiv

15d ago63 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
azure_servicebus_async_batch_send_receive_queue_messages.py
1import asyncio
2from azure.servicebus.aio import ServiceBusClient
3from azure.servicebus import ServiceBusMessage
4from azure.identity.aio import DefaultAzureCredential
5
6# Replace with your Service Bus namespace fully qualified domain name and queue name
7NAMESPACE_CONNECTION_STR = "YOUR_SERVICE_BUS_CONNECTION_STRING"
8QUEUE_NAME = "YOUR_QUEUE_NAME"
9
10async def send_single_message(sender):
11    # Create a ServiceBusMessage object and send it to the queue
12    message = ServiceBusMessage("Single Message")
13    await sender.send_messages(message)
14    print("Sent a single message")
15
16async def send_a_list_of_messages(sender):
17    # Create a list of messages and send it to the queue
18    messages = [ServiceBusMessage("Message in list") for _ in range(5)]
19    await sender.send_messages(messages)
20    print("Sent a list of 5 messages")
21
22async def send_batch_messages(sender):
23    # Create a batch of messages and send it to the queue
24    batch_message = await sender.create_message_batch()
25    for _ in range(10):
26        try:
27            # Add a message to the batch
28            batch_message.add_message(ServiceBusMessage("Message inside a ServiceBusMessageBatch"))
29        except ValueError:
30            # ServiceBusMessageBatch object reaches max_size.
31            # New ServiceBusMessageBatch object can be created here to send more messages.
32            break
33    await sender.send_messages(batch_message)
34    print("Sent a batch of 10 messages")
35
36async def run():
37    # create a Service Bus client using the connection string
38    async with ServiceBusClient.from_connection_string(
39        conn_str=NAMESPACE_CONNECTION_STR,
40        logging_enable=True
41    ) as servicebus_client:
42        # get a Queue Sender object to send messages to the queue
43        sender = servicebus_client.get_queue_sender(queue_name=QUEUE_NAME)
44        async with sender:
45            # send one message
46            await send_single_message(sender)
47            # send a list of messages
48            await send_a_list_of_messages(sender)
49            # send a batch of messages
50            await send_batch_messages(sender)
51
52        # get a Queue Receiver object to receive messages from the queue
53        receiver = servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME, max_wait_time=5)
54        async with receiver:
55            # receive messages from the queue
56            received_msgs = await receiver.receive_messages(max_message_count=20, max_wait_time=5)
57            for msg in received_msgs:
58                print("Received: " + str(msg))
59                # complete the message so that it is removed from the queue
60                await receiver.complete_message(msg)
61
62if __name__ == '__main__':
63    asyncio.run(run())