I'm trying to copy Spring context to Runnable/Callable tasks for a special case. I want other threads to run as they run before.
I've read this How to enable request scope in async task executor
and implemented a custom ThreadPoolTaskExecutor + decorator.
@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;
}
}
I was planning to use this executor as follows:
@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
}
However, contextExecutor is always invoked (in any thread!). How can I fix it?