You can just set the taskExecutor as bean, and @EnableAsync for this.
package org.blog.test.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration @EnableAsync public class AsyncConfig { @Bean public ThreadPoolTaskExecutor asyncTaskExecutor() { return new ThreadPoolTaskExecutor() {{ setCorePoolSize(5); setMaxPoolSize(20); setQueueCapacity(200); setThreadNamePrefix("AsyncExecutor_"); }}; } }
And then, you can just add @Async to your method that you want to set as async.
If you set @Async to class, all the method in the class will be executed as async.
If you set @Async to the method, only that method will be executed as async.
package org.blog.test.service.impl; import lombok.extern.slf4j.Slf4j; import org.blog.test.service.AsyncService; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Async @Slf4j @Service public class AsyncServiceImpl implements AsyncService { @Override public void doAsync() { log.info("async method"); } }
- execution result
2017-06-19 23:02:44.626 INFO 8336 --- [AsyncExecutor_1] o.b.test.service.impl.AsyncServiceImpl : async method
You can download all the example as below link.
https://gitlab.com/shashaka/async-executor-project
댓글 없음 :
댓글 쓰기