Practice Exams:

AZ-204 Microsoft Azure Developer Associate – Connect to and consume Azure and third-party services part 5

  1. AZ-203/ 204 – More on Azure Service Bus

So now, after having the introduction on to the Azio Service Bus service, let’s understand some more aspects of the service. So this service is a fully managed enterprise integration message broker. It is used as a medium for application components to send and receive messages and it is mainly used to decouple application components. Now, as part of the Azure Service Bus, you have two features which are available. So you could create something known as Azure service Bus queues or Azure service Bus. Topics So let’s go over the first aspect which is Azure Service Bus queues. So in this you could have an application component which behaves as the sender. That application component or sender will send a message onto something known as a queue.

So a queue is something that you create within the Azure Service Bus. And just like a normal queue, the message is sent by the sender onto the queue. So you could have multiple messages in the queue and then on the right hand side you will have a receiver. So this could be another application component that’s actually reading messages from the queue. Now, apart from the queue, you also have topics. Now, the key difference over here is that you could have multiple receivers or subscribers for a single topic. So let’s say over here you have three receivers or three subscribers for a particular topic. Now, for each message which is sent by the sender, that message is sent to all of the subscribers. That’s why I’m showing three different boxes over here. Each box, remember, is not a queue.

It’s basically the message holder for each receiver or each subscriber. So this is like a fan out scenario of your messages. So let’s say you want to send a notification from your application to all the users who have subscribed to your application. So you just send one message, that message will automatically go to all the subscribers. This is the meaning of fanning out or sending messages to a group of subscribers. So some of the features of Azure Service Bus are if you want to guarantee a first in and a first out scenario for a queue, then you can enable a checkbox or something known as sessions. So if you enable sessions for a queue, then you can be guaranteed a first in, first out scenario for the queue. There’s also a concept known as auto forwarding.

So here the service can automatically chain one queue to another queue or a topic. So if the message is sent to one queue, if it is chained to another queue or a topic, that message will be automatically forwarded to the other queue or topic. We then have dead lettering. This is used to hold messages that cannot be delivered to any receiver or messages that cannot be processed for topics. The receivers or subscribers can also use something known as filters. So filters can be used to ensure that receivers only receive particular type of messages from the topic. Right? So this marks the end of this chapter. Let’s now move on to the labs where we’ll see more on how to implement Azure service bus.

  1. AZ-203/ 204 – Lab – Azure Service Bus Queue

Hi and welcome back. Now in this lab we are going to see how to implement the queue part of the Azure service bus. So first we’re going to create something known as a service bus namespace. The namespace is used to hold your queues and your topics. We then go ahead and create a queue and then we’ll use a net program to send and receive a message from the queue. So let’s go ahead with our lab. So here we are in a zero. Let’s go ahead and create a resource. We can search for service bus, we can go ahead and click on Create. We can give a name for the service bus, we can view the full pricing details. Now I’m going to use the standard pricing model because it allows it to use queues and topics. In basic you’re only allowed to use queues. So let me choose that. I’ll choose my resource group, choose the location and go ahead and create this namespace.

Now once the service bus namespace is in place, you can go on to it. Now you can go ahead and create a new queue. You can give a name for the queue. Now you can specify what is the max queue size. You can also specify what is the time to live for the message. So let’s say that I give a time of just 1 minute. So when a message is sent to the queue, a receiver has up to 1 minute to go ahead and process that message or to basically read and process that message. If no one goes ahead and reads the message for 1 minute, that message will be deleted from this particular queue. You also have remember the concept of enable sessions. So this ensures the first in first out scenario for your queue. You also have dead lettering on message expiration. So in case if messages don’t get processed you can send them on to a dead lettering queue.

So let’s go ahead and hit on Create. Right, so now the queue is in place. If you go on to queues over here you can go ahead to the queue. So here you can see the queue URL. You can also see the stats for the queue. So here you can also see the active message count if you go on to the properties. Here is where you can actually change the properties for the queue. You can also disable the queue at any point in time. Now currently over here there is no provision to go ahead and actually send a message to the queue. So we’re going to be doing this from a dotnet program. So now let’s see the dotnet program which can be used to send and receive messages from the queue. Now let’s look at the dotnet program for working with the Azure service bus queue. So if we go on to tools, let’s go on to our NuGet packages just to see what we have. So in the installed.

What I have done is that I’ve gone ahead and installed Microsoft Azure service bus. This is the new Get package I’ve installed. As part of this solution. Let’s go back on to our program. So first we have our connection string. So we have to connect to Azure service bus and then we have the name of our queue. So how do we get the connection string? We can go on to a zero, we can go on to our service bus, we can go on to Shared Access policy, we can go on to the default policy which is in place, which allows this particular key to manage, send and listen on to the resources in our service bus. We can take either primary or the secondary connection string. So we can go ahead and click on Copy. We can go onto our program and let’s copy it over here.

Now, once you’ve copied that in my main method, I’m basically calling Asynchronous function. Now in the Asynchronous function, now in the Asynchronous function I’m creating a new cue client using the connection string to our service bus namespace and the queue name. And then I have two methods. So one is to send a message and the other is to receive a message. So if I go on to the method to send a message, the first is the message body. So what exactly do you want to send? You have to encode the message and then you use the qClient send a sync to go ahead and send the message. Very simple. Now, if you go on to the task of receiving a message, the first thing that you have to do is to associate a method that will be used to receive the message.

So this is basically your handler for the messages. So what method is going to be used to handle the messages? You also need to have a method that can be used to handle the exceptions in case the message has an issue. So over here, the first thing you need to do is to create an options so you have the message handler options, wherein here you will mention the handler that will be used to handle all of the exceptions. Once that is done, you will then use the register message handler to register the method that will be used to process the message and obviously the options for the exception handler. Right? So this is how you first register the exception handler and the method which is used to process your messages.

Once that is done, so here I have my exception handler and then I have the message processor itself. So it’s very simple. All I’m doing is I’m getting the body of the message and writing it to the console. You also have to go ahead and make sure that you complete the processing of the message. This is to tell the queue that this particular module has gone ahead and received the message so let’s go ahead, run the program and see how it works. Before we go ahead and run the program, just to show you that currently in our queue, we don’t have any active messages. Now let’s go ahead and just send the message onto the queue. So it’s going ahead and sending the message to the queue.

If you go ahead and refresh page, you can now see that you have one active message in the queue. So remember that we had a setting of 1 minute. So if no application or module reads the message within 1 minute, then this message will be automatically deleted from the queue. Now let’s go ahead and enable the method to receive the message from the queue. So it’s receiving the message. And here you can see the body of the message from the queue. So very simple. So this marks the end of this chapter in which we have seen how to send and receive messages from the queue using net.

  1. AZ-203/ 204 – Lab – Azure Service Bus Topic

Hi and welcome back. In this lab, let’s see how to implement the topic part for the Azure service pass. So in this lab, we’ll go ahead and create a topic. In our service pass namespace, we are going to create multiple subscribers to the topic and then we’ll use a net program to send and receive a message from the topic. So let’s go ahead. So here we are back in Azure. So remember that for our namespace we had gone ahead and created a queue. Now we can go on to topics and add a topic. Let’s give a topic name again. You can specify the maximum size and the time to live. For the message I’ll leave everything as this and click on Create. Now remember, for a topic you can create multiple subscribers.

Remember, this is based on the fan out scenario. So now we have our topic in place. What we can do now is to go on to subscriptions. Or we can just go ahead and click on add a subscription. We can give a subscription name here we can mention the time to live just for the subscriber, the log duration and other aspects. For the subscription itself, I’ll go ahead and click on Create. I’ll go ahead and add another subscription. Click on create. So now I have two subscribers in place. Now we’ll go ahead and understand the dotted program that can be used to work with topics. So here we have the dotted program which will be used to work with topics. Now, like working with queues, most of it remains the same. So we have the same NuGet packages which are part of the solution.

We also have the same endpoint. We mentioned a topic name, but this time we are also going to mention a subscription name. So remember that when you are receiving a message, you have multiple subscribers. So you can have different clients. For each subscriber, I will also have what is the topic client. So I’ll have a client for the topic and I’ll have a client for the subscription as well. Please note to replace this subscription name and the topic name accordingly. Now, apart from that, most of it remains the same. So when you are sending a message on to the topic, it’s similar to how to send a message to the queue, even when you are receiving a message that is also the same. So you have an exception handler and you have a separate method that is used to process the message.

The only difference is that when you are reading a message, you will use the subscription client. So the client which actually connects to the subscription in that particular topic, right? Let’s go ahead and run this program and see how it works. Now on the program I’ve gone ahead and changed the subscription name, because this is the name we have in the topic if you go back onto a zero. So if I go on to the topics if I go on to our demo topic. So I have my subscribers. Please note that the message count in both is zero. Now let’s first go ahead and send a message to the topic. So here we’re sending a message to the topic.

If I go ahead and just refresh this blade, you can now see that you have a message count of one in each subscriber. So remember that even though we send just one message to the topic, the topic has gone ahead and spread the messages on to the different subscribers. Now we can go ahead and receive the message as receiver A, so one of the subscribers. So I’ll go ahead and click on Run. So now it’s going ahead and receiving the message. And here you can see the body of the message if you go on to the blade. If you click on Refresh, you can also see receiver A as zero message because it’s been read by our receiver program, right? So in this lab, we have seen how we could send and receive messages from a topic using net.

  1. AZ-203/ 204 – Azure Service Bus – Message Properties

Hi and welcome back. Now from an exam perspective, it’s important to understand a couple of things when it comes to the Azure Service Bus. So first we’re going to go on to the message properties. Now, when we looked at how to basically work with either an Azure Service Bus queue or an Azure Service Bus topic, we could send the message data it’s itself into something known as a message object. Now, for the object itself, you can also set various properties. So just to give a recap in the code wherein we had seen how to basically create a message that could be sent either to a queue or to a topic, we created an instance of a new message and then we encoded a message body. Now, this message class has other properties that you can set as well.

So you have system defined properties and you can also define your own user defined properties. Now some of the important system defined properties are please remember, you can also set what is the value of these properties. So first is the message ID. So this can be used to uniquely identify the message. This can be set by the application itself. You then have the reply to. So here the publisher of the message can set the value of the queue or the topic the consumer needs to send a response to. You then have the reply to session ID. So this is used when the consumer needs to reply specifically based on a session ID. And then you have the session ID itself. So if you want to make messages session aware, you can set the ID for the message and then you have the correlation ID.

So this is used for correlation purposes. Now let’s look at a diagram in which we’ll try to understand where you can actually use the system defined properties. So let’s say that you have a publisher. So you have one component that’s publishing a message onto the queue. Now in the message now this message could be published to a queue known as input queue. Now the message itself you could set the message property. So the message ID, you could make it as one. And then in the reply to property you can specify the name of another queue. So over here by specifying the name of the queue has output queue. Now when a consumer actually reads that message, so it will see that the message ID is one, it will see that there is a system defined property of reply two with a name of another queue.

So after the consumer process the message, the publisher now expects that the reply message to be in a queue known as output queue. So what the consumer would do is that after processing the message, it will then create its own reply message and then send it to a queue known as output queue. Now, when it sends the message to the output queue here in the correlation ID system property. It would take the message ID of the original message and then set that as the correlation ID. So here we’ve seen an example wherein you could use the system defined properties of message ID, reply to and the corelation ID. Now let’s go over on the Visual Studio. Let’s see how we can set these properties. Now here I am in Visual Studio. So we have our earlier code which used to work with topics and subscriptions.

So remember, you can set the message properties either when sending a message to a service bus queue or a service bus topic. Now, earlier on we had set the message body, but in addition to that, you could send or you could set, sorry, a system defined property such as message ID. So if you go on to the message itself, so you can see you could set the correlation ID, you can do something known as a label. So let’s say you want to define what should be the priority of this particular message. You could put that as a label. You have the message ID, the partition key. So these are all the system defined properties which are available which you can set for the message object. Now at the same time, if you only set your own custom property, then you can use the user properties collection, you can add your own key value pair to the property as a user property for your message.

As simple as that. So let me go ahead and first send the message I only have a service bus topic in place, so it’s gone ahead and sent the message. Now, if I go ahead and receive the message so here you can see you’re getting the body of the message and you’re also getting the message ID. And what is the user property that we set? Right, so this marks the end of this chapter wherein we have looked at the properties for the messages in either a service bus queue or a service bus topic.

  1. AZ-203/ 204 – Azure Service Bus – Subscription Filters

Hi and welcome back. Now in this chapter, let’s look at the Azio Service Bus, the topics, these subscription filters. So we’ve seen that in Azure Service Bus topics, you could have multiple subscribers that could subscribe to a topic. Now in Azure Service Bus topics, the subscribers can define what are the type of messages they want to receive. So by default, all the messages that are sent to the topic are available, available to all of the subscribers who subscribe to the topic. But let’s say that the subscribers only want to receive certain type of messages from the topic itself. They can do this with the help of filters. So the first type of filter you have is the Boolean filter. So you have either the true filter or the false filter.

Here the subscription can either decide to receive all of the messages or neither of the messages. So here you can use SQL like expressions to define what are the type of messages that you want to receive. You can create SQL filters against both user defined and system defined properties. You then have the correlation filter. So this is used to define a set of conditions. So let me show you how to do this within. So let’s go on to Visual Studio. Let’s see how we can get this done. So, here we are in Visual Studio. Now again, I have code which would send messages on to a topic. And then I have one subscriber that would subscribe to topic. Now when sending a message on the topic, I’m going to send two messages. Now the first message is, I’m going to send in the message body saying this is a high priority message.

I’ll set the message ID, let me change it over here. Now I’m going to add or define a label that’s a system defined property. I’ll keep this as high. So let me say that this is a high priority message. I’ll put the user categories and then I’ll basically send this message onto our topic. Now I’ll send another message saying this is a low priority message. I’ll put the message ID and the label has low and I’ll send that as well onto the topic. Now, when I’m going to add my subscription, I’m going to now add something known as a subscription rule. So in the rule you could use the different types of filters. So remember, you have the Boolean filter, the SQL filter, or the correlation filter. So over here I’m going to give a filter name and I’m going to say please create a new sequel filter.

And in the SQL filter I’ll mention that only this subscriber will get messages wherein the label system property is having a value of high. So that means if you want a subscriber to only go ahead and process high priority messages, then you can define a filter such as this. I will then go ahead and add the rule on to a subscription itself. Right? So this is how you can actually add filters. Now, please note that if you have multiple conditions, so let’s say you want to check both on the label and let’s say another system or user defined property, then you would go ahead and use the correlation filter. Very important. Now, in the program, we have given a name of a subscription as Receiver A.

So if you go on to a zero, I already have a topic in place. I have the receiver also in place, basically a subscription. Now, this code first is going to go ahead and create the subscription and the rule as well, and then send a message. So remember the sending two messages onto the topic. But what I’m doing is that I am also adding that particular rule on to the subscription itself. Now let’s go ahead, let’s run the code. So the code will actually basically add the rule and it will also send a message onto the topic. So remember, it’s adding two messages over here, right? So going ahead and sending both of the messages.

Now, if I go on to Azure, let me go ahead and refresh this page. Now, you can see that even though we have sent two messages on to our topic, we can see that there is only one message count for the subscription receiver A. And that’s because of the filter we have set on this subscription. If I go ahead and run the code to basically receive the message, since the rule is only defined, we don’t need the rule anymore. So it’s going ahead and receiving the message and it’s gone ahead and received the message wherein this is a high priority message, right? So this is how you can actually add filters for your subscriptions in Azure service Bus topics.