wuzeyu
2023-11-27 7c98e347015e96a7683dbb08b36495c75c54bea5
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
110
111
112
package com.dy.common.mw.support;
 
 
import com.dy.common.threadPool.TreadPoolFactory;
import com.dy.common.mw.UnitAdapterInterface;
import com.dy.common.mw.UnitInterface;
import com.dy.common.mw.UnitStartedCallbackInterface;
 
public class SupportUnit implements UnitInterface {
    
    private final static SupportUnit instance = new SupportUnit() ;
    private static boolean started = false ;
    
    private SupportUnitAdapter adapter ;
    
    private SupportUnitConfigVo confVo ;
    
 
    private SupportUnit(){}
    
    public static SupportUnit getInstance(){
        return instance ;
    }
    
    @Override
    public void setAdapter(UnitAdapterInterface adapter) throws Exception {
        if(adapter == null){
            throw new Exception("支持模块适配器对象不能为空!") ;
        }
        
        this.adapter = (SupportUnitAdapter)adapter ; 
        this.confVo = this.adapter.getConfig() ;
        if(this.confVo == null){
            throw new Exception("支持模块配置对象不能为空!") ;
        }
    }
 
    @Override
    public void start(UnitStartedCallbackInterface callback) throws Exception {
        if(!started){
            started = true ;
            if(confVo.enableThreadPool){
                TreadPoolFactory.initThreadPoolShort("短任务工作线程池", 
                        this.confVo.short_maxThread, 
                        this.confVo.short_minThread, 
                        this.confVo.short_freeTimeout, 
                        this.confVo.short_busyTimeout);
                TreadPoolFactory.initThreadPoolLong("长任务工作线程池", 
                        this.confVo.long_maxThread, 
                        this.confVo.long_minThread, 
                        this.confVo.long_freeTimeout, 
                        this.confVo.long_busyTimeout);
                
                if(this.confVo.showStartInfo){
                    System.out.println("线程池模块成功启动");
                }
            }
            callback.call(null);
        }
    }
 
    @Override
    public void stop(UnitStartedCallbackInterface callback) throws Exception {
    }
    
    
    public static void main(String[] args) throws Exception{
        SupportUnitConfigVo supVo = new SupportUnitConfigVo() ;
        //短工作时长线程池,线程负责用时较短的工作任务
        supVo.short_maxThread = 100 ;
        supVo.short_freeTimeout = 5 ;
        supVo.short_busyTimeout = 60 ;
        //长工作时长线程池,线程负责用时较长的工作任务
        supVo.long_maxThread = 200 ;
        if(supVo.long_maxThread < 0){
            supVo.long_maxThread = -1 ;
        }
        supVo.long_minThread = 0 ; 
        supVo.long_freeTimeout = 60 ;
        supVo.long_busyTimeout = -1 ;
        if(supVo.long_busyTimeout < 0){
            supVo.long_busyTimeout = -1 ;
        }
        
        supVo.enableThreadPool = true ;
        
        supVo.showStartInfo = true ;
 
        SupportUnit supUnit = SupportUnit.getInstance() ;
        
        AdapterImp_SupportUnit supAdap = new AdapterImp_SupportUnit();
        supAdap.setConfig(supVo);
        supUnit.setAdapter(supAdap);
        supUnit.start(obj -> {
        });
    }
    private static class AdapterImp_SupportUnit implements SupportUnitAdapter {
        
        private SupportUnitConfigVo configVo = null ;
 
        @Override
        public SupportUnitConfigVo getConfig() {
            return this.configVo;
        }
 
        public void setConfig(SupportUnitConfigVo configVo) {
            this.configVo = configVo;
        }
 
 
    }
}