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