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
113
114
115
116
117
118
119
120
121
package com.dy.common.mw.core;
 
import java.util.ArrayList;
import java.util.List;
 
import com.dy.common.queue.Queue;
import com.dy.common.mw.UnitAdapterInterface;
import com.dy.common.mw.UnitInterface;
import com.dy.common.mw.UnitCallbackInterface;
 
 
public class CoreUnit implements UnitInterface {
    
    private static final CoreUnit instance = new CoreUnit() ;
    private static boolean started = false ;
    
    //恒久任务,一旦加入,永久存在
    private static final List<CoreTask> constantTasks = new ArrayList<>() ;
    //任务队列,一次性任务,任务执行时从队列中清除
    protected static Queue taskQueue = new Queue("coreTaskQueue") ;
    
    public CoreUnitAdapter adapter ;
    
    private CoreUnit(){}
 
    public static CoreUnit getInstance(){
        return instance ;
    }
    
    @Override
    public void setAdapter(UnitAdapterInterface adapter) throws Exception {
        if(adapter == null){
            throw new Exception("核心模块适配器对象不能为空!") ;
        }
        this.adapter = (CoreUnitAdapter)adapter ; 
        CoreUnitConfigVo vo = this.adapter.getConfig() ;
        if(vo == null){
            throw new Exception("核心模块配置对象不能为空!") ;
        }
        if(vo.coreInterval == null){
            throw new Exception("核心模块配置对象间隔属性值不能为空!") ;
        }
        if(vo.coreInterval <= 0){
            throw new Exception("核心模块配置对象属性coreInterval值不能小于0!") ;
        }
        if(vo.coreInterval > 1000){
            throw new Exception("核心模块配置对象属性coreInterval值不能大于1000!") ;
        }
        if(vo.queueWarnSize == null || vo.queueMaxSize == null){
            throw new Exception("核心模块配置对象队列节点限制数量属性值不能为空!") ;
        }
        if(vo.queueWarnSize < 500){
            throw new Exception("核心模块配置对象队列节点警告限制数量属性值不能小于500!") ;
        }
        if(vo.queueMaxSize < 5000){
            throw new Exception("核心模块配置对象队列节点拒绝限制数量属性值不能小于5000!") ;
        }
        if(vo.queueWarnSize > vo.queueMaxSize){
            throw new Exception("核心模块配置对象队列节点警告限制数量属性值不能大于拒绝限制数量!") ;
        }
        taskQueue.setLimit(vo.queueWarnSize, vo.queueMaxSize);
    }
 
    @Override
    public void start(UnitCallbackInterface callback) throws Exception {
        if(!started){
            started = true ;
            /*
            CoreThread ct = CoreThread.getInstance() ;
            ct.setSleep(this.adapter.getConfig().sleepBigBusy, this.adapter.getConfig().sleepSmallBusy);
            ct.start();
            */
            CoreTimer ct = CoreTimer.getInstance() ;
            ct.setSleep(this.adapter.getConfig().coreInterval);
            ct.start();
 
            CoreConstantManage ccm = CoreConstantManage.getInstance() ;
            ccm.setSleep(this.adapter.getConfig().coreInterval);
            ccm.start();
            
            if(adapter.getConfig().showStartInfo){
                System.out.println("核心模块成功启动,"
                        + "主线程执行间隔:" + adapter.getConfig().coreInterval + "毫秒");
            }
            callback.call(null);
        }
    }
 
    @Override
    public void stop(UnitCallbackInterface callback) {
    }
    
 
    /**
     * 加入核心任务
     * @param task 核心任务
     * @throws Exception 异常
     */
    @SuppressWarnings("unused")
    public void pushCoreTask(CoreTask task) throws Exception{
        taskQueue.pushTail(task);
    }
    
    /**
     * 加入恒久任务
     * @param task  恒久任务
     */
    @SuppressWarnings("unused")
    public static void addConstantTask(CoreTask task){
        constantTasks.add(task) ;
    }
    
    /**
     * 得到所有恒久任务
     * @return 所有恒久任务
     */
    public static List<CoreTask> getAllConstantTasks(){
        return constantTasks ;
    }
 
}