Customize thread pool parameters using configuration files

First create the thread pool MyThreadConfig:

@Configuration
public class MyThreadConfig {
    
    

     @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
    
    
       return new ThreadPoolExecutor(20,
                200,10,
                TimeUnit.SECONDS,new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}

Then create the thread pool configuration class ThreadPoolConfigProperties:

@ConfigurationProperties(prefix = "gulimall.thread")
@Component
@Data
public class ThreadPoolConfigProperties {
    
    
    //线程池核心大小
    private Integer coreSize;
    //最大大小
    private Integer maxSize;
    //休眠时长
    private Integer keepAlive;

}

Here is gulimall.threadto add dependencies to the prefix pom file in the configuration file
, which can automatically complete the configuration file

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

Related parameters can be configured in the configuration file:
insert image description here

After the configuration here is complete, you need to use a custom configuration class in MyThreadConfig:
Two methods:
First: Because ThreadPoolConfigProperties has been injected, use the annotation @EnableConfigurationProperties(ThreadPoolConfigProperties.class) directly on MyThreadConfig:
insert image description here
Second method:
set ThreadPoolConfigProperties passed in as a parameter:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42260782/article/details/131622807