SQS Using JAVA - Send, Receive And Delete data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi today we will see how java communicates with sqs like read write and even delete operation so for that first and foremost we need to create the aws credentials so we'll go to my security credentials and form aws access key and secret key we will generate a new key and take the access key id and in terminal i have already installed aws cli so you just give aws configure access key secret key default is mumbai so all this has been configured so once you generate access screen secret key especially i recommend you to delete it once your poc is done then we need to add dependency like maven dependency so for that we will give group id com dot amazon aws artifact id and the version so for latest version you can just google it and have an aws and you can see the latest version so you can just copy it from here and paste it in your ide in pom.xml so here you will find palm.xml and you will just update the dependency so if your autoimport is enabled it will automatically download the jar files if not then you just need to go and refresh it here or from here you just need to go and give maven and reimport so in order to send message what we need to do is like we will import aws amazon sqs then client builder then attribute value send message request and send message result to print the message id so using sqs builder i have created a sqs variable amazon sqs and then we have defined the queue url so you can get the q url from amazon just need to go to is my queue here it is so here i have created a standard queue if you click it you can get the url from here so i have used the same url and then i have created a message attribute so i'll show you what this attribute stands for so after that i will send a message so with q url this url then with message body hello world and with message attribute where i will put the attribute as name and type string and value paul with delay segment of 5 means it will appear after 5 second and then i'll just send the message and print the result get message id so if i go and run it oh that's beautiful our message id is printed so if you go and see what messages are there so you can see that hello world so if i click more details in message attribute i can see that name type string and value is paul which we have defined here message attribute so but these are not necessary and it's optional so you can omit this so we need to omit this and even we can omit delay second so if we omit delays again it will appear at an instance means when this message has been pushed it will be available in the queue okay there's some error so i hope the message has been pushed not yet okay we can see that it is available immediately and if we go to the message attribute there is no value here because we had not passed the value so now if i want to retrieve it what i need to do is like we'll just import aws sqs then the builder so in builder we can specify like which region which credentials to use which profile to use since i have already set it by default so it will take the default region that is mumbai and if i run it so what we have done is like mainly we need to specify the q url then wait time how much time it should wait and then maximum number so it should be by default it is 1 and maximum it can be 10. so once you run it i can see that only one message is printed because some message will be in flight mode so if you see here this messages message in flight it is not visible so what i will do is like i will push multiple messages so as of now i had four messages and if i try to run it read all the messages so most probably it will fetch one message yeah it is fetching only the first message so we have checked that say sqs dot receive message returns sometimes only one message this is because we had very less number of messages so in that case what we need to do is like i've written a while loop and i will send 100 messages at an instant and here we are using with visibility time of 10 so it can be like 100 120 like 2 minute so why it is used visibility timeout because we want to tell the queue that the message should not be visible to any other consumer until we are done like my program will take 10 seconds or maybe 20 seconds so i will give like visibility time out of 30 so in that in between 30 seconds it will not be available to any other consumers and maximum number of messages we can receive is 10 if i use 100 then it will throw error so it can be minimum 1 and maximum 10 and if we use dot with wait time so in that case what it will do is like maximum we can send is like 20 seconds so what it will do it will wait for 20 seconds if any message is available but if message is available within 5 second then it will return before 20 seconds itself so we will omit this and and run our read.main or sqs receive message so we can see that we had received 10 messages and all are of different id and if we go on the right side we can see that we will have the message attributes also since we are not passing here any attributes so we didn't receive any attributes and if we want to delete any message what we need to do is like we need to set the region then sqs then builder and we will do sqs dot delete message and pass the q url and message dot get receipt handle so first we will read the message and then from that message you will get the receipt handle so each message will have different receipt handle so you can see that we had message id then we had that received handle so each time we fetch that receipt handle may differ because what amazon does is like it will keep for copy of each message so sometimes this receipt handle may change so in that case what we need to do is like once we read the message then we will delete it so we need to iterate till until the queue is empty so if i run it the message is deleted so mostly it will be only one message and if we want to delete multiple messages what we need to do is like here we will just use sqs dot delete message and then we will pass the q url and message dot will pass the receive handle so these and these are same just uncomment it and comment this part and this too so we had the q url and with maximum number of messages 10 then we are iterating it so so at a time we can read 10 messages so we can delete also 10 message so we need to write a recursive method to fetch 10 messages at a time and delete it once it is read so if i run this delete operation again it should delete 10 messages so we can see that 10 messages are deleted so we had now 89 messages so this code has deleted one message and this code is deleted 10 message so it's total 11 messages if we rerun it again it has deleted 10 more records so you can verify that now it has deleted nine records let's check one two three four five six 7 8 9 10 no it has deleted 10 records let us redone it again one two three four five six seven eight nine ten so again we had later 10 records it should be 70. okay my badge okay the error is happening because we are not setting the visibility timeout so we need to set visibility timeout of like well set is 20 seconds like i will take 20 seconds to process my request so what we'll do is like we'll delete this in 20 second so my 10 request has been deleted most probably it should show now 69 yes again if we want to re-verify it so now we are deleted only five records so it should be 64. yes so in order to process or in order to delete we need to set the visibility timeout to some extent by default it is 30 second one two three four five six seven eight nine ten so i have deleted 10 records yeah so visibility timeout will make this message available in flight so like if i don't delete it so you have set the visibility timeout to 20 okay so let's check so you can see that 10 messages has been set to in flight so it will not be read by any other consumers so that's all for this tutorial if you like this video then don't forget to hit the like button and share and subscribe bye see you
Info
Channel: CodeSpace
Views: 15,452
Rating: undefined out of 5
Keywords: send message to sqs using java, java sqs message, sqs delete data, sqs receive data, aws sqs java, sqs send data, yt:cc=on, codeSpace, aws sqs, aws, amazon web services, simple queue service
Id: _C_SykraCVk
Channel Id: undefined
Length: 13min 27sec (807 seconds)
Published: Wed May 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.