0
0
Azurecloud~10 mins

Service Bus queues concept in Azure - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Service Bus queue client.

Azure
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)
Drag options to blanks, or click blank then click option'
Aconnection_str
Bqueue_name
Cclient
Dreceiver
Attempts:
3 left
💡 Hint
Common Mistakes
Using the queue name instead of the connection string.
Trying to use the client or receiver variables before they are defined.
2fill in blank
medium

Complete the code to send a message to the Service Bus queue.

Azure
from azure.servicebus import ServiceBusMessage

with client.get_queue_sender(queue_name=queue_name) as sender:
    message = ServiceBusMessage("Hello, Service Bus!")
    sender.[1]([message])
Drag options to blanks, or click blank then click option'
Areceive_messages
Bdelete_message
Ccreate_message
Dsend_messages
Attempts:
3 left
💡 Hint
Common Mistakes
Using receive_messages instead of send_messages.
Trying to create or delete messages directly.
3fill in blank
hard

Fix the error in receiving messages from the queue by completing the method name.

Azure
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)
Drag options to blanks, or click blank then click option'
Asend_messages
Breceive_messages
Cget_messages
Dfetch_messages
Attempts:
3 left
💡 Hint
Common Mistakes
Using send_messages on the receiver object.
Using non-existent methods like get_messages or fetch_messages.
4fill in blank
hard

Fill both blanks to create a dictionary of message bodies and complete messages.

Azure
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)
Drag options to blanks, or click blank then click option'
Areceive_messages
Bbody
Cmessage_id
Dsend_messages
Attempts:
3 left
💡 Hint
Common Mistakes
Using send_messages instead of receive_messages.
Using body instead of message_id as dictionary key.
5fill in blank
hard

Fill all three blanks to send a batch of messages with custom content.

Azure
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)
Drag options to blanks, or click blank then click option'
A"Message " + str(i)
Bsend_messages
Creceive_messages
D"Hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to concatenate strings inside the list comprehension incorrectly.
Using send_messages on the receiver or receive_messages on the sender.