'How to intercept configuration of all ThreadPoolTaskExecutors?

I'd like to intercept the creation of all ThreadPoolTaskExecutors inside the application context, and add all of them a custom TaskDecorator.

Pseudocode:

public void interceptTaskExecutors(List<ThreadPoolTaskExecutor> executors) {
    var decorator = new MyTaskDecorator();
    executors.stream().forEach(executor -> executor.setTaskDecorator(decorator));
}

But how can I actually intercept the bean initialization process of all TaskExecutors to apply this?



Solution 1:[1]

@M. Deinum probably means as follows:

@Configuration
public class ThreadPoolCustomizer implements BeanPostProcessor {
    @Autowired
    private TaskDecorator decorator;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ThreadPoolTaskExecutor)
            ((ThreadPoolTaskExecutor) bean).setTaskDecorator(decorator);
        return bean;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 membersound