Spring boot. How to Create TaskExecutor with Annotation?

后端 未结 3 1789
攒了一身酷
攒了一身酷 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: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).

提交回复
热议问题