| 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 ; | 
|     } | 
|   | 
| } |