I have a Spring @Configuration class as follows:
@Configuration
public class ExecutorServiceConfiguration {
@Bean
public BlockingQueue<Runnable> queue() {
return ArrayBlockingQueue<>(1000);
}
@Bean
public ExecutorService executor(BlockingQueue<Runnable> queue) {
return ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, queue);
}
@PreDestroy
public void shutdownExecutor() {
// no executor instance
}
}
I would also like to specify a @PreDestroy
method which shuts down the ExecutorService. However, the @PreDestroy
method cannot have any arguments which is why I'm not able to pass the executor
bean to this method in order to shut it. Specifying destroy method in @Bean(destroyMethod = "...")
does not work either. It allows me to specify existing shutdown
or shutdownNow
, but not the custom method which I intend to use.
I know I could instantiate the queue and executor directly and not as Spring beans, but I'd rather do it this way.
I love defining classes inline:
@Bean(destroyMethod = "myCustomShutdown")
public ExecutorService executor(BlockingQueue<Runnable> queue) {
return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, queue) {
public void myCustomShutdown() {
...
}
};
}