Вы можете обернуть ThreadPoolTaskExecutor в пользовательский класс, который запланирует проверку состояния задачи и отменит ее, если это не будет сделано.
public class CancellingExecutor implements Executor {
private final ExecutorService delegate;
private final ScheduledExecutorService monitoringExecutor;
public CancellingExecutor(final ExecutorService delegate,
final ScheduledExecutorService monitoringExecutor) {
this.delegate = delegate;
this.monitoringExecutor = monitoringExecutor;
}
@Override
public void execute(final Runnable command) {
Future<?> future = delegate.submit(command);
monitoringExecutor.schedule(() -> {
if (!future.isDone() && !future.isCancelled()) {
future.cancel(false);
}
}, 1, TimeUnit.MINUTES);
}
}
Конфигурация будет выглядеть следующим образом:
@Bean("asyncExecutor")
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(appConfigProperties.getThreadpoolCorePoolSize());
executor.setMaxPoolSize(appConfigProperties.getThreadpoolMaxPoolSize());
executor.setQueueCapacity(appConfigProperties.getThreadpoolQueueCapacity());
executor.setThreadNamePrefix("threadPoolExecutor-");
executor.initialize();
ScheduledExecutorService monitoringExecutor = Executors.newScheduledThreadPool(1);
return new CancellingExecutor(executor, monitoringExecutor);
}