Spring @Async annotation allows us to create asynchronous methods in spring. Let’s explore @Async in this tutorial on spring framework.

For a brief, when we annotate a method of a bean @Async annotation, Spring will execute it in a separate thread and the caller of the method will not wait till the method is completed execution. We will be defining our own Service and using Spring Boot 2 in this example. Let’s get started!

Spring @Async Example

We will be using Maven to create a sample project for the demonstration. To create the project, execute the following command in a directory that you will use as workspace:

If you are running maven for the first time, it will take a few seconds to accomplish the generate command because maven has to download all the required plugins and artifacts in order to make the generation task. Here is how project creation looks like:

project-creationCreating a project with Maven

Once you have created the project, feel free to open it in your favourite IDE. Next step is to add appropriate Maven Dependencies to the project. Here is the pom.xml file with the appropriate dependencies:

Finally, to understand all the JARs which are added to the project when we added this dependency, we can run a simple Maven command which allows us to see a complete Dependency Tree for a project when we add some dependencies to it. Here is a command which we can use:

When we run this command, it will show us the following Dependency Tree:
spring async maven dependencies

Enabling Async Support

Enabling Async support is as well, just a matter of a single annotation. Apart from enabling the Async execution, we will also make use of Executor which allow us to define Thread limits as well. More on this once we write the code:

Here we made use of @EnableAsync annotation which enables Spring’s ability to run Asynchronous methods in a background thread pool.

Next, we also add the mentioned Executor:

Here, we set that maximum of 2 threads should run concurrently and the queue size is set to 500. Here is the complete code of the class with import statements:

We will make a service next which actually makes of Thread executions.

Making a Model

We will be using a public Movie API which just returns a Movie’s Data. We will be defining our model for the same:

We have used @JsonIgnoreProperties so that if there are more attributes in the response, they can be safely ignored by Spring.

Making the Service

It’s time we define our Service which will be calling the mentioned Movie API. We will use a simple RestTemplate to hit a GET API and obtain results asynchronously. Let’s look at the sample code we use:

This class is a @Service which makes it eligible for Spring Component Scan. The lookForMovie method’s return type is CompletableFuture which is a requirement for any asynchronous service. As timing for the API can vary, we have added a delay of 2 second for demonstration.

Making a Command Line Runner

We will be running our app using a CommandLineRunner which is the easiest way to test our application. A CommandLineRunner runs right after all the beans of the application has been initialised. Let’s see the code for CommandLineRunner:

We just used the RestTemaplate to hit the sample API we used with some randomly picked Movie IDs. We will be running our application to see what output it shows.

Running the application

When we run the application, we will see the following output:

If you observe closely, only two threads were made to be executed in the app, namely JDAsync-1 and JDAsync-2.

Conclusion

In this lesson, we studied how we can use Spring’s Asynchronous capabilities with Spring Boot 2.

Read more Spring related posts here.

Download the Source Code

By admin

Leave a Reply

%d bloggers like this: