dangsn
2024-06-06 33321f5486fd586fda6fd3f46b7e71754fede28b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.vci.starter.web.autoconfigure;
 
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.Executor;
 
/**
 * timer需要的默认的线程池
 * @author weidy
 * @date 2021-2-2
 */
@Configuration
@EnableAsync
@ConditionalOnProperty(prefix = "vciasync",name="enabled",havingValue = "true",matchIfMissing = false)
public class VciAsyncConfig {
 
    /**
     * 初始化大小
     */
    private int corePoolSize;
 
    /**
     * 最大的值
     */
    private int maxPoolSize;
 
    /**
     * 队列的个数
     */
    private int queueCapacity;
 
    /**
     * 存活时间
     */
    private int keepAliveSeconds = 60;
 
    /**
     * 默认的线程池,
     * @return 线程池对象
     */
    @Bean
    @ConditionalOnMissingBean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new VciThreadPoolTaskExecutor();
        int cpu =  Runtime.getRuntime().availableProcessors();
        if(getCorePoolSize() == 0){
            setCorePoolSize(cpu);
        }
        if(getMaxPoolSize() == 0){
            setMaxPoolSize(2* cpu);
        }
        if(getQueueCapacity() == 0){
            setQueueCapacity(100);
        }
        executor.setCorePoolSize(getCorePoolSize());
        executor.setMaxPoolSize(getMaxPoolSize());
        executor.setQueueCapacity(getQueueCapacity());
        executor.setKeepAliveSeconds(getKeepAliveSeconds());
        executor.initialize();
        return executor;
    }
 
    public int getCorePoolSize() {
        return corePoolSize;
    }
 
    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }
 
    public int getMaxPoolSize() {
        return maxPoolSize;
    }
 
    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }
 
    public int getQueueCapacity() {
        return queueCapacity;
    }
 
    public void setQueueCapacity(int queueCapacity) {
        this.queueCapacity = queueCapacity;
    }
 
    public int getKeepAliveSeconds() {
        return keepAliveSeconds;
    }
 
    public void setKeepAliveSeconds(int keepAliveSeconds) {
        this.keepAliveSeconds = keepAliveSeconds;
    }
 
    @Override
    public String toString() {
        return "VciAsyncConfig{" +
                "corePoolSize=" + corePoolSize +
                ", maxPoolSize=" + maxPoolSize +
                ", queueCapacity=" + queueCapacity +
                '}';
    }
}