java - 如何在 Spring Boot 中关闭 ThreadPoolTaskExecutor 的特定任务(进程)?
问题描述
大家好
我想终止或关闭 ThreadPoolTaskExecutor 中的特定任务
例如,我想为将排队的每个任务设置一个唯一的 ID 或名称,然后使用我添加到此任务的 ID 或名称将其关闭
那是因为我有用户,我的应用程序用例迫使我为每个用户指定任务,但有时我想为特定用户关闭特定任务
例如
user.setTaskId('john-deo-1521');
user.setTaskId('john-deo-1522');
并关闭
taskExecutor.shutDownTaskById('john-deo-1521');
这是我对 ThreadPoolTaskExecutor 的主要配置
@Configuration
@EnableAsync
public class ServiceExecutorConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(30);
taskExecutor.setMaxPoolSize(40);
taskExecutor.setQueueCapacity(10);
taskExecutor.initialize();
return taskExecutor;
}
}