Spring boot. How to Create TaskExecutor with Annotation?

后端 未结 3 1768
攒了一身酷
攒了一身酷 2020-12-30 00:15

I did a @Service class in Spring Boot application with one of the methods that should run asynchronously. As I read method should be @Async annotat

相关标签:
3条回答
  • 2020-12-30 00:18

    First – let’s go over the rules – @Async has two limitations:

    • it must be applied to public methods only
    • self-invocation – calling the async method from within the same class – won’t work

    So your processPage() method should be in separate class

    0 讨论(0)
  • 2020-12-30 00:24

    UPDATE: as of Spring Boot 2.2, one need not create the ThreadPoolTaskExecutor through code. Indeed, ThreadPoolTaskExecutor is the default, and can be completely configured using properties prefixed with spring.task.execution.

    So, steps:

    1. use @EnableAsync in a @Configuration-annotated class,
    2. annotate one or more methods with @Async
    3. optionally override the default ThreadPoolTaskExecutor config with properties

    Properties example:

    spring.task.execution.pool.core-size=1
    spring.task.execution.pool.max-size=20
    spring.task.execution.pool.keep-alive=120s
    spring.task.execution.pool.queue-capacity=1000
    spring.task.execution.shutdown.await-termination=true
    spring.task.execution.shutdown.await-termination-period=5m
    spring.task.execution.thread-name-prefix=async-executor-
    spring.task.execution.pool.allow-core-thread-timeout=false
    

    If more customization is required, one can also implement the TaskExecutorCustomizer interface, for example (in kotlin):

    @Component
    class AsyncExecutorCustomizer : TaskExecutorCustomizer {
        override fun customize(taskExecutor: ThreadPoolTaskExecutor?) {
            taskExecutor?.let { executor ->
                executor.setRejectedExecutionHandler(ThreadPoolExecutor.CallerRunsPolicy())
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 00:25

    Add a @Bean method to your Spring Boot application class:

    @SpringBootApplication
    @EnableAsync
    public class MySpringBootApp {
    
        @Bean
        public TaskExecutor taskExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(5);
            executor.setMaxPoolSize(10);
            executor.setQueueCapacity(25);
            return executor;
        }
    
        public static void main(String[] args) {
            // ...
        }
    }
    

    See Java-based container configuration in the Spring Framework reference documentation on how to configure Spring using Java config instead of XML.

    (Note: You don't need to add @Configuration to the class because @SpringBootApplication already includes @Configuration).

    0 讨论(0)
提交回复
热议问题