Complete the code to create a Service Bus queue client.
from azure.servicebus import ServiceBusClient connection_str = "Endpoint=sb://example.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=key" queue_name = "myqueue" client = ServiceBusClient.from_connection_string([1]) receiver = client.get_queue_receiver(queue_name=queue_name)
The ServiceBusClient is created using the connection string to connect to the Azure Service Bus namespace.
Complete the code to send a message to the Service Bus queue.
from azure.servicebus import ServiceBusMessage with client.get_queue_sender(queue_name=queue_name) as sender: message = ServiceBusMessage("Hello, Service Bus!") sender.[1]([message])
The method send_messages is used to send messages to the queue.
Fix the error in receiving messages from the queue by completing the method name.
with client.get_queue_receiver(queue_name=queue_name) as receiver: messages = receiver.[1](max_message_count=5, max_wait_time=10) for msg in messages: print(str(msg)) receiver.complete_message(msg)
The receive_messages method is used to get messages from the queue.
Fill both blanks to create a dictionary of message bodies and complete messages.
with client.get_queue_receiver(queue_name=queue_name) as receiver: messages = list(receiver.[1](max_message_count=3)) results = {msg.[2]: msg for msg in messages} for msg in messages: receiver.complete_message(msg)
We receive messages with receive_messages and use message_id as the dictionary key.
Fill all three blanks to send a batch of messages with custom content.
from azure.servicebus import ServiceBusMessage messages = [ServiceBusMessage([1]) for i in range(3)] with client.get_queue_sender(queue_name=queue_name) as sender: sender.[2](messages) with client.get_queue_receiver(queue_name=queue_name) as receiver: received = receiver.[3](max_message_count=3) for msg in received: print(str(msg)) receiver.complete_message(msg)
Each message is created with the content "Message " plus the index. We send messages with send_messages and receive them with receive_messages.