始终调用Spring自定义ThreadPoolTask Executor

问题描述 投票:0回答:1

我正在尝试将Spring上下文复制到Runnable / Callable任务以获取特殊情况。我希望其他线程在之前运行时运行。

我读过这个How to enable request scope in async task executor

并实现了一个自定义ThreadPoolTask​​Executor +装饰器。

@Configuration
public class ContextCopyConfig {

    private Integer connectionsLimit=10;

    @Bean(name = "contextExecutor")
    public Executor contextExecutor() {
        ThreadPoolTaskExecutor poolExecutor = new ThreadPoolTaskExecutor();
        poolExecutor.setTaskDecorator(new ContextCopyingDecorator());
        poolExecutor.setMaxPoolSize(connectionsLimit);
        poolExecutor.setCorePoolSize(connectionsLimit);

        poolExecutor.initialize();
        return poolExecutor;
    }

}

我打算使用这个执行程序如下:

@Autowired
@Qualifier(value = "contextExecutor")
private Executor contextExecutor;

public void parallelHere() throws IOException, InterruptedException, ExecutionException {
    Collection<Callable<Pair<String, OutputStream>>> tasks = new ArrayList<>(); //some tasks

    //ExecutorService executor = Executors.newFixedThreadPool(connectionsLimit); 

    List<Future<Pair<String, OutputStream>>> results = ((ThreadPoolTaskExecutor) contextExecutor).getThreadPoolExecutor().invokeAll(tasks);
    ((ThreadPoolTaskExecutor) contextExecutor).getThreadPoolExecutor().shutdown(); //always reclaim resources
}

但是,始终调用contextExecutor(在任何线程中!)。我该如何解决?

java spring spring-boot java.util.concurrent
1个回答
0
投票

这个帖子:

How to create additional TaskExecutor beside TaskExecutionAutoConfiguration?

描述了这个问题。仅当用户未创建自定义Executor时,Springboot才会创建默认Executor。在SpringBoot 2+中你必须使用

@AutoConfigureAfter(TaskExecutionAutoConfiguration.class)

在您的自定义配置上。

但是,在以前的Spring版本中,不存在TaskExecutionAutoConfiguration,并且Executor是由工厂创建的。使用下面的行,您可以创建由Spring创建的默认执行程序的exect副本。

@Primary
@Bean
//see package org.springframework.aop.interceptor.AsyncExecutionInterceptor
public Executor getDefaultExecutor(){
    //     Executor defaultExecutor = super.getDefaultExecutor(beanFactory);
    //     return (defaultExecutor != null ? defaultExecutor : new SimpleAsyncTaskExecutor());
    return new SimpleAsyncTaskExecutor();
}
© www.soinside.com 2019 - 2024. All rights reserved.