多个 ThreadPoolTaskExecuters Spring Java 配置
Multiple ThreadPoolTaskExecuters Spring Java Config
我的应用程序中需要多个任务执行器,但我没有看到如何使用 Java Config。 XML 版本很简单,但我一定在 Java Config
中遗漏了一些东西
我需要两个具有不同队列和线程池大小的不同执行器,如何使用 Java 配置来完成?
这是我的 AsyncConfig class
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@EnableScheduling
@EnableAsync
@Configuration
public class AsyncConfig implements AsyncConfigurer {
@Autowired
Environment environment;
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(environment.getRequiredProperty("aysnc.executor.poolSize", Integer.class));
executor.setMaxPoolSize(environment.getRequiredProperty("aysnc.executor.maxPoolSize", Integer.class));
executor.setQueueCapacity(environment.getRequiredProperty("aysnc.executor.queueCapacity", Integer.class));
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}
我认为您可以删除接口,在标记为 @Bean(name = "nameOfExecutor")
的 JavaConfig 中定义 2 个执行器,然后根据文档在 @Async("nameOfExecutor")
中使用 bean 名称:May be used to determine the target executor to be used when executing this method, matching the qualifier value (or the bean name) of a specific Executor or TaskExecutor bean definition.
如果您的用户 Spring 引导您可以定义一个 bean SchedulingConfigurer。在此 bean 中,您可以覆盖方法 configureTasks。
我觉得你可以尝试设置每个任务的执行者
@Configuration
public class MySchedulingConfigurer implements SchedulingConfigurer {
@Autowired
@Qualifier(value="taskExecutor2")
Executor taskExecutor2;
@Autowired
@Qualifier(value="taskExecutor1")
Executor taskExecutor1;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
if (XXXXXXXXXX) {
taskRegistrar.setScheduler(taskExecutor1);
} else {
taskRegistrar.setScheduler(taskExecutor2);
}
}
@Bean(destroyMethod = "shutdown",name="taskExecutor2")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(10);
}
@Bean(destroyMethod = "shutdown",name="taskExecutor1")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
}
我的应用程序中需要多个任务执行器,但我没有看到如何使用 Java Config。 XML 版本很简单,但我一定在 Java Config
中遗漏了一些东西我需要两个具有不同队列和线程池大小的不同执行器,如何使用 Java 配置来完成?
这是我的 AsyncConfig class
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@EnableScheduling
@EnableAsync
@Configuration
public class AsyncConfig implements AsyncConfigurer {
@Autowired
Environment environment;
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(environment.getRequiredProperty("aysnc.executor.poolSize", Integer.class));
executor.setMaxPoolSize(environment.getRequiredProperty("aysnc.executor.maxPoolSize", Integer.class));
executor.setQueueCapacity(environment.getRequiredProperty("aysnc.executor.queueCapacity", Integer.class));
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}
我认为您可以删除接口,在标记为 @Bean(name = "nameOfExecutor")
的 JavaConfig 中定义 2 个执行器,然后根据文档在 @Async("nameOfExecutor")
中使用 bean 名称:May be used to determine the target executor to be used when executing this method, matching the qualifier value (or the bean name) of a specific Executor or TaskExecutor bean definition.
如果您的用户 Spring 引导您可以定义一个 bean SchedulingConfigurer。在此 bean 中,您可以覆盖方法 configureTasks。
我觉得你可以尝试设置每个任务的执行者
@Configuration
public class MySchedulingConfigurer implements SchedulingConfigurer {
@Autowired
@Qualifier(value="taskExecutor2")
Executor taskExecutor2;
@Autowired
@Qualifier(value="taskExecutor1")
Executor taskExecutor1;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
if (XXXXXXXXXX) {
taskRegistrar.setScheduler(taskExecutor1);
} else {
taskRegistrar.setScheduler(taskExecutor2);
}
}
@Bean(destroyMethod = "shutdown",name="taskExecutor2")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(10);
}
@Bean(destroyMethod = "shutdown",name="taskExecutor1")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
}