Back to snippets

azure_servicebus_async_batch_send_receive_queue_messages.py

python

This quickstart demonstrates how to send a batch of messages to an Azur

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
4
5NAMESPACE_CONNECTION_STR = "Endpoint=sb://<your-namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<your-key>"
6QUEUE_NAME = "<your-queue-name>"
7
8async def send_single_message(sender):
9    # Create a ServiceBusMessage object
10    message = ServiceBusMessage("Single Message")
11    # Send the message to the queue
12    await sender.send_messages(message)
13    print("Sent a single message")
14
15async def send_a_list_of_messages(sender):
16    # Create a list of ServiceBusMessage objects
17    messages = [ServiceBusMessage("Message in list") for _ in range(5)]
18    # Send the list of messages to the queue
19    await sender.send_messages(messages)
20    print("Sent a list of 5 messages")
21
22async def send_batch_message(sender):
23    # Create a batch object
24    async with sender:
25        batch_message = await sender.create_message_batch()
26        for _ in range(10):
27            try:
28                # Add a message to the batch
29                batch_message.add_message(ServiceBusMessage("Message inside a ServiceBusMessageBatch"))
30            except ValueError:
31                # ServiceBusMessageBatch object reaches max_size.
32                # New ServiceBusMessageBatch object can be created here to send more data.
33                break
34        # Send the batch of messages to the queue
35        await sender.send_messages(batch_message)
36    print("Sent a batch of 10 messages")
37
38async def run():
39    # Create a Service Bus client using the connection string
40    async with ServiceBusClient.from_connection_string(
41        conn_str=NAMESPACE_CONNECTION_STR,
42        logging_enable=True
43    ) as servicebus_client:
44        # Get a Queue Sender object to send messages to the queue
45        sender = servicebus_client.get_queue_sender(queue_name=QUEUE_NAME)
46        async with sender:
47            # Send messages
48            await send_single_message(sender)
49            await send_a_list_of_messages(sender)
50            await send_batch_message(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())