Developing Solutions for Microsoft Azure - AZ-204 Exam: Implementing Pub/Sub Messaging with Azure Service Bus

Implementing Pub/Sub Messaging with Azure Service Bus

Question

A company is implementing a publish-subscribe (Pub/Sub) messaging component by using Azure Service Bus.

You are developing the first subscription application.

In the Azure portal you see that messages are being sent to the subscription for each topic.

You create and initialize a subscription client object by supplying the correct details, but the subscription application is still not consuming the messages.

You need to ensure that the subscription client processes all messages.

Which code segment should you use?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

D.

Using topic client, call RegisterMessageHandler which is used to receive messages continuously from the entity.

It registers a message handler and begins a new thread to receive messages.

This handler is waited on every time a new message is received by the receiver.

subscriptionClient.RegisterMessageHandler(ReceiveMessagesAsync, messageHandlerOptions); Reference: https://www.c-sharpcorner.com/article/azure-service-bus-topic-and-subscription-pub-sub/

The correct answer is D: subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions).

Explanation:

When implementing a Pub/Sub messaging component using Azure Service Bus, messages are sent to a topic and then subscribers can receive these messages by creating a subscription to the topic. In this scenario, the subscription client has been initialized correctly but it is not consuming the messages, so we need to use RegisterMessageHandler method to process the messages.

The RegisterMessageHandler method is used to register a message handler callback function that will be called when a new message arrives at the subscription. It takes two parameters:

  1. The message handler callback function (ProcessMessagesAsync): This is a method that will be called for each message that is received by the subscription. You will need to implement this method to define how to process the message. The method should return a Task object.

  2. The message handler options (messageHandlerOptions): This is an object that contains configuration options for the message handler. You can set options such as the maximum number of concurrent calls to the message handler, the maximum time to wait for a message to be processed, and whether to automatically complete the message after it has been processed.

Therefore, option D is the correct answer, as it uses the RegisterMessageHandler method to specify the ProcessMessagesAsync method and message handler options to process the messages. Option A creates a new rule for the subscription, option B initializes the subscription client, and option C closes the subscription client, but none of these options will consume the messages.