How to start Spring Async with Spring Boot

For scaling a heavy system, Java provides a mechanics for executing expensive jobs in background with Java Thread Pool & Future to manage the final results. Spring Framework makes it more easy with annotation @Async. In the How to start Spring Async with Spring Boot, JavaSampleApproach will guide you how to build asynchronized services with @Async & Spring Boot.

Related Posts:
1. How to create a Java Thread
2. Java Thread Pool – ExecutorService
3. Spring ThreadPoolTaskExecutor with Callable interface for concurrency programming
4. Java Future

I. Techonologies for How to start Spring Async with Spring Boot

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot

II. Overview
1. Project Structure

Spring Async Project Structure

2. Step to do

– Create Spring Boot project
– Create an Asynchronized Service
– Customize Async Executor
– Create Client to use @Async Service
– Run & Enjoy Results

III. Practices
1. Create Spring Boot project

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, add project info. Press Next, then Finish, a Spring Boot project will be created successfully.

2. Create an Asynchronized Service

Create a Service with an asynchronize method process() by using @Async. So the process() method wil be executed in a separated Thread when be invoked.

3. Customize Async Executor

By default, Spring provides a SimpleAsyncTaskExecutor executor. Spring also supports an AsyncConfigurerSupport class to customize AsyncExecutor.

@EnableAsync is used to detect ansynchronized method with @Async by Spring.

4. Create Client to use @Async Service

In the main Spring Boot class, we implement a run() function of CommandLineRunner:

For waiting Until All Processes are Done, we use below segment code:

5. Run & Enjoy Results

Run Project with Spring Boot mode & Enjoy the Logs:

IV. Sourcecode

Spring Async

By grokonez | January 18, 2017.

Last updated on June 4, 2017.

Related Posts

1 thought on “How to start Spring Async with Spring Boot”

Got Something To Say:

Your email address will not be published. Required fields are marked *