WEBVTT

00:00.110 --> 00:00.680
Hi everyone.

00:00.680 --> 00:01.220
Welcome back.

00:01.220 --> 00:07.340
In this lecture we are going to learn about how to build an asynchronous client using the HTTP client

00:07.340 --> 00:09.170
that we have been using so far.

00:09.170 --> 00:13.730
So in this case we are going to use a similar function or a similar client.

00:13.730 --> 00:17.150
And then we are going to be creating a brand new function.

00:17.150 --> 00:21.920
The code is going to look pretty much the same, but the return type and the function that we are calling

00:21.920 --> 00:23.630
here is going to be a little different.

00:23.630 --> 00:26.660
So in this case I'm going to name this one as async okay.

00:26.690 --> 00:30.920
So in the place of send we are going to be calling send async.

00:30.920 --> 00:34.520
So the request arguments to this function is still the same.

00:34.520 --> 00:37.850
But the response is going to be a completable future.

00:37.850 --> 00:44.030
So if you go to the send async function it's going to return you a completable future of HTTP response.

00:44.030 --> 00:48.620
If you have not worked with Completablefuture, whatever I'm going to explain might not really make

00:48.620 --> 00:52.190
sense, but if you have already worked on it, it will be pretty easy to follow.

00:52.190 --> 00:54.710
I have a specific course on this specific topic.

00:54.710 --> 01:00.410
Please check my profile if you want to learn more in depth about how to use Completablefuture and what

01:00.410 --> 01:01.880
are the different options that you have?

01:01.910 --> 01:04.790
Okay, but in here, let's go ahead and get this started.

01:04.790 --> 01:07.720
So what we are going to do, we are going to use a response.

01:07.720 --> 01:10.450
So the response is an async response.

01:10.450 --> 01:10.930
Right.

01:10.930 --> 01:15.070
And then in order to access the actual response there are some functions.

01:15.070 --> 01:16.810
This is a functional API.

01:16.840 --> 01:20.050
So if you press dot it gives you all these options actually.

01:20.050 --> 01:22.780
So in our case we are going to be getting the response.

01:22.780 --> 01:26.500
And then we are going to apply the object mapper logic that we have over here.

01:26.500 --> 01:29.140
So in this case we can use a then apply.

01:29.140 --> 01:32.380
So then apply gives you access to the HTTP response.

01:32.380 --> 01:32.830
Right.

01:32.830 --> 01:34.360
So you can give any name you want.

01:34.390 --> 01:40.060
So in this case I'm going to give the name as HTTP response okay.

01:40.090 --> 01:44.050
And then what we are going to do we are going to be printing the HTTP status code.

01:44.050 --> 01:44.500
Right.

01:44.530 --> 01:47.080
So let's go ahead and copy that one and then put it over here.

01:47.080 --> 01:51.520
So this is going to be HTTP response okay.

01:51.550 --> 01:53.500
This is going to be HTTP response over here.

01:53.500 --> 01:55.540
So that we print the status code over here.

01:55.540 --> 01:56.920
And after that what do we do.

01:56.920 --> 02:02.110
So we need to still perform the logic of converting the string to a movie.

02:02.140 --> 02:02.560
Right.

02:02.560 --> 02:04.870
So we just need to put this logic over here.

02:04.870 --> 02:09.520
Because what we have access here is that here is a HTTP response.

02:09.520 --> 02:12.640
So let's go ahead and add the semicolon over here.

02:12.640 --> 02:13.630
We can remove this code.

02:13.630 --> 02:16.360
And it looks like we need to add the exception logic.

02:16.360 --> 02:18.640
Because there is a JSON processing exception.

02:18.640 --> 02:21.280
That can still happen if this parsing fails.

02:21.280 --> 02:25.030
So now let's go ahead and add the try and catch block over here.

02:25.060 --> 02:25.600
There you go.

02:25.600 --> 02:31.090
So this is to handle the exception that can be thrown from this actual deserialization okay.

02:31.120 --> 02:32.500
So now what we will do.

02:32.530 --> 02:34.180
We need to return this from here.

02:34.210 --> 02:34.420
Right.

02:34.450 --> 02:39.940
So in this case we can add the return over here because the response is going to be a computable future

02:39.940 --> 02:42.130
not the actual movie okay.

02:42.160 --> 02:46.060
So in this case we're going to add compatible future movie over here.

02:46.060 --> 02:49.060
So let me quickly recap what we have done so far here.

02:49.060 --> 02:53.110
So what we have done here is that we have changed the send call to send async.

02:53.110 --> 02:57.850
When we make this call, what this is going to return is a this is going to return you a computable

02:57.850 --> 02:58.360
future.

02:58.360 --> 03:04.210
So once we have the handle of computable future, what this is going to do this is going to hold the

03:04.210 --> 03:04.630
future.

03:04.630 --> 03:10.270
Which means any time when this code completes this particular logic, whatever we have in the then apply

03:10.270 --> 03:11.290
will get invoked.

03:11.290 --> 03:16.950
And what this send async also means is that when the caller makes a call to the specific function.

03:16.980 --> 03:21.510
The thread that's involved in executing this code is not going to be blocked anymore.

03:21.540 --> 03:26.400
As soon as this call is executed, the thread will be released and then a future will be returned.

03:26.400 --> 03:28.170
So we have a handle of this future.

03:28.170 --> 03:33.570
When that future completes, then we will use the logic that we have here to be executed.

03:33.570 --> 03:37.410
Once this logic is executed, the Completablefuture will be executed.

03:37.440 --> 03:37.800
Okay.

03:37.830 --> 03:41.850
And in here, what we are going to do, we are going to be calling the test function over here.

03:41.850 --> 03:45.090
So we are going to call the get movie id async.

03:45.120 --> 03:45.540
There we go.

03:45.570 --> 03:49.080
Let's copy this one and then put it over here and then put it over here.

03:49.080 --> 03:52.200
So in this case what we have is a compatible feature right.

03:52.200 --> 03:54.450
So all these assertions will fail here.

03:54.450 --> 03:57.540
So in this case what we can do we can actually call Dot.

03:57.540 --> 04:00.630
And you have many functions so that we can actually get the data.

04:00.630 --> 04:02.520
What's present in the compatible feature.

04:02.520 --> 04:06.600
Since we are using the test cases we can very well invoke the join function.

04:06.600 --> 04:11.490
So what this join function will do is that it will wait for that compatible feature to complete.

04:11.490 --> 04:16.980
And then we will basically have a handle of that object that is present inside the compatible feature.

04:16.980 --> 04:22.300
And then we are actually performing the assertions as we have done for the synchronous call.

04:22.300 --> 04:25.840
So now what I'm going to do I'm going to be executing this test.

04:27.310 --> 04:29.770
And this test is going to pass as expected.

04:29.770 --> 04:34.120
So one thing you need to make sure is that you need to have the J-web server up and running.

04:34.150 --> 04:38.050
As long as that's up and running you're going to see a green check mark over here.

04:38.080 --> 04:38.680
There you go.

04:38.710 --> 04:41.410
This test case succeeded as expected.

04:41.410 --> 04:46.870
So in this lecture what we have learned is that we have learned how to build an asynchronous HTTP client,

04:46.870 --> 04:49.750
which is by calling the send async function.

04:49.750 --> 04:53.140
So once you call the send async function, what you get is a comparable feature.

04:53.140 --> 04:58.720
And then you implement the handle of that specific response using the then apply function.

04:58.720 --> 05:01.090
So there are many ways of handling exceptions.

05:01.090 --> 05:04.570
Also I'm not going to cover those things as part of this course.

05:04.570 --> 05:09.460
If you want to learn about Completablefuture in detail, I have a specific course named multithreading

05:09.460 --> 05:12.430
Parallel and Asynchronous Coding in modern Java.

05:12.430 --> 05:14.680
So this is a pretty popular course in Udemy.

05:14.710 --> 05:19.000
You're welcome to enroll and then learn more about Completablefuture in this specific course.

05:19.000 --> 05:24.160
If you go to the bottom, there is a or there are many section that talks about comparable feature in

05:24.160 --> 05:24.760
detail.

05:24.760 --> 05:26.170
This marks the end of this lecture.

05:26.170 --> 05:27.340
Thank you for watching.
