TheadPoolTaskExecutorConfiguration.java 1.98 KB
package com.topdraw.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author :
 * @description:
 * @function :
 * @date :Created in 2022/6/22 11:06
 * @version: :
 * @modified By:
 * @since : modified in 2022/6/22 11:06
 */
@Configuration
public class TheadPoolTaskExecutorConfiguration {

    @Value("${task.pool.core-pool-size:16}")
    private Integer corePoolSize;
    @Value("${task.pool.core-pool-size:35}")
    private Integer maxPoolSize;
    @Value("${task.pool.keep-alive-seconds:10}")
    private Integer keepAliveSeconds;
    @Value("${task.pool.queue-capacity:300}")
    private Integer queueCapacity;

    @Bean
    @Primary
    public ThreadPoolTaskExecutor getThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }


    /*@Bean
    public ThreadPoolExecutor getThreadPoolTaskExecutor() {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(queueCapacity);
        ThreadPoolExecutor threadPoolTaskExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, arrayBlockingQueue);
        return threadPoolTaskExecutor;
    }*/

}