ThreadPoolExecutorConfig.java 1.25 KB
package cn.com.glsx.clue.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
@Component
public class ThreadPoolExecutorConfig {

    @Autowired
    private Environment env;

    @Bean
    public ThreadPoolExecutor templateExecutor() {
        Integer corePoolSize = env.getProperty("executor.template.corePoolSize", Integer.class, 4);
        Integer maxPoolSize = env.getProperty("executor.template.maxPoolSize", Integer.class, 20);
        Integer keepAliveTime = env.getProperty("executor.template.keepAliveTime", Integer.class, 60);
        Integer queueSize = env.getProperty("executor.template.queueSize", Integer.class, 20);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime,
                TimeUnit.SECONDS, new LinkedBlockingQueue(queueSize),
                new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}