How to pass Custom Thread pool to spring Async flow
In the previous post-https://onecompiler.com/posts/3ur2zgj3g, we learned how to use spring async flow.
Introduction
Now we are going to learn about how to pass a custom thread pool for the async flow of execution.
Enable Async behaviour using spring annotation
- Add '@EnableAsync' annotation to spring boot application.
- Add '@Async' to any spring bean method. (Other than spring bean it won't work)
- Create a custom thread pool and register that bean to spring container by using '@Bean'
- Call that method, it won't block the current thread.
SpringBoot Main app - Add '@EnableAsync' on top of the class and '@Bean' on top of thread pool method
package com.oc.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@SpringBootApplication
@EnableAsync
public class RabitMqApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(RabitMqApplication.class, args);
TestService testService = context.getBean(TestService.class);
System.out.println("Started Main Thread");
testService.test1();
testService.test2();
testService.test1();
testService.test2();
System.out.println("Ended Main Thread");
}
@Bean
public TaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize(20);
executor.setThreadNamePrefix("my-pool");
executor.initialize();
return executor;
}
}
Test Service - Add '@Asyn' on top of the method
package com.oc.app;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class TestService {
@Async
public void test1() {
System.out.println("Entered test1() By Thread - " + Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Ended test1() By Thread - " + Thread.currentThread().getName());
}
@Async
public void test2() {
System.out.println("Entered test2() By Thread - " + Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Ended test2() By Thread - " + Thread.currentThread().getName());
}
}
Output :
Started Main Thread
Ended Main Thread
Entered test1() By Thread - pool1
Entered test2() By Thread - pool2
Entered test2() By Thread - pool4
Entered test1() By Thread - pool3
Ended test1() By Thread - pool1
Ended test1() By Thread - pool3
Ended test2() By Thread - pool4
Ended test2() By Thread - pool2
- Note: Above response will take just 5+ secs because of asynchronous execution.