Multiple ThreadPoolTaskExecuters Spring Java Config

荒凉一梦 提交于 2019-12-10 18:22:16

问题


I have a need for multiple tasks executors in my application but I am not seeing how do so with Java Config. The XML version is straight forward but I must be missing something with the Java Config

I need two different executors with different queue and thread pool sizes, how can this be done using Java Config?

Here is my 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();
    }
}

回答1:


I think you can remove the interface, define your 2 Executors in your JavaConfig marked as @Bean(name = "nameOfExecutor") and then use the bean name in @Async("nameOfExecutor") as per the docs: 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.




回答2:


If you user Spring boot your can define a bean SchedulingConfigurer. In this bean you can override the metod configureTasks.

I think that you can try set the executor of each Task

@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);
    }
}


来源:https://stackoverflow.com/questions/32207023/multiple-threadpooltaskexecuters-spring-java-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!