Hi everyone.
Welcome back.
In this lecture we are going to learn about how to build an asynchronous client using the HTTP client
that we have been using so far.
So in this case we are going to use a similar function or a similar client.
And then we are going to be creating a brand new function.
The code is going to look pretty much the same, but the return type and the function that we are calling
here is going to be a little different.
So in this case I'm going to name this one as async okay.
So in the place of send we are going to be calling send async.
So the request arguments to this function is still the same.
But the response is going to be a completable future.
So if you go to the send async function it's going to return you a completable future of HTTP response.
If you have not worked with Completablefuture, whatever I'm going to explain might not really make
sense, but if you have already worked on it, it will be pretty easy to follow.
I have a specific course on this specific topic.
Please check my profile if you want to learn more in depth about how to use Completablefuture and what
are the different options that you have?
Okay, but in here, let's go ahead and get this started.
So what we are going to do, we are going to use a response.
So the response is an async response.
Right.
And then in order to access the actual response there are some functions.
This is a functional API.
So if you press dot it gives you all these options actually.
So in our case we are going to be getting the response.
And then we are going to apply the object mapper logic that we have over here.
So in this case we can use a then apply.
So then apply gives you access to the HTTP response.
Right.
So you can give any name you want.
So in this case I'm going to give the name as HTTP response okay.
And then what we are going to do we are going to be printing the HTTP status code.
Right.
So let's go ahead and copy that one and then put it over here.
So this is going to be HTTP response okay.
This is going to be HTTP response over here.
So that we print the status code over here.
And after that what do we do.
So we need to still perform the logic of converting the string to a movie.
Right.
So we just need to put this logic over here.
Because what we have access here is that here is a HTTP response.
So let's go ahead and add the semicolon over here.
We can remove this code.
And it looks like we need to add the exception logic.
Because there is a JSON processing exception.
That can still happen if this parsing fails.
So now let's go ahead and add the try and catch block over here.
There you go.
So this is to handle the exception that can be thrown from this actual deserialization okay.
So now what we will do.
We need to return this from here.
Right.
So in this case we can add the return over here because the response is going to be a computable future
not the actual movie okay.
So in this case we're going to add compatible future movie over here.
So let me quickly recap what we have done so far here.
So what we have done here is that we have changed the send call to send async.
When we make this call, what this is going to return is a this is going to return you a computable
future.
So once we have the handle of computable future, what this is going to do this is going to hold the
future.
Which means any time when this code completes this particular logic, whatever we have in the then apply
will get invoked.
And what this send async also means is that when the caller makes a call to the specific function.
The thread that's involved in executing this code is not going to be blocked anymore.
As soon as this call is executed, the thread will be released and then a future will be returned.
So we have a handle of this future.
When that future completes, then we will use the logic that we have here to be executed.
Once this logic is executed, the Completablefuture will be executed.
Okay.
And in here, what we are going to do, we are going to be calling the test function over here.
So we are going to call the get movie id async.
There we go.
Let's copy this one and then put it over here and then put it over here.
So in this case what we have is a compatible feature right.
So all these assertions will fail here.
So in this case what we can do we can actually call Dot.
And you have many functions so that we can actually get the data.
What's present in the compatible feature.
Since we are using the test cases we can very well invoke the join function.
So what this join function will do is that it will wait for that compatible feature to complete.
And then we will basically have a handle of that object that is present inside the compatible feature.
And then we are actually performing the assertions as we have done for the synchronous call.
So now what I'm going to do I'm going to be executing this test.
And this test is going to pass as expected.
So one thing you need to make sure is that you need to have the J-web server up and running.
As long as that's up and running you're going to see a green check mark over here.
There you go.
This test case succeeded as expected.
So in this lecture what we have learned is that we have learned how to build an asynchronous HTTP client,
which is by calling the send async function.
So once you call the send async function, what you get is a comparable feature.
And then you implement the handle of that specific response using the then apply function.
So there are many ways of handling exceptions.
Also I'm not going to cover those things as part of this course.
If you want to learn about Completablefuture in detail, I have a specific course named multithreading
Parallel and Asynchronous Coding in modern Java.
So this is a pretty popular course in Udemy.
You're welcome to enroll and then learn more about Completablefuture in this specific course.
If you go to the bottom, there is a or there are many section that talks about comparable feature in
detail.
This marks the end of this lecture.
Thank you for watching.