春天的靴子.如何使用注释创建TaskExecutor?

Pav*_*vlo 20 java concurrency spring asynchronous

@Service在Spring Boot应用程序中创建了一个类,其中一个方法应该异步运行.因为我读取方法应该@Async注释,我也必须运行一个TaskExecutorbean.但是在Spring手册http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html我没有找到任何信息或示例如何TaskExecutor使用注释运行,没有XML配置.是否可以TaskExecutor在没有XML的情况下在Spring Boot中创建bean,仅注释?这是我的服务类:

@Service
public class CatalogPageServiceImpl implements CatalogPageService {

    @Override
    public void processPagesList(List<CatalogPage> catalogPageList) {
        for (CatalogPage catalogPage:catalogPageList){
            processPage(catalogPage);
        }
    }

    @Override
    @Async("locationPageExecutor")
    public void processPage(CatalogPage catalogPage) {
        System.out.println("print from Async method "+catalogPage.getUrl());
    }
}
Run Code Online (Sandbox Code Playgroud)

Jes*_*per 36

@Bean在Spring Boot应用程序类中添加一个方法:

@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) {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅Spring Framework参考文档中有关如何使用Java配置而不是XML配置Spring 的基于Java的容器配置.

(注意:您不需要添加@Configuration到类中,因为@SpringBootApplication已经包含@Configuration).


Der*_*lin 15

更新:从Spring Boot 2.1开始,无需创建ThreadPoolTaskExecutor直通代码,因为它ThreadPoolTaskExecutor是默认值,并且可以使用前缀为 的属性来完全配置spring.task.execution

那么,步骤:

  1. @EnableAsync在带注释的类中使用@Configuration
  2. 注释一个或多个方法@Async
  3. ThreadPoolTaskExecutor可以选择使用属性覆盖默认配置

属性示例:

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
Run Code Online (Sandbox Code Playgroud)

如果需要更多定制,还可以实现该TaskExecutorCustomizer接口,例如(在 kotlin 中):

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
Run Code Online (Sandbox Code Playgroud)

  • 从 Spring Boot 2.1 开始,请参阅 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes#task-execution (2认同)

小智 6

首先 - 让我们回顾一下规则 - @Async有两个限制:

  • 它必须仅适用于公共方法
  • 自调用 - 从同一个类中调用异步方法 - 将无法正常工作

所以你的processPage()方法应该在单独的类中

  • @Async 不需要公开(至少不再需要) (2认同)
  • 为自我调用的评论点赞,回想起来应该很明显,但也不适合我:) (2认同)