AZ-204 - Connect and Consume Services - Azure Service Bus

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] now in this chapter let's go ahead and create an azure service bus queue so over here in all resources let me go ahead and click on add and over here let me go ahead and search for service bus so i'll go ahead and choose service bus and i'll go ahead and hit on create now in order to go ahead and actually create a queue and a topic in service bus we have to go ahead and create something known as a service pass namespace so over here i'll go ahead and choose an existing resource group over here we need to go ahead and give us name for the namespace so it needs to be unique i'll go ahead and choose my location now if i go on to the full pricing details now over here you have the basic you have the standard and you have the premium now i'm going to go ahead and choose standard because over here you have the ability to go ahead and create both cues and topics so i'm going to go ahead and select standard i'll go on to next for the tags i'll go on to review and create and let me go ahead and hit on create so this might take around two to three minutes let's come back once we have the namespace in place now once you have the namespace in place i'll go ahead on to the resource and over here now we can go and create our queues and our topics if you go ahead and scroll down you can go on to the queue section as well and you can go ahead and create a queue from here so let me go ahead and hit on to create a new queue over here i can go ahead and give a queue name and you can go ahead and specify a lot of properties when it comes to the queue for the moment let me go ahead and leave everything as it is and let me go ahead and hit on create and now we have our cue in place let's move on to the next chapter to see how we can start adding messages on to the queue via the azir portal itself now in the last chapter we had gone ahead and create our queue in the azure service pass namespace now let me go on to the queue and over here you get a lot of information about the queue so for example over here you can see what is the current size based on the number of messages in the queue what is the max size of the queue what is the free space etc over here you also have the message counts now you can actually go on to the service bus explorer and over here you can go ahead send messages you can go ahead and receive messages and you could also go ahead and have a peek of the messages in the queue over here you can go ahead and choose the content type so if you want to go ahead and just send a simple text message or if you want to go ahead and send a message an xml base message or a json based message so let me go ahead and choose application json and over here let me go ahead and add these json messages so over here this is information about different orders so i'll go ahead and paste it over here and let me go ahead and click on send by going down let me go ahead and add the other message as well click on send add the other message over here click on send so we've gone ahead and sent out three messages onto the queue if you go on to the overview over here you can see the number of active messages over here you can see the current size has also changed going back on to the service bus explorer if i go on to the peak section over here i can go ahead and click on peak so it will go ahead and peek at the messages in the queue so the maximum is 32 messages so this is not going to go ahead and remove the messages from the queue it's only going to go ahead and have a look at the messages in the queue that's why you can see that the number of active messages is still three you can go on to any message by clicking on it and over here you can see the contents of that message if you go ahead and scroll down there is something known as broker properties wherein you have a message id delivery count etc if i now go on to receive over here you can go ahead and receive one message at a time i can go ahead and click on receive over here and it's saying do you want to go ahead and perform a destructive receive because what this will do is that this will go ahead and now remove one message from our queue so i'll go ahead and click on yes so now you can see we have two messages or active messages in our queue and over here you can see the message which was removed so you can actually go ahead and use the service bus explorer to go ahead and send and receive messages now in this chapter we'll see how to use a dotnet program to go ahead and send messages onto the queue in service bus firstly what i'll do is that i'll ensure to receive all the messages that i currently have in my queue so that i don't have any messages as part of my service bus queue now over here in visual studio let me go ahead and create a new project so i'll go ahead and create a console based application so i'll go and choose console application i'll go on to next over here let me go ahead and give a name i'll go on to next i'll go ahead and hit on create now over here let me go ahead and add the required nuget packages so that we can go ahead and work with the service bus queue so over here let me go ahead and search for service bus and let me go ahead and not choose microsoft.azure.servicebus i need to go ahead and actually choose a newer version when it comes to the service bus so i'm going to go ahead and search for messaging dot service bus and over here i am going to go ahead and choose azure dot messaging dot service bus version 7. so let me go ahead and install this package as part of my project now once this is done i am going to go ahead and create a new class so over here we'll go ahead and create objects of this class and we'll go ahead and now the details of this object has json string has messages in our queue so over here i'll go ahead and create a new class so i'll go ahead and name this has the order class over here let me go ahead and add some properties so over here i'm adding the properties of the order id the quantity and the unit price i'm also going ahead and overriding the tostring method which is available for the class by default and over here i want to go ahead and basically give a json string representation of my properties in my object so over here let me go ahead and ensure to use system.txt.json right so now we have this in place now i'll go on to my main program now let me go ahead and create a list of new orders so over here i want to go ahead and add these orders as messages on to my queue over here let me go ahead and now add two properties one is my connection string and the other is my q name so over here i have the connection string and my q name over here i need to go ahead and replace the connection string now there are two ways in which you can actually go ahead and get connection strings so in azure if you go on to your namespace if you go on to shared access policies over here by default there is a root manage shared access key now this key gives permissions onto all the queues and topics that are part of your service pass name space but if you only want to go ahead and specify the connection string of only that particular queue what you can do is that you can actually go on to the queues over here in the entities go on to your app queue and over here also you have the concept of shared access policies over here let me go ahead and click on add now over here since we only want to go ahead and send messages on to the queue by this program let me go ahead and just choose the permission of send and give a simple name for the policy and hit on create now once you have the policy in place just click on it just wait for a second and now over here you can go ahead and take either the primary or the secondary connection string so i'll go ahead and take the primary connection string i'll go on to my program and i'll replace it over here now the next step is to go ahead and now use the service bus client so this class allows us to go ahead and interact with the service bus so this is actually part of azure dot messaging dot service bus so over here let me go ahead and create an object based on the service bus client and over here we can actually go ahead and give our connection string so over here let me go ahead and give the connection string next we need to go ahead and use the class of service bus sender to go ahead and create an object that allows us to go ahead and send messages onto the queue so over here i can go ahead and use now my client dot create sender and over here i have to go ahead and give the name of the queue or the topic so over here let me go ahead and give the q name now next for each order that i have in my list of orders over here so in my orders so let me go ahead and create now a message based on the class of service pass message is equal to new service pass message and over here we can give the contents of the message so over here it's basically my order dot 2 string so remember we had gone ahead and ensure to basically override the two string method so that we return now a json string and that's the reason i did this so that the entire json string is now sent has a message next we need to go ahead and use now our sender dot send message sync and over here i need to go ahead and send my service bus message i'll use the get awaiter dot get result and over here then i can go ahead and say that all of the messages have been sent right so now let me go ahead and run this program right so all the messages have been sent if i now go on to my queue let me go on to the service pass explorer let me go on to peak and let me go ahead and hit on peak over here anyway i can see that i have five messages in place right so over here you can see all of the messages now over here it's showing the content type has application xml so this is based on basically the content of a message if you want to go ahead and now set the content type here is what you can do so if you go back on to our program for each message there are a lot of properties that are in place so over here you also have the content type so let me go ahead and turn this as application json and now let's go ahead and send the messages again on to the queue right all of them have been sent if i go back let me go ahead and hit on refresh so i can see 10 messages in place let me go ahead and hit on peak so if i go on to let's say message number 10 now over here i can see the content type as application json so over here now when it comes to your messages there are a lot of properties that are in place there are properties that you can actually make use of so in this chapter i want to show you how you can send messages on to your service pass queue via a.net program now in the last chapter we had seen how we can send messages on to the service pass queue that's from a.net program in this chapter let's see how to use dotnet to go ahead and peek at messages from our service pass queue so i'll go ahead and create a new project over here again i'll choose console application i'll go on to next and over here let me go ahead and give a name for the project hit on next i'll go ahead and hit on create so normally even in your application you'll actually have separate modules for sending messages onto the queue and separate modules for receiving messages from the queue now we need to go ahead and ensure to again have the required packages in place so the packages remain the same whether it be sending messages or receiving messages so in browse let me go ahead and again search for messaging dot service bus so i'll go ahead and choose azure dot messaging dot service bus i'll hit uninstall so i'll go ahead and hit on ok and accept i'll go back on to my program so over here let me go ahead and have the same two properties in place one is my connection string and the other is my q name now over here we still have our service bus client which remains the same but now this time since we have receiving messages i am going to go ahead and use the class of service pass receiver is equal to the client dot create receiver and over here again we have to go ahead and give our cue name now in order to go ahead and ensure that we only peek at messages in our queue over here for the create receiver method you can also go ahead and have a service bus receiver options object so over here let me go ahead and paste in some code so i'm going ahead and creating an object based on the class of service bus receiver options and over here in the receive mode i'm going ahead and entering as the peak lock receive mode now over here for the message itself so service pass received message that's another class is equal to my receiver dot receive message async again you can go ahead and receive one message or a set of messages let me go ahead and just receive one message for now over here let me go ahead and get the get avita dot get result and once we have our message in place so if you go on to the body of the message to string we should be able to go ahead and write it on to the console right so let's go ahead and run this program before actually run the program so over here you can see we have the messages from sequence number four onto sequence number 13. so let me go ahead and run the program now in the program i am actually getting an error and it is saying that we have on authorized access and that is because i had gone ahead and copied the same connection string from the last program but that's a mistake so if i go on to my shared access policy over here our policy was only to go ahead and send messages on to the queue so we have to go ahead and click on add and over here i have to go ahead and now choose the policy of listen i can go ahead and give a name for the policy hit on create now let me go on to the listen policy over here i can go ahead and take now the connection string i can go ahead and copy it on to my program and now let me go ahead and run this again and now over here you can see you're getting the output as desired now i'll go ahead and close this and over here if i go back on to the service pass explorer if i go on to the peak feature you can see that i still have 10 active messages in place it has not gone ahead and removed any message from my queue now in this chapter let's see how we can receive messages from our queue so over here firstly when i go to my service bus receiver options in the receiver mode let me go ahead and change the mode now on to receive and delete so now it will go ahead and receive a message and after receiving the message it will go ahead and delete the message over here now let's go ahead and receive multiple messages so over here let me go ahead and change the method so i'll go ahead and now do receive messages async and over here i can go and specify the number of messages so let me go ahead and specify the number of messages has three over here let me go ahead and use get beta dot get result now over here i won't get an object of the class service bus receive message then over here for each now message in my messages so i've gone and replaced as messages over here so let me go ahead and again display the body and if you also want to go ahead and display let's see what is the sequence number of the message you can go ahead and do that as well so you can go ahead and choose sequence number so you have the ability to go ahead and access a lot of properties of the message itself so let me go ahead and now run the program so you can see that we have the sequence number along with the messages itself now over here if i go ahead and hit on refresh so now you can see we have only seven messages in place so now our program has gone ahead received the message and deleted the message as well now in this chapter i want to go through this message lock duration setting that you have for the service pass queue even when you go ahead and define a service pass queue you can go ahead and define this message lock duration so by default it's 30 seconds so what is the meaning of this message lock duration so this comes into the picture when you're going ahead and basically using the peak method to go ahead and look at your messages so when you go ahead and receive messages there are two modes one is basically called the peak and lock mode and the other is called receive and delete in the receive and delete everything is one operation your program will go ahead and receive the message and it will be deleted automatically in the peak and lock method you have to go ahead and perform two operations so so the first is getting the message and the other is explicitly deleting the message so you get the message you process the message and then you go ahead and delete the message now between the time of receiving and processing the message and deleting the message during this time so your application that's on the consumer side is going in and performing some operation on the message now if you have multiple consumers that are going ahead and reading messages from the service queue you have to go ahead and make sure that the other application does not go ahead and pick up the same message because it is already being processed by the first application so that's where this message lock duration comes into play so when the first application goes ahead and picks up the message that message will be in the lock state for 30 seconds this means no other application can go ahead and pick up the same message once the lock duration is complete then the message will be available again in the message queue so it's up to your application to ensure that the message is processed in 30 seconds and then it is deleted from the queue so again there are advantages and disadvantages of both of these methods so during the receive and delete method let's say your application has crashed while it is going at and processing the message that message will have already been deleted so you can't go ahead and use another application to process that same message that message will be lost where has in the peak and lock method right once the application has received the message and let's say it has gone ahead and crashed during the processing of the message that message will again return back on to the queue because the application would have not gone it and performed the delete operation so again you have to go ahead and decide which method you want to go ahead and use for receiving messages whether you want to go ahead and use the peak and lock method or whether you want to go ahead and use the receive and delete method so now over here i have the code in place which is used for receiving messages from a service bus queue over here the receive mode is p clock now over here when you go ahead and receive the message if you want to go ahead and now delete the message you have to go ahead and use the method of complete message a sync and over here you have to go ahead and give the message itself so if you want to go ahead and ensure that with the peak and lock method you go ahead and delete the message you have to go ahead and use this method accordingly so over here i have that namespace in place the service pass namespace over here i'll go on to the service bus explorer over here let me go ahead and enter a very simple message and let me go ahead and hit on send so if i go on to the receive or the peak section i can see i have one active message in place now over here let me go ahead and run this program right so you can see the test message and you can see the sequence number so over here now if i go ahead and hit on refresh you can see you have zero active messages in place now in this chapter i want to go through the time to live feature which is available for the messages in your queue now if i go on to the ovio section for the queue there is something known has the message time to live so over here the default is 14 days so over here you're giving an expiration time for the messages because you don't want messages to live indefinitely in the queue you want some consumer to actually go ahead and consume the messages so by default the time to live is 14 days so this applies on to all messages in the queue and you can go ahead and change this accordingly but when sending messages on to the queue you also have the ability to go ahead and set the time to live for each individual message so that is also possible now over here i have the program in place that was used to go ahead and send a set of messages onto a service bus queue now just to go ahead and verify if i go ahead onto our service bus explorer if i go on to peak so currently i have seven active messages so going back on to our program this is going to go ahead and add five new messages now over here for each message there is a time to live property so over here i'm saying go ahead and give a time span of just 30 seconds so i'm saying please go ahead and ensure that there is only a time to live for each individual message in the queue for just 30 seconds so let me go ahead and run this program right so all of the messages have been sent if i go on to my queue if i go ahead and hit on refresh i can see 12 messages if i go ahead and peak the messages so over here i can see the new sequence numbers from 14 on to 18. now let me go ahead and wait for 30 seconds now i waited for around 30 seconds now let me go ahead and hit on refresh and now i can see i only have seven messages in the queue so the other messages that we had have now gone ahead and expired based on that time to live value over here i can go ahead and hit on peak again just to go ahead and look at the messages and over here the messages after sequence number 14 are no longer present now in this chapter i want to go through the dead letter q so over here you can actually see the option of the dead letter so what is the purpose of the dead letter queue so when your application is actually sending messages on to the queue if there were any issues in the processing of the message or if the time to live was reached for that particular message then the messages will be transferred onto the dead letter q so in the dead letter queue you can actually go ahead and look at the messages and understand the reason as to why the messages were sent on to the dead letter queue so remember this is for those messages that were not processed properly or if they have reached their time to live now in order to go ahead and have the dead letter q so if you go on to the overview so over here when it comes to dead lettering so over here if you click on disable on message expiration you can now go ahead and enable dead lettering on message expiration let me go ahead and hit on ok so now you are going ahead and enabling that dead letter q now what i'll do is that so over here earlier on we had seen the program wherein we could go ahead and set a message time to live for our messages so over here it was going in and writing five messages onto the queue so let me go ahead and run the program right so all of the messages have been sent if i go ahead on to my queue if i go ahead and hit on refresh so over here i can see i have 12 active messages now at the same time i've also gone ahead and open up the program that is used to go ahead and receive our messages now over here let me go ahead and again change the code to go ahead and receive a single message so service pass message and over here let me go ahead and just receive one message let me ensure it's of the class service pass receive message and then over here i'll go ahead and add code wherein we can actually go ahead and find now what is the reason that the message was sent on to the dead letter queue so i'll go ahead and look at the error description and what is the reason and what is the message body now over here on to my queue if i go ahead and hit on refresh so i can see now that i have seven active messages and five messages now in the dead letter queue now over here the important thing is in the queue name to go ahead and append now that dead letter q so if you want to go ahead and have your program now read messages from the dead letter q we can go ahead and specify that let me also go ahead and mention the same thing at the end as well this is for our connection string and now let me go ahead and run the program right so over here we can see the dead letter reason and what is the description over here the message expired and was dead letter over here if i go back and hit on refresh i can see now i have four messages in the dead letter q so in this chapter we have seen how you can push messages onto the dead letter queue and how you can read messages from the queue itself now in this chapter i want to go through the feature of duplicate message detection which is available for your queues so if i actually go back onto azure really quickly if i go on to the overview for my queue if i go ahead and scroll on top so currently duplicate detection is disabled and this is something that you can't enable currently for an existing queue so let me go on to my namespace and over here let me go ahead and create a new queue and over here if i go ahead and scroll down i can go ahead and enable duplicate detection and we have something known as a duplicate detection window over here let me go ahead and specify it has two minutes and then let me go ahead and hit on create now what is the purpose of this duplicate detection so let's say that you have application components that are going in and sending messages onto the queue now let's say that a sender has gone ahead and sent a message on to the queue and let's say now that the sender has crashed so it has forgotten that it has sent the earlier message onto the queue and let's say it goes at and tries to send the same message again now for those applications that are critical that need to ensure that duplicate messages are not present in the queue they can go ahead and make use of this duplicate message detection so over here what happens is that you can actually go ahead and mention something known as a duplicate detection time window so over here when an application actually sends a message on to the queue during the next two minute window interval which we have specified the queue will go ahead and try to detect if the application is sending the same message again and if it tries to go ahead and do that it will go ahead and reject that message now in order for the queue to understand duplicate messages you need to go ahead and specify a message id so this is a property that is available by default for a message so i said there are many properties that are available for a message and one of them is the message id so when sending messages you need to go ahead and specify that message id so let's go ahead and see how to implement this so over here i have the program for sending messages now over here let me go ahead and change the name of the queue also let me go on to my new queue let me go on to shared access policy let me go ahead and add a policy to go ahead and send messages onto the queue and let me go ahead and hit on create now i'll go on to the policy over here i'll go ahead and take the primary connection string i'll go on to my program and replace it over here now i do want to go ahead and actually send so many messages let me go ahead and just send one message on to the queue so over here just create a very simple message now over here we need the service pass client we need the sender over here since now we are not going in and sending many messages so over here we have our service pass message so this is fine the content type is fine i will remove the time to live over here let me go ahead and also ensure to say the message is sent and over here now let me go ahead and also specify the message id now just for the sake of simplicity let me go ahead and specify the message id has one so it needs to go ahead and be a string right so now that we have this in place remember when it comes to our new queue we don't have any messages in place there are no active messages so let me go ahead and run this program so it's gone it and sent the message so you can go ahead and refresh to go ahead and confirm so we can see we have one message in place you can go ahead and hit on p quickly so you have the message with the message id of one now let's go ahead and run the same program again so this is like sending now a duplicate message all right so your client is saying anyway the message has been sent but if you go back on to your service bus queue and if you go ahead and hit on refresh you can go ahead and hit multiple times you will still see that you have only one message in place so because of that duplicate detection window it has gone ahead and detected now a duplicate message is being sent on to the queue so it has gone ahead and rejected that message now in this chapter just want to go through a few points when it comes to the messages that you can send on to the queue so we have seen that for messages that can be sent on to your service pass queue there are a lot of properties in place so for example the content type the body the message id there are also other properties as well so for example you have the properties of reply to you also have the property of the correlation ide so i want to go ahead and talk about the important properties when it comes to the exam so firstly for the messages that you have so the entire message body is sent has the payload of the request and then for each message you have the metadata so you have the system defined properties which you have just seen these are also known as broker properties you can also go ahead and add your own user defined properties as well when it comes to the broker system defined properties we have already gone ahead and looked at an example of using the message id this is when it came to duplicate message detection there is also another property known as the reply to so let's say that an application is sending a message on to the queue and another application component has gone ahead and let's say read the message where the message id is equal to one now if the sending application expects that the receiving application acknowledges that the message has been received and if this application now wants this application component to go ahead and send a receive notification via another message onto another queue in the reply to you can go ahead and give what is the name of the queue where the response needs to be sent to and when this application sends a message based on the message it has received over here it can go ahead and specify a new message id and in the correlation property it can go ahead and mention what was the message id of the message that it received this helps to go ahead and correlate messages together so what has been said and what has been received in reply on to what has been sent so in this chapter kind of want to go through these important points when it comes to property system properties for your messages now in the last chapter i had given an important note on the system defined properties for your messages now you could also go ahead and define your own application level properties as well so over here for each individual message over here i have the program to go ahead and send a message on to one of our queues that's app q so for your messages you always have application properties and you can just go ahead and add simple key value pairs so over here let's say that this particular message is basically part of the hr department you can go ahead and add that has a key value pair now if i just go back on to my app queue i'll go on to the service bus explorer let me go ahead and quickly receive all of the messages that are in the queue so that i don't have any messages in the queue so over here i don't have any active messages now if i go on to my program let me go ahead and send this message right so the message is sent now i'll go on to the program to go ahead and receive the message so it's in receive and delete mode now over here if you want to go ahead and now get the application properties over here i'll go ahead and add the code for getting the application properties so over here you can go through all of the keys which are present in your application properties you can go ahead and display the value of the key and over here going back onto your application properties for each key you can go in and get what is the value so let me go ahead and run this program and over here you can see you're getting the message body and along with that what is the key and the value for your application property hi and welcome back now in this chapter i just want to go through some quick points when it comes to the core differences between an azios storage queue and it is your service pass queue so we've seen azure storage queues which is available as part of azure storage accounts and then we have the more mature messaging service when it comes to the azure service pass queue now if you need to go ahead and store more than 80 gigs of messages in a queue then you should go ahead and use as your storage queues use as your service per squeeze when your queue size will not go beyond 80 gigs if you need guaranteed first in first out delivery of messages then you can also go ahead and enable sessions for the queue so over here if this is the first message that is sent into the queue you can be ensured that this will be the first message that will be received by the consumer this is actually not easy to achieve in azure storage queues it's not available out of the box over here but you do have a feature of enabling sessions for azure service bus that helps to enable this pattern of first in first out delivery of messages and the azure service pass has also other mature features such as duplicate detection so we've also seen that we've seen how the azure service pass can go out and you know detect duplicate messages so in this chapter just want to kind of give that quick overview when it comes to the core distinction between azure storage queues and as your service bus cues now we have seen how to work with azure service bus queues now let's go ahead and see how to work with azure service bus topics in the next set of chapters so over here when it comes to publishing messages it's mostly the same over here you have an application that is actually sending messages onto azure service bus but over here instead of setting it on onto a queue they are sending it on to a topic and then you can have multiple application components that can actually go in and subscribe onto the topic so if they want to go ahead and receive messages from the topic they can actually go ahead and subscribe to those messages they also have the facility to go ahead and create filters to decide which messages they want to go ahead and receive so over here the logic is a little bit different when it comes to topics over here you need to go ahead and subscribe to a topic in order to go ahead and receive messages and you also have the additional feature of having filters to go ahead and filter out what messages you want to go ahead and receive now let's see in the next set of chapters on how to work with service bus topics so here we are in zero now i'll go back on to my namespace i'll go on to topics now and let me go ahead and add now a new topic so over here i'll go ahead and give a name for the topic and over here you have some properties which are similar onto the queue where you can go ahead and specify what is the max size of the topic the message time to live let me go ahead and hit on create so now we have the topic in place now you can go ahead on to the topic now in the topic over here we have something known as subscriptions so now we can go ahead and add subscriptions that can go ahead and subscribe on to receiving messages from this topic so over here let me go ahead and now add a subscription over here let me go ahead and give a subscription name over here you can go ahead a max delivery count wherein you can go ahead and specify what is the max number of messages that can be delivered at one point in time over here you can go ahead and specify settings on to auto delete this particular subscription if it's idle for let's say 14 days and you have other aspects as well when it comes to this subscription let me go ahead and hit on create let me go ahead and add another subscription so i'll go ahead and mention this has subscription b over here again the same max delivery count and let me go ahead and hit on create so now i have two subscriptions that i subscribe on to this topic so if i go on to the subscriptions over here you can see both of the subscriptions and here you can see the message count now over here also i can now go on to the service bus explorer so now over here i'll choose the content type as application json and over here similarly to sending messages to our service pass queue i'll go ahead and send it on to our topic so i'll go ahead and hit on send over here let me go ahead and send all the messages on to the topic so i'll send the other message as well now over here if i go on to my subscriptions i can see a message count of three for each of my subscriptions so each subscription has gone ahead and received the message if i go on to the service bus explorer i can go ahead and receive the message on behalf of a subscription so i can go in and choose a subscription i can see i have three messages i can go ahead and hit on receive click on yes now as part of this subscription we have gone ahead and received a message if i go back now on to my subscriptions over here you can see for subscription a we have two messages and for subscription b we have three messages in place so when it comes to azure service pass topics you can go ahead and send messages on to the topics and you can go ahead and have subscribers that can go ahead and subscribe on to the azure service bus topic now in this chapter i want to go ahead and show you how you can use a.net program to go ahead and send messages on to the topic i'm going to be going ahead and reusing the same program we had seen early on for sending messages onto an azure service pass queue because the program is exactly the same you can go and use the same code to go ahead and send messages onto a topic the first thing i'll do is in my namespace let me go ahead and create a new topic so over here i'll just go ahead and give a name for the topic and hit on create i'll go on to the topic i'll go ahead on to subscriptions and let me go ahead and create a new subscription so over here let me go ahead and give a name for the subscription let me go ahead and give a max delivery count and let me go ahead and hit on create now next let me go on to the shared access policy let me go ahead and add a new shared access policy to go ahead and send messages on to the service bus topic and let me go ahead and hit on create let me go ahead and open the policy i want to go ahead and take the connection string i'll go on to the program and i'll go ahead and replace the connection string over here so this is remember the same program to go ahead and send a set of messages on to a queue over here let me go ahead and give the topic name and everything else remains the same so we're going out and creating a list of orders over here our service bus client our service bus sender class for each order in the orders i am going at and sending the message and then i'm going it and sending it on to the topic and then for each order in the orders list i'm going in and creating a message and then i'm going in and sending the message on to the subs bus topic let me go ahead and run the program right so all the messages have been sent so if i go back on to my topic and if i go on to my subscriptions over here you can see for subscription a i have five messages in place so it's pretty simple you can actually go ahead and reuse the same code to go ahead and send messages onto a service bus topic now similarly to the program we had reused when sending messages on to a queue over here i'm reusing one of the programs we had seen earlier to go ahead and receive messages from the queue this time we'll go ahead and reuse the same program to basically receive messages from our topic so firstly let me go ahead and replace the connection string so over here let me go on to shared access policy so we have one for send i'll go ahead and click on add so this is for listen let me go ahead and hit on create i'll go on to the policy i'll go ahead and take the connection string i'll go on to my program i'll replace it over here i'll go ahead and also give the name of the topic and remember for topics we have to go ahead and receive the messages as part of a subscription so over here let me go ahead and give the subscription name remember we have gone ahead and create a subscription with the name of subscription a so let me go ahead and specify over here now in the create receiver method over here let me go ahead and delete all of this so now over here instead of actually specifying the queue name i can actually go ahead and specify what is the topic name over here let me go ahead and just replace this has topic name over here let me go ahead and put has the topic name and over here what is the subscription name also when you're going ahead and creating the receiver you also have the options to go ahead and again specify the receive mode so over here you can go ahead and again specify the receive mode to go ahead and receive and delete the messages from the topic let me go ahead and just delete this right so everything is in place now let me go ahead and run the program right so over here you're getting the relative messages from the topic now if i go on to my subscriptions currently i had five messages let me go ahead and hit on refresh and now you can see the message count has reduced on to three so again over here you can reuse a lot of aspects the same program which you use for receiving messages from the queue to go ahead and receive messages from the topic hi and welcome back now in this chapter i want to go through the filters aspect which is available for your azure service bus topics so in topics you have this feature that's known as filters and this can actually go ahead and help subscriptions decide on which messages they want to receive now there are different types of filters that are available you have sql filters here you can go ahead and use sql like conditions for the evaluation this can be done against both the system or the user defined properties for the system defined properties the properties must be prefixed by cis sys dot and the property name you also have boolean filters this can be either a true filter or a false filter please know that the boolean filter is actually based on a sql filter itself then you have correlation filters your conditions can be used to match against the users or system defined properties now if you want to go ahead and decide between correlation filters and sql filters so the correlation filters actually process information much faster than the sql filters so let's go ahead on to zero and let's look at boolean filters first so over here for our service pass topic so we have one subscription in place wherein the message count is three now if i go on to the subscription itself if you go ahead and scroll down so over here you can see that there is a default filter in place this is of the filter type sql filter now if you go ahead and choose the filter so over here it's basically a very simple sql filter where one is equal to one now this is also the representation of the true filter that's the boolean filter so this means that except all messages that are sent onto the service bus topic now let's go ahead and delete this default filter and let me go ahead and add another filter so over here i'll just give a name of false filter and over here i want to go ahead and again give a boolean filter representation but this time i am trying to say that or give a false condition to ensure that this subscription does not receive any messages let me go ahead and click on save so once this is in place again if i go on to my topic so currently remember i have three messages in place now over here i have the program to go ahead and send messages onto that service bus topic so let me go ahead and run the program so it's saying all the messages have been sent so going back on to seo for my new topic let me go ahead and hit on refresh and i can see for my subscription i still have only those three messages in place and this is because of the filter we have actually put in we have gone ahead and given a false filter wherein we have a false condition and this will ensure that this subscription does not receive any messages so in this chapter just want to first give an example of the boolean filter this is actually based on the sql filter itself now in the last chapter we had gone ahead and seen how to work with a sql filter and over there we gave a very simple filter so that it was a representation of a boolean filter now let me go ahead and show you how to create a proper sql filter so let me go ahead and hit on delete first for the existing filter that we have and then let me go ahead and add a filter over here you can see you have only two filter types either it's a sql filter or it's a correlation filter so over here let me go ahead and give a name for the filter and over here we can go ahead and give a condition so over here the condition is basically based on the message id so i want to say if the message id is 3 only then go ahead and accept the message now since this is a system defined message i need to go ahead and prefix this with sys dot and then the message id let me go ahead and scroll down and let me go ahead and save the changes so if i go on to my topic right so i have three messages currently in place now i'll go on to the program which is used to go ahead and send messages on to our azure service bus topic over here let me go ahead and just add some code so over here first i'll go ahead and add an integer variable and then for each message let me go ahead and set the message id is equal to i dot to string because we require a string for the message id and let me go ahead and increment the value of i so at one point in time the value of i will be three and that will be the message id so idly only one message should now be taken up by the subscription let me go ahead and run the program right so this is done now if i go ahead and hit on refresh so i can see one message has now been added if i go on to the subscription if i go on to the service bus explorer if i go ahead and peek at the messages if i go with the last message over here you can see the message id so in this chapter i want to show you how you can make use of sql filters now in the last chapter i had gone ahead and given an example of a sql filter now over here i'll just go ahead and show you how to configure a correlation filter so if i go back on to my subscription if i go ahead and scroll down let me go ahead and delete the existing filter let me go ahead and add another filter and over here let's say you want to go ahead and add a filter based on the content type so you can go ahead and choose the correlation filter and over here in the system properties you can go ahead and choose the content type and over here you can go ahead and put the value has application json and then you can go ahead and hit on save changes so this filter means that this subscription will only go ahead and pick up messages if the content type has been set onto application json and that's something that we have seen that we could already do so in one of our earlier programs i had gone in and showed you how to set the content type over here i just want to go ahead and show you how you can define a correlation filter based on the properties it could be either the system defined properties or the user defined properties hi and welcome back now as part of the exam it is important to go ahead and understand the important azios cli commands when it comes to the azure service bus so over here i got the commands that we are going to execute so the first thing we are going to do is to go ahead and set up some variables so over here what is the name of the namespace we want to go ahead and create what is our resource group name our location what is the name of the queue and what is the name of the topic and what is the name of the subscription we can then go ahead and use the command of az service pass namespace create to go in and create our namespace we go ahead and give the name of our namespace the resource group the location and the sku so let me go ahead and execute all of these commands over here in azure cloud shell i'll go ahead and just clear the screen first let me go ahead and right click and enter all of these variables so first is our namespace name then the resource group then the location then the name of the queue that we want to go ahead and create in the namespace then our topic name then a subscription name so in another tab if i go on to all resources so over here if i go ahead and look at the namespaces that i have so currently i only have one service bus namespace in place now let me go ahead and use the command to go ahead and create a namespace so i'll go ahead and hit on paste over here let's wait till this command is complete now once i've got a json output back so over here my namespace is now in place i'll go on to the next command that is ac service pass queue create this will actually go ahead and now create a queue over here we are giving our cue name and what is the mac size so let me go ahead and copy this command i'll just clear the screen let me go ahead and paste this particular command to go ahead and create the queue in our service bus namespace this is also done i'll go ahead and clear the screen now next i'll go ahead and use the easy service bus topic create command to go ahead and create a topic so i'll go ahead and copy this i'll paste it over here once i have the topic in place i'll go ahead and clear the screen and i'll use the file command to go ahead and create a subscription so let me go ahead and enter this particular command this is also done now in all resources if i go ahead and hit on refresh you can see your new namespace in place if you go on to it if you go ahead and scroll down if you go on to cues you can see you have your app queue in place if you go on the topics you can see your app topic in place if you go on to the app topic if you go on to subscriptions you will see subscription a in place so in this chapter just want to go through the important azure cli commands when it comes to working with your queues and topics in a service name space now in this chapter i quickly just want to go through an example of azir functions that goes at and listens for messages in the azure service bus queue so over here in visual studio let me go ahead and create a new project over here i'll go ahead and choose azure functions in my recent project templates i'll go on to next so over here let me go ahead and just give a name let me go ahead and hit on create over here now let me go ahead and actually search for the service pass queue trigger over here i'll go ahead and give a name for my queue so i have queue known as app queue and over here i need to go ahead and give a name for the connection string that will be used by my azure functions to go ahead and connect onto my service pass namespace i'll go ahead and hit on create so over here i have now the code in place let me go ahead and quickly rename the class i'll just rename the function as well now over here we have the service pass trigger in place we have the name of our queue we have the connection string and over here all of our items are returned as strings so over here the first thing i will do is to go ahead and ensure that i define the service bus connection so i'll go on to my local settings.json file over here let me go ahead and give the service pass connection now over here in azure i have a service pass namespace in place i also have a queue in place let me go on to the queue i'll go on to shared access policy over here let me go ahead and add a policy i'll go ahead and choose listen has the permission and over here let me go ahead and give a name and hit on create over here i'll go ahead and take the policy i'll go ahead and take the primary connection string i'll go ahead and place it over here now let me go ahead and run this program as it is now over here it's actually giving us an error so what we need to do is that let me go ahead and close this stop my program so over here i have to go back onto my local settings.json file and over here in the connection string i need to go ahead and remove the entity path so this is required let me go ahead and now run this program again right so now we have it running so over here the removal of the entity part is something that is actually required by azir functions when you're developing it in visual studio now if i go on to the service pass explorer let me go ahead and just send a simple text based message and let me go ahead and hit on send right so it's send over here you can see that it has gone ahead and read the message from the queue over here you can go ahead and see test message so this is the string that it has gone ahead and taken from the body of the message over here if you go on to the receive if you go ahead and hit on refresh so you can see there are zero active messages so i didn't have any messages in this queue earlier on so the azure function has gone ahead and performed the operation of both a receive and delete operation right so in this chapter i want to show you how you can get started working with azure functions for your service bus cues now in the last chapter we had gone ahead and seen how to receive messages from the queue now over here when it came to our trigger we were receiving the message has a string now you could also go ahead and receive it as of the class type message over here you can actually go ahead and include the package of microsoft.azure.servicebus so over here based on this azir function this package is already in place you can go ahead and make use of it once you have now the message class in place you can actually go ahead and now use different properties so over here for your queue item now you can actually go ahead and take the body to string so over here you're looking at the body of the message and let's say you want other aspects as well so for example if you want to go ahead and get the content type you can go ahead and do that as well and then over here i also want to go ahead and ensure that i convert the bytes so when we get the message from the azure service plus queue it will be in bytes so let me go ahead and ensure that i get the string so over here let me go ahead and ensure to include the namespace for system.txt so over here getstring and then what i'll do is that let me go ahead and remove this and just get the body over here now let me go ahead and run the program once you have the program running over here in the service bus explorer let me go ahead and send a message and over here you can see the body of the message test message and what is the content type so over here just want to go ahead and additionally show you how you can use the message class now in this chapter i just want to quickly show you how you can create an azure function that listens for messages from an azure service bus topic so this is similar to listening to messages from it as your service pass queue so over here in visual studio let me go ahead and choose azure functions and go on to next over here let me go ahead and give a name let me go ahead and hit on create so over here i'll go ahead and choose service pass topic trigger now over here i need to go ahead and give a name for the topic so i already have a topic in place in my service bus name space known as app topic over here we have to go ahead and give the name of the connection string that connects on to our service bus and then over here we have to go ahead and give the name of the subscription so remember when it comes to topics you have to go ahead and subscribe onto a topic to go ahead and receive the messages so over here let me go ahead and hit on create over here again let me go ahead and rename the class i'll also go ahead and rename the function and over here everything remains the same so we have our subscription our topic and the connection over here again you're going in and receiving a string but similar to the azure function that we have seen with azure service pass queues you can go ahead and also use the message class as well so let me go ahead and define the service bus connection string i'll go on to localsettings.json over here let me go ahead and define this now over here i have a namespace in place if i go on topics so over here i do have an app topic in place let me go ahead first of all on to subscriptions so this is a new topic that i created and let me go ahead and add a new subscription so i'll give the same subscription name as i've defined in my code over here i'll define the max delivery count and let me go ahead and hit on create once this is done i'll go onto shared access policy over here i'll click on add i'll choose the permission of listen i'll go ahead and give a policy name and hit on create i'll go on to the policy i'll go ahead and take the connection string i'll go ahead and define it here in visual studio let me go ahead and remove the entity path and let me go ahead and run this program as it is so it's running over here so let me go on to the service bus explorer over here let me go ahead and send a simple text based message and over here let me go ahead and hit on send if i go back on to my program i can see it has gone ahead and processed the message so i said over here you can go ahead and again use that message class as we've seen earlier over here it's receiving it has a stream but you could also go ahead and receive it as an object based on the message class
Info
Channel: Alan Rodrigues
Views: 1,317
Rating: undefined out of 5
Keywords: AZ-204, Azure Service Bus
Id: LbzZLkmO_WE
Channel Id: undefined
Length: 82min 2sec (4922 seconds)
Published: Tue Oct 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.