diff --git a/CreeperModule/src/main/java/org/example/config/CreeperConfigFile.java b/CreeperModule/src/main/java/org/example/config/CreeperConfigFile.java index 1eb3f80..2cbe5e6 100644 --- a/CreeperModule/src/main/java/org/example/config/CreeperConfigFile.java +++ b/CreeperModule/src/main/java/org/example/config/CreeperConfigFile.java @@ -6,6 +6,7 @@ import org.example.bean.FileType; import org.example.constpool.CreeperModuleConstPool; import java.nio.file.Paths; +import java.util.List; import java.util.Map; /** @@ -18,8 +19,9 @@ public class CreeperConfigFile extends ConfigFile> { private static final String fileName = "creeperConfig.json"; - public CreeperConfigFile() { - super(filePath, fileName, + public CreeperConfigFile(String module, String pluginName, List needPlugins, boolean isAutoStart) { + + super(module,pluginName,needPlugins,isAutoStart,filePath, fileName, Map.of("taskCenter",new TaskCenterConfig(10,50,1000)), FileType.CREEPER); } diff --git a/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java b/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java index e00a602..fd9c047 100644 --- a/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java +++ b/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java @@ -1,28 +1,36 @@ package org.example.init; import org.example.config.CreeperConfigFile; +import org.example.constpool.ConstPool; import org.example.constpool.CreeperModuleConstPool; import org.example.constpool.PluginName; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.CommonPlugin; +import org.example.plugin.Plugin; +import org.example.taskcenter.TaskCenter; import org.example.util.FileUtil; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; /** * @author Genius * @date 2023/07/29 01:56 **/ -public class CreeperConfigInitMachine extends ConfigInitMachine { - public CreeperConfigInitMachine() { - super(PluginName.CREEPER_CONFIG_PLUGIN, - new CreeperConfigFile(), - ChopperLogFactory.getLogger(LoggerType.Creeper) - ); +@Plugin(moduleName = ConstPool.CREEPER, + pluginName = PluginName.CREEPER_CONFIG_PLUGIN, + needPlugin = {PluginName.FILE_CACHE_PLUGIN}, + pluginClass= CreeperConfigFile.class ) +public class CreeperConfigInitMachine extends ConfigInitMachine { + + + public CreeperConfigInitMachine(List needPlugins, boolean isAutoStart, String moduleName, String name, Class clazz) { + super(needPlugins, isAutoStart, moduleName, name, clazz); } @Override diff --git a/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java b/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java index 1e1039c..ebe390b 100644 --- a/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java +++ b/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java @@ -14,40 +14,20 @@ import java.util.List; * @date 2023/07/28 23:49 **/ -@Plugin(moduleName = ConstPool.CREEPER,pluginName = PluginName.TASK_CENTER_PLUGIN,needPlugin = {PluginName.FILE_CACHE_PLUGIN}) +@Plugin(moduleName = ConstPool.CREEPER, + pluginName = PluginName.TASK_CENTER_PLUGIN, + needPlugin = {PluginName.FILE_CACHE_PLUGIN,PluginName.CREEPER_CONFIG_PLUGIN}, + pluginClass= TaskCenter.class ) public class TaskCenterInitMachine extends CommonInitMachine{ - public TaskCenterInitMachine() { - super(List.of(PluginName.FILE_CACHE_PLUGIN), - ChopperLogFactory.getLogger(LoggerType.Creeper), - PluginName.TASK_CENTER_PLUGIN); - } - @Override - public boolean init() { - try { - TaskCenter center = TaskCenter.center(); - if(center==null){ - return fail(); - } - center.guardian(); - }catch (Exception e){ - return fail(); - } - return success(); + public TaskCenterInitMachine(List needPlugins, boolean isAutoStart, String moduleName, String name, Class clazz) { + super(needPlugins, isAutoStart, moduleName, name, clazz); } @Override public void shutdown() { - TaskCenter center = TaskCenter.center(); - if(center!=null){ - center.shutdown(); - } + this.getPlugin().shutdown(); } - @Override - public void afterInit() { - //爬虫任务恢复 - TaskCenter.center().restoreTaskCenter(); - } } diff --git a/CreeperModule/src/main/java/org/example/init/module/CreeperModuleInitMachine.java b/CreeperModule/src/main/java/org/example/init/module/CreeperModuleInitMachine.java index 8658a9d..49c821f 100644 --- a/CreeperModule/src/main/java/org/example/init/module/CreeperModuleInitMachine.java +++ b/CreeperModule/src/main/java/org/example/init/module/CreeperModuleInitMachine.java @@ -16,15 +16,8 @@ import static org.example.constpool.ConstPool.FILE; * @date 2023/07/29 01:58 **/ public class CreeperModuleInitMachine extends ModuleInitMachine { - public CreeperModuleInitMachine() { - super(List.of(FILE), - ChopperLogFactory.getLogger(LoggerType.Creeper), - List.of( - new CreeperConfigInitMachine(), - new TaskCenterInitMachine()), - CREEPER); + super(List.of(FILE), ChopperLogFactory.getLogger(LoggerType.Creeper), CREEPER); } - } diff --git a/CreeperModule/src/main/java/org/example/taskcenter/TaskCenter.java b/CreeperModule/src/main/java/org/example/taskcenter/TaskCenter.java index 22dcbab..1bb188e 100644 --- a/CreeperModule/src/main/java/org/example/taskcenter/TaskCenter.java +++ b/CreeperModule/src/main/java/org/example/taskcenter/TaskCenter.java @@ -11,6 +11,7 @@ import org.example.constpool.PluginName; import org.example.exception.FileCacheException; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.GuardPlugin; import org.example.taskcenter.handler.BootStrapTaskHandler; import org.example.taskcenter.request.ReptileRequest; import org.example.taskcenter.task.ReptileTask; @@ -18,6 +19,7 @@ import org.example.thread.ChopperBotGuardianTask; import org.example.util.ConfigFileUtil; import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.concurrent.*; import java.util.concurrent.locks.ReentrantLock; @@ -33,14 +35,12 @@ import java.util.concurrent.locks.ReentrantLock; * 2,记录正在运行的爬虫任务 * 3,未完成的爬虫任务修复 */ -public class TaskCenter implements ChopperBotGuardianTask{ +public class TaskCenter extends GuardPlugin { private long waitingQueueTime; //等待队列时间 private int threads; //处理任务的线程数量 - private volatile static TaskCenter taskCenter; - private Map runningTask; //正在运行的任务 private BlockingQueue waitingTask; //正在等待运行的任务 @@ -54,42 +54,38 @@ public class TaskCenter implements ChopperBotGuardianTask{ private ReentrantLock lock = new ReentrantLock(); - public TaskCenter() { + public TaskCenter(String module, String pluginName, List needPlugins, boolean isAutoStart) { + super(module, pluginName, needPlugins, isAutoStart); + afterDo = false; } - private TaskCenter(int waitingQueueTime, int threads, int capacity){ - this.runningTask = new ConcurrentHashMap<>(); - this.waitingTask = new ArrayBlockingQueue<>(capacity); - this.bootStrapTaskHandler = new BootStrapTaskHandler(); - this.taskPool = Executors.newFixedThreadPool(threads); - this.waitingQueueTime = waitingQueueTime; - this.threads = threads; - } - public static TaskCenter center(){ - if(taskCenter==null){ - synchronized (TaskCenter.class){ - if(taskCenter==null){ - if (!init()) { - return null; - } - } + @Override + public void start() { + try { + ReptileTask task = waitingTask.poll(waitingQueueTime,TimeUnit.MILLISECONDS); + if(task!=null){ + runningTask.put(task.getTaskId(),task); + taskPool.submit(task::reptile); } + } catch (InterruptedException e) { + throw new RuntimeException(e); } - return taskCenter; } - private static boolean init(){ + public boolean init(){ FileCache configFileCache = FileCacheManagerInstance.getInstance().getFileCache(CreeperConfigFile.getFullFilePath()); Object obj = configFileCache.get("taskCenter"); if (obj!=null) { TaskCenterConfig taskCenterConfig = JSONObject.parseObject(obj.toString(), TaskCenterConfig.class); - taskCenter = new TaskCenter( - taskCenterConfig.getWaitingTime(), - taskCenterConfig.getThreads(), - taskCenterConfig.getQueueCapacity() - ); - return taskCenter.newLogFile(new CreeperLogConfigFile(new ArrayList<>())); + this.threads = taskCenterConfig.getThreads(); + this.runningTask = new ConcurrentHashMap<>(); + this.waitingTask = new ArrayBlockingQueue<>(taskCenterConfig.getQueueCapacity()); + this.bootStrapTaskHandler = new BootStrapTaskHandler(); + this.taskPool = Executors.newFixedThreadPool(threads); + this.waitingQueueTime = taskCenterConfig.getWaitingTime(); + super.init(); + return newLogFile(new CreeperLogConfigFile(new ArrayList<>())); } return false; } @@ -106,20 +102,6 @@ public class TaskCenter implements ChopperBotGuardianTask{ } } - public void work(){ - ChopperLogFactory.getLogger(LoggerType.Creeper) - .info("TaskCenter start to work.threads:{},waitingTime:{}s",threads,waitingQueueTime/1000); - while(true){ - try { - ReptileTask task = waitingTask.take(); - runningTask.put(task.getTaskId(),task); - taskPool.submit(task::reptile); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - } - public boolean finishTask(String taskId){ ReptileTask reptileTask; if ((reptileTask=runningTask.remove(taskId))!=null) { @@ -189,21 +171,18 @@ public class TaskCenter implements ChopperBotGuardianTask{ PluginName.TASK_CENTER_PLUGIN,restoreNum); } - public boolean shutdown(){ - taskPool.shutdown(); - return taskPool.isShutdown(); - } - public void request(ReptileRequest request){ ReptileTask task = bootStrapTaskHandler.distribute(request); if(task!=null){ addTask(task); } } - @Override - public void threadTask() { - this.work(); + public void shutdown(){ + super.shutdown(); + taskPool.shutdown(); } + + } diff --git a/CreeperModule/src/main/java/org/example/taskcenter/task/ReptileTask.java b/CreeperModule/src/main/java/org/example/taskcenter/task/ReptileTask.java index 68286d3..376799f 100644 --- a/CreeperModule/src/main/java/org/example/taskcenter/task/ReptileTask.java +++ b/CreeperModule/src/main/java/org/example/taskcenter/task/ReptileTask.java @@ -1,10 +1,15 @@ package org.example.taskcenter.task; import lombok.Data; +import org.example.constpool.PluginName; import org.example.core.control.LoadTask; +import org.example.init.InitPluginRegister; +import org.example.init.TaskCenterInitMachine; +import org.example.plugin.CommonPlugin; import org.example.taskcenter.TaskCenter; import org.example.taskcenter.request.ReptileRequest; import org.example.util.TimeUtil; +import us.codecraft.webmagic.Task; import java.time.LocalDateTime; @@ -48,7 +53,8 @@ public class ReptileTask { //完成任务 request.response(res); //让请求响应结果 - TaskCenter.center().finishTask(taskId); + TaskCenter plugin = (TaskCenter) InitPluginRegister.getPlugin(PluginName.TASK_CENTER_PLUGIN); + plugin.finishTask(taskId); this.type = TaskStatus.Finish; this.endTime = TimeUtil.getNowTime_YMDHMS(); } diff --git a/FileModule/src/main/java/org/example/cache/FileCacheManager.java b/FileModule/src/main/java/org/example/cache/FileCacheManager.java index d58b925..7113b30 100644 --- a/FileModule/src/main/java/org/example/cache/FileCacheManager.java +++ b/FileModule/src/main/java/org/example/cache/FileCacheManager.java @@ -2,6 +2,7 @@ package org.example.cache; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.CommonPlugin; import org.example.util.TimeUtil; import java.util.List; @@ -18,11 +19,11 @@ import static java.lang.Thread.sleep; /** * 文件自动刷入管理类,不断监听文件是否需要自动写入 */ -public class FileCacheManager { +public class FileCacheManager extends CommonPlugin { - private final List fileCaches; + private List fileCaches; - private final ConcurrentHashMap fileCacheMap; + private ConcurrentHashMap fileCacheMap; private AtomicLong sleepTime; //睡眠时间 @@ -32,15 +33,25 @@ public class FileCacheManager { private volatile Watcher watcher; - protected FileCacheManager(List fileCaches){ - this.fileCaches = new CopyOnWriteArrayList<>(fileCaches); - fileCacheMap = new ConcurrentHashMap<>(); - for (FileCache fileCache : fileCaches) { - fileCacheMap.put(fileCache.getFullFilePath(),fileCache); + public FileCacheManager(String module, String pluginName, List needPlugins, boolean isAutoStart) { + super(module, pluginName, needPlugins, isAutoStart); + } + + @Override + public boolean init() { + try { + this.fileCaches = new CopyOnWriteArrayList<>(); + fileCacheMap = new ConcurrentHashMap<>(); + for (FileCache fileCache : fileCaches) { + fileCacheMap.put(fileCache.getFullFilePath(),fileCache); + } + initSleepTime(); + this.watchPool = Executors.newSingleThreadExecutor(); + this.autoSyncer = Executors.newFixedThreadPool(fileCaches.size()+20); + }catch (Exception e){ + return false; } - initSleepTime(); - this.watchPool = Executors.newSingleThreadExecutor(); - this.autoSyncer = Executors.newFixedThreadPool(fileCaches.size()); + return true; } /** @@ -141,4 +152,12 @@ public class FileCacheManager { return watchPool.isShutdown()&&autoSyncer.isShutdown(); } + @Override + public void shutdown() { + close(); + } + + public void setFileCaches(List fileCaches) { + this.fileCaches = fileCaches; + } } diff --git a/FileModule/src/main/java/org/example/cache/FileCacheManagerInstance.java b/FileModule/src/main/java/org/example/cache/FileCacheManagerInstance.java index 9a6435a..a0c6f80 100644 --- a/FileModule/src/main/java/org/example/cache/FileCacheManagerInstance.java +++ b/FileModule/src/main/java/org/example/cache/FileCacheManagerInstance.java @@ -1,6 +1,8 @@ package org.example.cache; import org.example.constpool.GlobalFileCache; +import org.example.constpool.PluginName; +import org.example.init.InitPluginRegister; import java.util.ArrayList; import java.util.List; @@ -13,18 +15,21 @@ import java.util.List; //FileCacheManager单例实体类 public class FileCacheManagerInstance { - //获取全局的一个fileCaches - private static List fileCaches = List.of(GlobalFileCache.ModuleSrcConfigFile); private static volatile FileCacheManager Instance; public static FileCacheManager getInstance(){ if(Instance==null){ synchronized (FileCacheManagerInstance.class){ if(Instance==null){ - Instance = new FileCacheManager(fileCaches); + Instance = InitPluginRegister.getPlugin(PluginName.HOT_RECOMMENDATION_PLUGIN, FileCacheManager.class); } } } return Instance; } + + public static void initInstance(FileCacheManager fileCacheManager){ + Instance = fileCacheManager; + fileCacheManager.addFileCache(GlobalFileCache.ModuleSrcConfigFile); + } } diff --git a/FileModule/src/main/java/org/example/init/ConfigInitMachine.java b/FileModule/src/main/java/org/example/init/ConfigInitMachine.java index e237aa6..20c25b2 100644 --- a/FileModule/src/main/java/org/example/init/ConfigInitMachine.java +++ b/FileModule/src/main/java/org/example/init/ConfigInitMachine.java @@ -1,35 +1,44 @@ package org.example.init; import org.example.bean.ConfigFile; +import org.example.plugin.CommonPlugin; +import org.example.plugin.Plugin; import org.example.util.ConfigFileUtil; import org.slf4j.Logger; import java.nio.file.Paths; +import java.util.List; /** * @author Genius * @date 2023/07/29 00:08 **/ -public abstract class ConfigInitMachine extends CommonInitMachine{ +public abstract class ConfigInitMachine extends CommonInitMachine{ - private T configFile; + private ConfigFile configFile; - private String filePath; - public ConfigInitMachine(String pluginName,T configFile, Logger logger) { - super(logger,pluginName); - this.configFile = configFile; - filePath = Paths.get(configFile.getFilePath(), configFile.getFileName()).toString(); + + public ConfigInitMachine(List needPlugins, boolean isAutoStart, String moduleName, String name, Class clazz) { + super(needPlugins, isAutoStart, moduleName, name, clazz); } - - @Override public boolean init() { - if (ConfigFileUtil.createConfigFile(filePath,configFile,logger,pluginName)) { - return true; + Plugin ano = this.getClass().getAnnotation(Plugin.class); + try { + configFile = (ConfigFile) ano.pluginClass() + .getDeclaredConstructor(String.class,String.class,List.class,boolean.class) + .newInstance(moduleName,pluginName,needPlugins,isAutoStart); + if (ConfigFileUtil.createConfigFile(Paths.get(configFile.getFilePath(), configFile.getFileName()).toString(),configFile,logger,pluginName)) { + return true; + }else{ + return false; + } + }catch (Exception e){ + return fail(e.getMessage()); } - return false; + } diff --git a/FileModule/src/main/java/org/example/init/FileCacheManagerInitMachine.java b/FileModule/src/main/java/org/example/init/FileCacheManagerInitMachine.java index 9279fc9..30d752c 100644 --- a/FileModule/src/main/java/org/example/init/FileCacheManagerInitMachine.java +++ b/FileModule/src/main/java/org/example/init/FileCacheManagerInitMachine.java @@ -1,33 +1,49 @@ package org.example.init; +import org.example.cache.FileCacheManager; import org.example.cache.FileCacheManagerInstance; +import org.example.constpool.ConstPool; import org.example.constpool.PluginName; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.CommonPlugin; +import org.example.plugin.Plugin; + +import java.util.List; /** * @author Genius * @date 2023/04/26 02:09 **/ + +@Plugin(moduleName = ConstPool.FILE, + pluginName = PluginName.FILE_CACHE_PLUGIN, + needPlugin = {}, + pluginClass= FileCacheManager.class ) public class FileCacheManagerInitMachine extends CommonInitMachine{ - public FileCacheManagerInitMachine() { - super(ChopperLogFactory.getLogger(LoggerType.File), PluginName.FILE_CACHE_PLUGIN); + + public FileCacheManagerInitMachine(List needPlugins, boolean isAutoStart, String moduleName, String name, Class clazz) { + super(needPlugins, isAutoStart, moduleName, name, clazz); } @Override public boolean init() { + Plugin ano = this.getClass().getAnnotation(Plugin.class); try { - FileCacheManagerInstance.getInstance().start(); - return success(); + plugin = ano.pluginClass() + .getDeclaredConstructor(String.class,String.class,List.class,boolean.class) + .newInstance(moduleName,pluginName,needPlugins,isAutoStart); + if (plugin.init()) { + FileCacheManagerInstance.initInstance((FileCacheManager) plugin); + FileCacheManagerInstance.getInstance().start(); + return success(); + }else{ + return fail(); + } }catch (Exception e){ return fail(e.getMessage()); } } - @Override - public void shutdown() { - super.shutdown(); - FileCacheManagerInstance.getInstance().close(); - } } diff --git a/FileModule/src/main/java/org/example/init/module/FileModuleInitMachine.java b/FileModule/src/main/java/org/example/init/module/FileModuleInitMachine.java index 2984080..154caaf 100644 --- a/FileModule/src/main/java/org/example/init/module/FileModuleInitMachine.java +++ b/FileModule/src/main/java/org/example/init/module/FileModuleInitMachine.java @@ -1,10 +1,12 @@ package org.example.init.module; import org.example.constpool.ConstPool; +import org.example.constpool.PluginName; import org.example.init.FileCacheManagerInitMachine; import org.example.init.ModuleInitMachine; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.Plugin; import java.util.List; @@ -12,12 +14,11 @@ import java.util.List; * @author Genius * @date 2023/07/22 18:47 **/ + public class FileModuleInitMachine extends ModuleInitMachine { public FileModuleInitMachine() { - super(List.of( - new FileCacheManagerInitMachine() - ), ConstPool.FILE, ChopperLogFactory.getLogger(LoggerType.File)); + super(ConstPool.FILE, ChopperLogFactory.getLogger(LoggerType.File)); } } diff --git a/FileModule/src/main/java/org/example/pojo/CommonConfigFile.java b/FileModule/src/main/java/org/example/pojo/CommonConfigFile.java deleted file mode 100644 index b78c932..0000000 --- a/FileModule/src/main/java/org/example/pojo/CommonConfigFile.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.example.pojo; - -import org.example.bean.ConfigFile; - -/** - * @author Genius - * @date 2023/04/26 00:12 - **/ -public class CommonConfigFile extends ConfigFile { - - public CommonConfigFile(String filePath, String fileName, Object data) { - super(filePath, fileName, data); - } -} diff --git a/FileModule/src/main/java/org/example/pojo/configfile/ModuleSrcConfigFile.java b/FileModule/src/main/java/org/example/pojo/configfile/ModuleSrcConfigFile.java index 65d7f35..979a3fa 100644 --- a/FileModule/src/main/java/org/example/pojo/configfile/ModuleSrcConfigFile.java +++ b/FileModule/src/main/java/org/example/pojo/configfile/ModuleSrcConfigFile.java @@ -3,8 +3,10 @@ package org.example.pojo.configfile; import org.example.bean.ConfigFile; import org.example.bean.FileType; import org.example.constpool.ConstPool; +import org.example.constpool.PluginName; import java.nio.file.Paths; +import java.util.List; import java.util.Map; /** @@ -18,19 +20,20 @@ public class ModuleSrcConfigFile extends ConfigFile> { private static final String filePath = "./config/"; private static final String fileName = "chopperBotConfig.json"; + public ModuleSrcConfigFile() { - super( filePath, fileName, - Map.of("src",Map.of( ConstPool.ACCOUNT, "./config/"+ConstPool.ACCOUNT, - ConstPool.SECTION, "./config/"+ConstPool.SECTION, - ConstPool.BARRAGE, "./config/"+ConstPool.BARRAGE, - ConstPool.CREEPER, "./config/"+ConstPool.CREEPER, - ConstPool.SECTION_WORK, "./config/"+ConstPool.SECTION_WORK, - ConstPool.HOT, "./config/"+ConstPool.HOT, - ConstPool.PUBLISH, "./config/"+ConstPool.PUBLISH), - "plugin",Map.of()), - FileType.CHOPPER_BOT); + super( "ChopperBot", PluginName.MODULE_CONFIG_PLUGIN,List.of(),true,filePath, fileName, + Map.of("src",Map.of( ConstPool.ACCOUNT, "./config/"+ConstPool.ACCOUNT, + ConstPool.SECTION, "./config/"+ConstPool.SECTION, + ConstPool.BARRAGE, "./config/"+ConstPool.BARRAGE, + ConstPool.CREEPER, "./config/"+ConstPool.CREEPER, + ConstPool.SECTION_WORK, "./config/"+ConstPool.SECTION_WORK, + ConstPool.HOT, "./config/"+ConstPool.HOT, + ConstPool.PUBLISH, "./config/"+ConstPool.PUBLISH), + "plugin",Map.of()), FileType.CHOPPER_BOT); } + public Map packageConfig() { return super.packageConfig(); } diff --git a/FileModule/src/test/java/org/example/cache/FileCacheTest.java b/FileModule/src/test/java/org/example/cache/FileCacheTest.java index a409f61..b0ed6c0 100644 --- a/FileModule/src/test/java/org/example/cache/FileCacheTest.java +++ b/FileModule/src/test/java/org/example/cache/FileCacheTest.java @@ -1,7 +1,6 @@ package org.example.cache; import org.example.constpool.GlobalFileCache; -import org.example.pojo.CommonConfigFile; import org.example.pojo.Student; import org.example.pojo.configfile.ModuleSrcConfigFile; import org.example.exception.FileCacheException; diff --git a/HotModule/src/main/java/org/example/config/HotModuleConfig.java b/HotModule/src/main/java/org/example/config/HotModuleConfig.java index 5db13a4..f0fbcca 100644 --- a/HotModule/src/main/java/org/example/config/HotModuleConfig.java +++ b/HotModule/src/main/java/org/example/config/HotModuleConfig.java @@ -22,6 +22,23 @@ public class HotModuleConfig extends ConfigFile> { private static final long OneDay = 0x5265C00; private static final String fileName = "hotModuleConfig.json"; + public HotModuleConfig(String module, String pluginName, List needPlugins, boolean isAutoStart) { + super(module, pluginName, needPlugins, isAutoStart + ,HotModuleConstPool.HOT_MODULE_CONFIG_ROOT,fileName, + Map.of("Module", List.of( + new HotModuleSetting(CreeperModuleConstPool.DOUYU,2,true, true,true, new ArrayList<>(), false, + List.of(allLiveDog()), OneDay, FiveMinute), + new HotModuleSetting(CreeperModuleConstPool.BILIBILI, 2,true,true,true, new ArrayList<>(), false, + List.of(allLiveDog()), OneDay, FiveMinute), + new HotModuleSetting(CreeperModuleConstPool.HUYA,2, true,true,true, new ArrayList<>(), false, + List.of(allLiveDog()), OneDay, FiveMinute), + new HotModuleSetting(CreeperModuleConstPool.DOUYING, 2,true,true,true, new ArrayList<>(), false, + List.of(allLiveDog()), OneDay, FiveMinute) + ), + "GuardNum",10 + ), FileType.HOT); + } + public HotModuleConfig(){ super(HotModuleConstPool.HOT_MODULE_CONFIG_ROOT,fileName, Map.of("Module", List.of( diff --git a/HotModule/src/main/java/org/example/core/guard/Guard.java b/HotModule/src/main/java/org/example/core/guard/Guard.java index c39062f..20d3ceb 100644 --- a/HotModule/src/main/java/org/example/core/guard/Guard.java +++ b/HotModule/src/main/java/org/example/core/guard/Guard.java @@ -7,9 +7,11 @@ import org.example.bean.hotmodule.HotModuleList; import org.example.constpool.PluginName; import org.example.core.HotModuleDataCenter; import org.example.core.control.HotModuleLoadTask; +import org.example.core.recommend.HeatRecommendation; import org.example.init.HeatRecommendationInitMachine; import org.example.init.InitPluginRegister; import org.example.log.ResultLogger; +import org.example.plugin.CommonPlugin; import org.slf4j.Logger; import java.util.List; @@ -65,7 +67,9 @@ public class Guard implements Runnable, ResultLogger { HotModuleDataCenter.DataCenter().addLiveList(platform,(List) data); //查看热度推送插件是否装载,如果装载则进行热度推送 if(InitPluginRegister.isRegister(PluginName.HOT_RECOMMENDATION_PLUGIN)){ - HeatRecommendationInitMachine.heatRecommendation.sendHotEvent(platform); + HeatRecommendation plugin = (HeatRecommendation) InitPluginRegister.getPlugin(PluginName.HOT_RECOMMENDATION_PLUGIN); + assert plugin != null; + plugin.sendHotEvent(platform); } } diff --git a/HotModule/src/main/java/org/example/core/guard/HotModuleGuard.java b/HotModule/src/main/java/org/example/core/guard/HotModuleGuard.java index f20982f..0889146 100644 --- a/HotModule/src/main/java/org/example/core/guard/HotModuleGuard.java +++ b/HotModule/src/main/java/org/example/core/guard/HotModuleGuard.java @@ -1,41 +1,94 @@ package org.example.core.guard; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import lombok.Data; import org.example.cache.FileCache; import org.example.cache.FileCacheManagerInstance; import org.example.config.HotModuleConfig; +import org.example.config.HotModuleSetting; import org.example.constpool.HotModuleConstPool; import org.example.core.control.HotModuleLoadTask; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.CommonPlugin; import org.example.thread.NamedThreadFactory; +import org.example.util.ClassUtil; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.*; +import static org.example.constpool.HotModuleConstPool.LOAD_TASK_CLASS_ROOT; + /** * @author Genius * @date 2023/07/19 03:21 **/ -public class HotModuleGuard { +public class HotModuleGuard extends CommonPlugin { private List guards; //热度监控守卫列表,用于初始化一开始的热度监控列表 private ScheduledExecutorService hotModuleGuardPool; //热度监控守卫 定时线程池 private Map runningGuards; // 运行的热度监控守卫 - - - protected HotModuleGuard(List guards,int guardNum){ - this.guards = guards; - this.hotModuleGuardPool = Executors.newScheduledThreadPool(guardNum, new NamedThreadFactory("HotModuleGuard")); - runningGuards = new ConcurrentHashMap<>(); + public HotModuleGuard(String module, String pluginName, List needPlugins, boolean isAutoStart) { + super(module, pluginName, needPlugins, isAutoStart); } + @Override + public boolean init() { + try { + FileCache HotModuleFileCache = FileCacheManagerInstance.getInstance().getFileCache(HotModuleConfig.getFullFilePath()); + + List guards = new ArrayList<>(); + int guardNum = (Integer)HotModuleFileCache.get("GuardNum"); + Map map = new HashMap<>(); + JSONArray modules = (JSONArray)HotModuleFileCache.get("Module"); + for (Object module : modules) { + HotModuleSetting hotModuleSetting = JSONObject.parseObject(module.toString(), HotModuleSetting.class); + map.put(hotModuleSetting.getPlatform(),hotModuleSetting); + } + LoggerType type = ChopperLogFactory.nameToType.get(getModule()); + for (String clazz : ClassUtil.getClassesInPackage(LOAD_TASK_CLASS_ROOT)) { + String[] split = clazz.split("\\."); + String clazzName = split[split.length-1].toLowerCase(); + if(clazzName.endsWith("loadtask")&& clazzName.contains("hot")){ + String platformName = clazzName.split("hot")[0]; + boolean isHotModule = clazzName.contains("module"); + if(map.containsKey(platformName)){ + HotModuleSetting hotModuleSetting = map.get(platformName); + Class loadClazz = Class.forName(clazz); + if(isHotModule&&hotModuleSetting.isEnableHotModule()){ + HotModuleLoadTask task = (HotModuleLoadTask)loadClazz.getDeclaredConstructor().newInstance(); + guards.add(new Guard(ChopperLogFactory.getLogger(type),clazzName,task, + hotModuleSetting.getUpdateHotModuleTimes(),hotModuleSetting.getFailRetryTimes())); + }else if(hotModuleSetting.isEnableHotLive()){ + HotModuleLoadTask task = (HotModuleLoadTask)loadClazz.getDeclaredConstructor().newInstance(); + guards.add(new Guard(ChopperLogFactory.getLogger(type),clazzName,task, + hotModuleSetting.getUpdateHotLivesTimes(),hotModuleSetting.getFailRetryTimes())); + } + } + } + } + this.guards = guards; + this.hotModuleGuardPool = Executors.newScheduledThreadPool(guardNum, new NamedThreadFactory("HotModuleGuard")); + runningGuards = new ConcurrentHashMap<>(); + start(); + }catch (Exception e){ + return false; + } + return true; + } private void guardStart(Guard guard){ - ScheduledFuture scheduledFuture = hotModuleGuardPool.scheduleWithFixedDelay( - guard, 0, guard.getDelayTime(), TimeUnit.MILLISECONDS - ); - runningGuards.put(guard.getGuardName(),scheduledFuture); + if(!runningGuards.containsKey(guard.getGuardName())){ + ScheduledFuture scheduledFuture = hotModuleGuardPool.scheduleWithFixedDelay( + guard, 0, guard.getDelayTime(), TimeUnit.MILLISECONDS + ); + runningGuards.put(guard.getGuardName(),scheduledFuture); + } + } public void start(){ if(runningGuards.size()==0){ @@ -49,6 +102,11 @@ public class HotModuleGuard { } } + @Override + public void shutdown() { + close(); + } + public boolean close(){ hotModuleGuardPool.shutdown(); runningGuards.clear(); diff --git a/HotModule/src/main/java/org/example/core/guard/HotModuleGuardInstance.java b/HotModule/src/main/java/org/example/core/guard/HotModuleGuardInstance.java index d583fff..07b42cb 100644 --- a/HotModule/src/main/java/org/example/core/guard/HotModuleGuardInstance.java +++ b/HotModule/src/main/java/org/example/core/guard/HotModuleGuardInstance.java @@ -1,6 +1,9 @@ package org.example.core.guard; +import org.example.constpool.PluginName; +import org.example.init.InitPluginRegister; + import java.util.ArrayList; import java.util.List; @@ -10,25 +13,18 @@ import java.util.List; **/ public class HotModuleGuardInstance { - private static List guardList = new ArrayList<>(); - - private static int guardNum; private static volatile HotModuleGuard Instance; public static HotModuleGuard getInstance(){ if(Instance==null){ synchronized (HotModuleGuardInstance.class){ if(Instance==null){ - Instance = new HotModuleGuard(guardList,guardNum); + Instance = InitPluginRegister.getPlugin(PluginName.HOT_GUARD_PLUGIN,HotModuleGuard.class); } } } return Instance; } - public static void InitInstance(List guards,int num){ - guardList = guards; - guardNum = num; - } } diff --git a/HotModule/src/main/java/org/example/core/recommend/HeatRecommendation.java b/HotModule/src/main/java/org/example/core/recommend/HeatRecommendation.java index 192e181..551f71f 100644 --- a/HotModule/src/main/java/org/example/core/recommend/HeatRecommendation.java +++ b/HotModule/src/main/java/org/example/core/recommend/HeatRecommendation.java @@ -15,8 +15,12 @@ import org.example.config.HotModuleSetting; import org.example.constpool.PluginName; import org.example.core.HotModuleDataCenter; import org.example.exception.InitException; +import org.example.init.InitPluginRegister; +import org.example.init.TaskCenterInitMachine; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.CommonPlugin; +import org.example.plugin.GuardPlugin; import org.example.taskcenter.TaskCenter; import org.example.taskcenter.request.LiveReptileRequest; import org.example.thread.ChopperBotGuardianTask; @@ -25,71 +29,92 @@ import java.util.*; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; /** * 根据热门的直播以及看门狗设置来推荐热门直播间到系统 */ -public class HeatRecommendation implements ChopperBotGuardianTask { +public class HeatRecommendation extends GuardPlugin { private Map> platformFollowDogMap ; //每个平台的跟风列表 private BlockingQueue hotEventList; //用于接收每一次的热榜更新信息,并进行跟风狗推送 - - public HeatRecommendation(){ - platformFollowDogMap = new ConcurrentHashMap<>(); - hotEventList = new ArrayBlockingQueue<>(1024); - List modules = new ArrayList<>(); - - JSONArray jsonModules = (JSONArray) FileCacheManagerInstance - .getInstance() - .getFileCache(HotModuleConfig.getFullFilePath()) - .get("Module"); - - jsonModules.forEach(jsonModule->{ - modules.add(JSONObject.parseObject(jsonModule.toString(),HotModuleSetting.class)); - }); - - for (HotModuleSetting module : modules) { - if (module.isFollowDogEnable()) { - platformFollowDogMap.put(module.getPlatform(),module.getFollowDogs()); - } - } + public HeatRecommendation(String module, String pluginName, List needPlugins, boolean isAutoStart) { + super(module, pluginName, needPlugins, isAutoStart); } + @Override + public boolean init() { + try { + platformFollowDogMap = new ConcurrentHashMap<>(); + hotEventList = new ArrayBlockingQueue<>(1024); + List modules = new ArrayList<>(); - /** - * 处理阻塞队列中的热门推送提醒,并且从热点数据中心获取热门直播,进行推送 - * @throws InterruptedException - */ - private void handlerHotEvent() throws InterruptedException { - while(true){ - String platform = hotEventList.take(); - List followDogList; - ChopperLogFactory.getLogger(LoggerType.Hot).info("<{}> Hotspot event detected.", PluginName.HOT_RECOMMENDATION_PLUGIN); - if(platformFollowDogMap.containsKey(platform) - &&(followDogList=platformFollowDogMap.get(platform)).size()>0){ - //发送给爬虫队列 - for (FollowDog followDog : followDogList) { - String moduleName = followDog.getModuleName(); - List lives; - if(FollowDog.ALL_LIVES.equals(moduleName)){ - lives = HotModuleDataCenter.DataCenter().getLiveList(platform); - }else{ - lives = HotModuleDataCenter.DataCenter().getModule(platform,moduleName).getHotLives(); - } - //TODO 待完善 1,需要发送弹幕爬虫请求 2,callback更改 - for (Live live : needRecommend(lives, followDog.getBanLiver(), followDog.getTop())) { + JSONArray jsonModules = (JSONArray) FileCacheManagerInstance + .getInstance() + .getFileCache(HotModuleConfig.getFullFilePath()) + .get("Module"); - Objects.requireNonNull(TaskCenter.center()). - request( new LiveReptileRequest(live, LiveReptileRequest.LiveType.Online,(t)->{ - ChopperLogFactory.getLogger(LoggerType.Hot).info("成功推荐主播,结果为:T"); - })); + jsonModules.forEach(jsonModule->{ + modules.add(JSONObject.parseObject(jsonModule.toString(),HotModuleSetting.class)); + }); + for (HotModuleSetting module : modules) { + if (module.isFollowDogEnable()) { + platformFollowDogMap.put(module.getPlatform(),module.getFollowDogs()); + } + } + }catch (Exception e){ + return false; + } + return super.init(); + } + + @Override + public void start() { + try { + String platform = hotEventList.poll(5,TimeUnit.MILLISECONDS); + if("shutdown".equals(platform)){ + return; + } + if(platform!=null){ + List followDogList; + ChopperLogFactory.getLogger(LoggerType.Hot).info("<{}> Hotspot event detected.", PluginName.HOT_RECOMMENDATION_PLUGIN); + if(platformFollowDogMap.containsKey(platform) + &&(followDogList=platformFollowDogMap.get(platform)).size()>0){ + //发送给爬虫队列 + for (FollowDog followDog : followDogList) { + String moduleName = followDog.getModuleName(); + List lives; + if(FollowDog.ALL_LIVES.equals(moduleName)){ + lives = HotModuleDataCenter.DataCenter().getLiveList(platform); + }else{ + lives = HotModuleDataCenter.DataCenter().getModule(platform,moduleName).getHotLives(); + } + //TODO 待完善 1,需要发送弹幕爬虫请求 2,callback更改 + for (Live live : needRecommend(lives, followDog.getBanLiver(), followDog.getTop())) { + CommonPlugin plugin = InitPluginRegister.getPlugin(PluginName.TASK_CENTER_PLUGIN); + if(plugin!=null){ + ((TaskCenter)plugin).request( new LiveReptileRequest(live, LiveReptileRequest.LiveType.Online,(t)->{ + ChopperLogFactory.getLogger(LoggerType.Hot).info("成功推荐主播,结果为:T"); + })); + } + } } } } + }catch (Exception e){ + e.printStackTrace(); + } + } + + @Override + public void shutdown() { + super.shutdown(); + if(hotEventList.isEmpty()){ + hotEventList.offer("shutdown"); } } @@ -130,13 +155,6 @@ public class HeatRecommendation implements ChopperBotGuardianTask { public void sendHotEvent(String platform){ hotEventList.offer(platform); } - @Override - public void threadTask() { - try { - handlerHotEvent(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } + } diff --git a/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java b/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java index a3d1dda..bf0e06c 100644 --- a/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java @@ -1,9 +1,13 @@ package org.example.init; +import org.example.constpool.ConstPool; import org.example.constpool.PluginName; import org.example.core.recommend.HeatRecommendation; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.CommonPlugin; +import org.example.plugin.Plugin; +import org.example.taskcenter.TaskCenter; import java.util.List; @@ -11,32 +15,17 @@ import java.util.List; * @author Genius * @date 2023/07/29 14:48 **/ + +@Plugin(moduleName = ConstPool.HOT, + pluginName = PluginName.HOT_RECOMMENDATION_PLUGIN, + needPlugin = {PluginName.HOT_CONFIG_PLUGIN,PluginName.HOT_GUARD_PLUGIN}, + pluginClass= HeatRecommendation.class ) public class HeatRecommendationInitMachine extends CommonInitMachine{ - public static HeatRecommendation heatRecommendation; - public HeatRecommendationInitMachine() { - super( List.of(PluginName.HOT_CONFIG_PLUGIN,PluginName.HOT_GUARD_PLUGIN), - ChopperLogFactory.getLogger(LoggerType.Hot), - PluginName.HOT_RECOMMENDATION_PLUGIN - ); + public HeatRecommendationInitMachine(List needPlugins, boolean isAutoStart, String moduleName, String name, Class clazz) { + super(needPlugins, isAutoStart, moduleName, name, clazz); } - @Override - public boolean init() { - try { - heatRecommendation = new HeatRecommendation(); - }catch (Exception e){ - return fail(); - } - - return success(); - } - - @Override - public void afterInit() { - - heatRecommendation.guardian(); - } } diff --git a/HotModule/src/main/java/org/example/init/HotConfigInitMachine.java b/HotModule/src/main/java/org/example/init/HotConfigInitMachine.java index a280d52..60fb755 100644 --- a/HotModule/src/main/java/org/example/init/HotConfigInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HotConfigInitMachine.java @@ -6,19 +6,27 @@ package org.example.init; **/ import org.example.config.HotModuleConfig; +import org.example.constpool.ConstPool; import org.example.constpool.PluginName; +import org.example.core.recommend.HeatRecommendation; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.CommonPlugin; +import org.example.plugin.Plugin; + +import java.util.List; /** * 热门模块配置文件初始化机器 */ -public class HotConfigInitMachine extends ConfigInitMachine { +@Plugin(moduleName = ConstPool.HOT, + pluginName = PluginName.HOT_CONFIG_PLUGIN, + needPlugin = {PluginName.FILE_CACHE_PLUGIN}, + pluginClass= HotModuleConfig.class ) +public class HotConfigInitMachine extends ConfigInitMachine { - public HotConfigInitMachine() { - super(PluginName.HOT_CONFIG_PLUGIN - ,new HotModuleConfig() - ,ChopperLogFactory.getLogger(LoggerType.Hot)); + + public HotConfigInitMachine(List needPlugins, boolean isAutoStart, String moduleName, String name, Class clazz) { + super(needPlugins, isAutoStart, moduleName, name, clazz); } - } diff --git a/HotModule/src/main/java/org/example/init/HotGuardInitMachine.java b/HotModule/src/main/java/org/example/init/HotGuardInitMachine.java index ae36de3..5ed0a23 100644 --- a/HotModule/src/main/java/org/example/init/HotGuardInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HotGuardInitMachine.java @@ -6,12 +6,16 @@ import org.example.cache.FileCache; import org.example.cache.FileCacheManagerInstance; import org.example.config.HotModuleConfig; import org.example.config.HotModuleSetting; +import org.example.constpool.ConstPool; import org.example.constpool.PluginName; import org.example.core.control.HotModuleLoadTask; +import org.example.core.guard.HotModuleGuard; import org.example.core.guard.HotModuleGuardInstance; import org.example.core.guard.Guard; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.plugin.CommonPlugin; +import org.example.plugin.Plugin; import org.example.util.ClassUtil; import java.util.ArrayList; @@ -25,15 +29,16 @@ import static org.example.constpool.HotModuleConstPool.LOAD_TASK_CLASS_ROOT; * @author Genius * @date 2023/07/21 09:58 **/ + +@Plugin(moduleName = ConstPool.HOT, + pluginName = PluginName.HOT_GUARD_PLUGIN, + needPlugin = {PluginName.FILE_CACHE_PLUGIN,PluginName.HOT_CONFIG_PLUGIN}, + pluginClass= HotModuleGuard.class ) public class HotGuardInitMachine extends CommonInitMachine{ - public HotGuardInitMachine() { - super( List.of(PluginName.HOT_CONFIG_PLUGIN, - PluginName.FILE_CACHE_PLUGIN), - - ChopperLogFactory.getLogger(LoggerType.Hot), - PluginName.HOT_GUARD_PLUGIN); + public HotGuardInitMachine(List needPlugins, boolean isAutoStart, String moduleName, String name, Class clazz) { + super(needPlugins, isAutoStart, moduleName, name, clazz); } /** @@ -43,53 +48,8 @@ public class HotGuardInitMachine extends CommonInitMachine{ * 3,初始化热度监控插件 * @throws Exception */ - private void envInit() throws Exception { - FileCache HotModuleFileCache = FileCacheManagerInstance.getInstance().getFileCache(HotModuleConfig.getFullFilePath()); - List guards = new ArrayList<>(); - int guardNum = (Integer)HotModuleFileCache.get("GuardNum"); - Map map = new HashMap<>(); - JSONArray modules = (JSONArray)HotModuleFileCache.get("Module"); - for (Object module : modules) { - HotModuleSetting hotModuleSetting = JSONObject.parseObject(module.toString(), HotModuleSetting.class); - map.put(hotModuleSetting.getPlatform(),hotModuleSetting); - } - for (String clazz : ClassUtil.getClassesInPackage(LOAD_TASK_CLASS_ROOT)) { - String[] split = clazz.split("\\."); - String clazzName = split[split.length-1].toLowerCase(); - if(clazzName.endsWith("loadtask")&& clazzName.contains("hot")){ - String platformName = clazzName.split("hot")[0]; - boolean isHotModule = clazzName.contains("module"); - if(map.containsKey(platformName)){ - HotModuleSetting hotModuleSetting = map.get(platformName); - Class loadClazz = Class.forName(clazz); - if(isHotModule&&hotModuleSetting.isEnableHotModule()){ - HotModuleLoadTask task = (HotModuleLoadTask)loadClazz.getDeclaredConstructor().newInstance(); - guards.add(new Guard(logger,clazzName,task, - hotModuleSetting.getUpdateHotModuleTimes(),hotModuleSetting.getFailRetryTimes())); - }else if(hotModuleSetting.isEnableHotLive()){ - HotModuleLoadTask task = (HotModuleLoadTask)loadClazz.getDeclaredConstructor().newInstance(); - guards.add(new Guard(logger,clazzName,task, - hotModuleSetting.getUpdateHotLivesTimes(),hotModuleSetting.getFailRetryTimes())); - } - } - } - } - HotModuleGuardInstance.InitInstance(guards,guardNum); - HotModuleGuardInstance.getInstance().start(); - } - - @Override - public boolean init() { - try { - envInit(); - } catch (Exception e) { - return fail(e.getMessage()); - } - - return success(); - } @Override public void shutdown() { diff --git a/HotModule/src/main/java/org/example/init/module/HotModuleInitMachine.java b/HotModule/src/main/java/org/example/init/module/HotModuleInitMachine.java index 0816252..39e438a 100644 --- a/HotModule/src/main/java/org/example/init/module/HotModuleInitMachine.java +++ b/HotModule/src/main/java/org/example/init/module/HotModuleInitMachine.java @@ -19,16 +19,10 @@ import java.util.List; * 整个热门模块的模块初始化类 */ public class HotModuleInitMachine extends ModuleInitMachine { - public HotModuleInitMachine() { super( List.of(ConstPool.FILE,ConstPool.CREEPER), ChopperLogFactory.getLogger(LoggerType.Hot), - List.of( - new HotConfigInitMachine(), //热门模块配置文件插件 - new HotGuardInitMachine(), //平台热门直播,热门模块监控插件 - new HeatRecommendationInitMachine() //平台热门直播推送插件 - ), ConstPool.HOT ); } diff --git a/common/src/main/java/org/example/bean/ConfigFile.java b/common/src/main/java/org/example/bean/ConfigFile.java index 5a98fdc..f4373d4 100644 --- a/common/src/main/java/org/example/bean/ConfigFile.java +++ b/common/src/main/java/org/example/bean/ConfigFile.java @@ -1,7 +1,10 @@ package org.example.bean; +import org.example.plugin.CommonPlugin; + import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.List; import java.util.Map; /** @@ -10,7 +13,7 @@ import java.util.Map; **/ //配置文件的抽象类,只负责构建配置文件最基础的架构,一般不用来存放配置文件本身的内容 -public abstract class ConfigFile{ +public abstract class ConfigFile extends CommonPlugin { private FileType fileType; private String filePath; @@ -21,6 +24,19 @@ public abstract class ConfigFile{ //上一次更新时间 private LocalDateTime updateTime; + public ConfigFile() { + super(null, null, null, true); + } + + public ConfigFile(String filePath, String fileName, T data,FileType fileType) { + super(null, null, null, true); + this.fileType = fileType; + this.filePath = filePath; + this.fileName = fileName; + this.data = data; + this.updateTime = LocalDateTime.now(); + } + /** * 用于最开始创建配置文件结构的打包 * @return Map @@ -62,25 +78,17 @@ public abstract class ConfigFile{ } - public ConfigFile() { - } - - public ConfigFile(String filePath, String fileName, T data) { - this.filePath = filePath; - this.fileName = fileName; - this.data = data; - this.updateTime = LocalDateTime.now(); - this.fileType = FileType.COMMON; - } - - public ConfigFile(String filePath,String fileName,T data,FileType fileType){ - this.filePath = filePath; - this.fileName = fileName; - this.data = data; - this.updateTime = LocalDateTime.now(); + public ConfigFile(String module, String pluginName, List needPlugins, boolean isAutoStart, + String filePath, String fileName, T data,FileType fileType) { + super(module, pluginName, needPlugins, isAutoStart); this.fileType = fileType; + this.filePath = filePath; + this.fileName = fileName; + this.data = data; + this.updateTime = LocalDateTime.now(); } + public String getFilePath() { return this.filePath; } diff --git a/common/src/main/java/org/example/bean/Plugin.java b/common/src/main/java/org/example/bean/Plugin.java deleted file mode 100644 index a6fd97e..0000000 --- a/common/src/main/java/org/example/bean/Plugin.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.example.bean; - -import lombok.AllArgsConstructor; -import lombok.Data; -import org.example.init.InitMachine; - -import java.util.List; - -/** - * @author Genius - * @date 2023/07/31 20:14 - **/ -@Data -@AllArgsConstructor -public class Plugin{ - private InitMachine initMachine; - private String moduleName; - private String pluginName; - private List needPlugin; - private boolean enable; -} diff --git a/common/src/main/java/org/example/constpool/PluginName.java b/common/src/main/java/org/example/constpool/PluginName.java index db6eddb..ecc23a2 100644 --- a/common/src/main/java/org/example/constpool/PluginName.java +++ b/common/src/main/java/org/example/constpool/PluginName.java @@ -11,7 +11,7 @@ package org.example.constpool; public class PluginName { //File - public static String MODULE_CONFIG_PLUGIN = "ModuleConfig"; + public static final String MODULE_CONFIG_PLUGIN = "ModuleConfig"; public static final String FILE_CACHE_PLUGIN = "FileCache"; //Creeper @@ -19,7 +19,7 @@ public class PluginName { public static final String TASK_CENTER_PLUGIN = "TaskCenter"; //Hot - public static String HOT_CONFIG_PLUGIN = "HotConfig"; - public static String HOT_GUARD_PLUGIN = "HotGuard"; - public static String HOT_RECOMMENDATION_PLUGIN = "HotRecommendation"; + public static final String HOT_CONFIG_PLUGIN = "HotConfig"; + public static final String HOT_GUARD_PLUGIN = "HotGuard"; + public static final String HOT_RECOMMENDATION_PLUGIN = "HotRecommendation"; } diff --git a/common/src/main/java/org/example/init/CommonInitMachine.java b/common/src/main/java/org/example/init/CommonInitMachine.java index 81aa607..92e826e 100644 --- a/common/src/main/java/org/example/init/CommonInitMachine.java +++ b/common/src/main/java/org/example/init/CommonInitMachine.java @@ -5,31 +5,59 @@ package org.example.init; * @date 2023/07/21 00:57 **/ +import org.example.log.ResultLogger; +import org.example.plugin.CommonPlugin; +import org.example.plugin.Plugin; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; /** * 模块中的小模块初始化抽象类 */ -public abstract class CommonInitMachine implements ComponentInitMachine{ +public abstract class CommonInitMachine implements ComponentInitMachine, ResultLogger { - protected List needPlugins; //初始化时需要的插件 + protected String moduleName; //模块名称 - protected String pluginName; + protected String pluginName; //插件名称 + protected List needPlugins = new ArrayList<>(); //初始化时需要的插件 + protected boolean isAutoStart; //是否自动启动 - protected Logger logger; + protected Class pluginClass; //插件类型 + + protected Logger logger; //日志 + + protected CommonPlugin plugin; //插件类 + public CommonInitMachine(List needPlugins, boolean isAutoStart, String moduleName, String name,Class clazz) { + this.needPlugins = needPlugins; + this.isAutoStart = isAutoStart; + this.moduleName = moduleName; + pluginName = name; + pluginClass =clazz; + } + public CommonInitMachine(String moduleName, Logger logger) { + this.moduleName = moduleName; + this.logger = logger; + this.needPlugins = new ArrayList<>(); + } + + public CommonInitMachine(String moduleName, List needPlugins, Logger logger) { + this.moduleName = moduleName; + this.needPlugins = needPlugins; + this.logger = logger; + } /** * 注册插件 */ public void registerPlugin(){ - InitPluginRegister.registerPluginTable.put(pluginName,this.getClass()); + InitPluginRegister.registerPluginTable.put(pluginName,this); } /** @@ -47,58 +75,49 @@ public abstract class CommonInitMachine implements ComponentInitMachine{ return true; } - - public CommonInitMachine(List needPlugins, Logger logger, String pluginName) { - this.needPlugins = needPlugins; - this.logger = logger; - this.pluginName = pluginName; + @Override + public boolean init() { + Plugin ano = this.getClass().getAnnotation(Plugin.class); + try { + plugin = ano.pluginClass() + .getDeclaredConstructor(String.class,String.class,List.class,boolean.class) + .newInstance(moduleName,pluginName,needPlugins,isAutoStart); + if (plugin.init()) { + return success(); + }else{ + return fail(); + } + }catch (Exception e){ + return fail(e.getMessage()); + } } - public CommonInitMachine(Logger logger,String pluginName){ - needPlugins = new ArrayList<>(); - this.logger = logger; - this.pluginName = pluginName; - } - - public CommonInitMachine(String pluginName){ - this.pluginName = pluginName; - needPlugins = new ArrayList<>(); - this.logger = LoggerFactory.getLogger(pluginName); - } - - @Override public void successLog() { successLog(String.format("[✔] {%s} init success!",pluginName)); } - @Override public void successLog(String str) { logger.info(str); } - @Override public void failLog() { failLog(String.format("[❌] {%s} init error!",pluginName)); } - @Override public void failLog(String str) { logger.error(str); } - @Override public boolean fail(String failCause) { failLog(String.format("[❌] {%s} init error! Execption:{%s}",pluginName,failCause)); return false; } - @Override public boolean success() { successLog(); return true; } - public boolean success(String str){ successLog(str); return true; @@ -106,6 +125,7 @@ public abstract class CommonInitMachine implements ComponentInitMachine{ @Override public void shutdown(){ + plugin.shutdown(); shutdownLog(); } @@ -115,10 +135,32 @@ public abstract class CommonInitMachine implements ComponentInitMachine{ @Override public void afterInit() { + if(plugin!=null){ + plugin.afterInit(); + } } public List getNeedPlugins() { return needPlugins; } + public CommonPlugin getPlugin() { + return plugin; + } + public boolean isAutoStart() { + return isAutoStart; + } + public String getModuleName() { + return moduleName; + } + public String getPluginName() { + return pluginName; + } + public void setLogger(Logger logger) { + this.logger = logger; + } + + public Class getPluginClass() { + return pluginClass; + } } diff --git a/common/src/main/java/org/example/init/InitMachine.java b/common/src/main/java/org/example/init/InitMachine.java index a53e77a..9e89439 100644 --- a/common/src/main/java/org/example/init/InitMachine.java +++ b/common/src/main/java/org/example/init/InitMachine.java @@ -1,15 +1,13 @@ package org.example.init; -import org.example.log.ResultLogger; - /** * @author Genius * @date 2023/04/20 19:36 **/ //模块初始化接口 -public interface InitMachine extends ResultLogger { +public interface InitMachine { boolean init(); diff --git a/common/src/main/java/org/example/init/InitPluginRegister.java b/common/src/main/java/org/example/init/InitPluginRegister.java index d1985d2..ffe9b98 100644 --- a/common/src/main/java/org/example/init/InitPluginRegister.java +++ b/common/src/main/java/org/example/init/InitPluginRegister.java @@ -5,11 +5,17 @@ package org.example.init; * @date 2023/07/29 15:21 **/ -import lombok.AllArgsConstructor; -import lombok.Data; -import org.example.bean.Plugin; +import org.example.constpool.ConstPool; +import org.example.plugin.CommonPlugin; +import org.example.plugin.Plugin; +import org.example.util.ClassUtil; +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** @@ -17,12 +23,115 @@ import java.util.concurrent.ConcurrentHashMap; */ public class InitPluginRegister { - public static ConcurrentHashMap allPlugins = new ConcurrentHashMap<>(); - public static ConcurrentHashMap> registerPluginTable = new ConcurrentHashMap<>(); + public static ConcurrentHashMap> modulePlugin = new ConcurrentHashMap<>(); + + public static ConcurrentHashMap> fatherAndSonPlugin = new ConcurrentHashMap<>(); + + public static ConcurrentHashMap> moduleFatherAndSonPlugin = new ConcurrentHashMap<>(); + + public static ConcurrentHashMap allPlugins = new ConcurrentHashMap<>(); + public static ConcurrentHashMap registerPluginTable = new ConcurrentHashMap<>(); + + public static Map pluginSetting; + + public static boolean initPluginRegister(){ + Set> initMachineClasses = ClassUtil.getAnnotationClass(ConstPool.PROJECT_PATH + ".init", Plugin.class); + for (Class initMachineClass : initMachineClasses) { + try { + Plugin ano = initMachineClass.getAnnotation(Plugin.class); + String moduleName = ano.moduleName(); + String pluginName = ano.pluginName(); + List needPlugins = List.of(ano.needPlugin()); + boolean autoStart = pluginSetting.containsKey(pluginName)?pluginSetting.get(pluginName):ano.autoStart(); + CommonInitMachine initMachine = (CommonInitMachine) initMachineClass + .getDeclaredConstructor(List.class,boolean.class,String.class,String.class,Class.class) + .newInstance(needPlugins,autoStart,moduleName,pluginName,ano.pluginClass()); + + if(allPlugins.containsKey(pluginName))return false; + allPlugins.put(pluginName,initMachine); + + if(modulePlugin.containsKey(moduleName)){ + modulePlugin.get(moduleName).add(pluginName); + }else{ + ArrayList list = new ArrayList<>(); + list.add(pluginName); + modulePlugin.put(moduleName,list); + } + if(!fatherAndSonPlugin.containsKey(pluginName)){ + fatherAndSonPlugin.put(pluginName,new ArrayList<>()); + } + for (String father : needPlugins) { + if(fatherAndSonPlugin.containsKey(father)){ + fatherAndSonPlugin.get(father).add(pluginName); + }else{ + ArrayList list = new ArrayList<>(); + list.add(pluginName); + fatherAndSonPlugin.put(father,list); + } + } + } catch (Exception e) { + return false; + } + } + try { + allPlugins.forEach( + (k,v)->{ + List list = fatherAndSonPlugin.get(k); + if(list!=null){ + List newList = new ArrayList<>(); + String moduleName = v.getModuleName(); + List modulePlugins = modulePlugin.get(moduleName); + if(modulePlugins!=null){ + for (String s : list) { + if(modulePlugins.contains(s)){ + newList.add(s); + } + } + moduleFatherAndSonPlugin.put(k,newList); + } + } + + } + ); + }catch (Exception e){ + e.printStackTrace(); + } + + return true; + } + public static boolean isRegister(String pluginName){ return registerPluginTable.containsKey(pluginName); } + public static boolean closePlugin(String pluginName){ + synchronized (pluginName) { + if (registerPluginTable.containsKey(pluginName)) { + for (String plugin : fatherAndSonPlugin.get(pluginName)) { + if (registerPluginTable.containsKey(plugin)) { + return false; + } + } + registerPluginTable.get(pluginName).shutdown(); + registerPluginTable.remove(pluginName); + } + return true; + } + } + + public static T getPlugin(String pluginName,Class plugin){ + if(registerPluginTable.containsKey(pluginName)){ + return (T)registerPluginTable.get(pluginName).getPlugin(); + } + return null; + } + + public static CommonPlugin getPlugin(String pluginName){ + if(registerPluginTable.containsKey(pluginName)){ + return registerPluginTable.get(pluginName).getPlugin(); + } + return null; + } } diff --git a/common/src/main/java/org/example/init/ModuleInitMachine.java b/common/src/main/java/org/example/init/ModuleInitMachine.java index b5ff94d..117fe08 100644 --- a/common/src/main/java/org/example/init/ModuleInitMachine.java +++ b/common/src/main/java/org/example/init/ModuleInitMachine.java @@ -1,7 +1,9 @@ package org.example.init; +import org.example.util.PluginUtil; import org.slf4j.Logger; +import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; @@ -15,7 +17,7 @@ import java.util.function.Supplier; */ public abstract class ModuleInitMachine extends CommonInitMachine{ - private List initMachines; + protected List initMachines = new ArrayList<>(); private String moduleName; @@ -31,22 +33,33 @@ public abstract class ModuleInitMachine extends CommonInitMachine{ return true; } + @Override + public void registerPlugin() { + InitPluginRegister.registerPluginTable.put(moduleName,this); + } + @Override public boolean init() { + try { + initMachines = PluginUtil.getModuleAllPluginInit(moduleName); + } catch (Exception e) { + return fail(e.getMessage()); + } + return initLogger(()->{ if(checkNeedPlugin()){ - for (InitMachine initMachine : this.getInitMachines()) { - if (((CommonInitMachine)initMachine).checkNeedPlugin()) { - if(!initMachine.init()){ - return fail(); + for (CommonInitMachine initMachine : initMachines) { + if (initMachine.checkNeedPlugin()) { + initMachine.setLogger(logger); + if(!initMachine.init()){ + return fail(); + } + initMachine.registerPlugin(); + }else{ + return false; } - ((CommonInitMachine) initMachine).registerPlugin(); - }else{ - return false; } - } - registerPlugin(); - return success(); + return success(); } return false; @@ -62,33 +75,22 @@ public abstract class ModuleInitMachine extends CommonInitMachine{ } } - public ModuleInitMachine(List initMachines, String moduleName) { - super(moduleName); - this.initMachines = initMachines; + + public ModuleInitMachine(String moduleName,Logger logger){ + super(moduleName,logger); this.moduleName = moduleName; } - public ModuleInitMachine(List initMachines,String moduleName,Logger logger){ - super(logger,moduleName); - this.initMachines = initMachines; + public ModuleInitMachine(List needPlugins, Logger logger, String moduleName) { + super(moduleName,needPlugins,logger); this.moduleName = moduleName; } - public ModuleInitMachine(List needPlugins, Logger logger, List initMachines, String moduleName) { - super(needPlugins, logger, moduleName); - this.initMachines = initMachines; - this.moduleName = moduleName; - } - public List getInitMachines() { + public List getInitMachines() { return initMachines; } - public void setInitMachines(List initMachines) { - this.initMachines = initMachines; - } - - public String getModuleName() { return moduleName; } diff --git a/common/src/main/java/org/example/log/ChopperLogFactory.java b/common/src/main/java/org/example/log/ChopperLogFactory.java index e74cc37..eb4b94f 100644 --- a/common/src/main/java/org/example/log/ChopperLogFactory.java +++ b/common/src/main/java/org/example/log/ChopperLogFactory.java @@ -1,14 +1,24 @@ package org.example.log; +import lombok.extern.java.Log; +import org.example.constpool.ConstPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Map; + /** * @author Genius * @date 2023/07/28 23:22 **/ public class ChopperLogFactory { + public static Map nameToType = Map.of( + ConstPool.HOT,LoggerType.Hot, + ConstPool.CREEPER,LoggerType.Creeper, + ConstPool.FILE, LoggerType.File + ); + public static Logger getLogger(LoggerType loggerType){ return getLogger(loggerType.getLoggerName()); } diff --git a/common/src/main/java/org/example/plugin/ChopperBotPlugin.java b/common/src/main/java/org/example/plugin/ChopperBotPlugin.java new file mode 100644 index 0000000..135d85d --- /dev/null +++ b/common/src/main/java/org/example/plugin/ChopperBotPlugin.java @@ -0,0 +1,6 @@ +package org.example.plugin; + +import org.example.init.InitMachine; + +public interface ChopperBotPlugin extends InitMachine { +} diff --git a/common/src/main/java/org/example/plugin/CommonPlugin.java b/common/src/main/java/org/example/plugin/CommonPlugin.java new file mode 100644 index 0000000..7fbe28a --- /dev/null +++ b/common/src/main/java/org/example/plugin/CommonPlugin.java @@ -0,0 +1,71 @@ +package org.example.plugin; + +import java.util.List; + +/** + * @author Genius + * @date 2023/07/31 22:55 + **/ +public abstract class CommonPlugin implements ChopperBotPlugin{ + private String module; + private String pluginName; + private List needPlugins; + + private boolean isAutoStart; + + @Override + public boolean init() { + return false; + } + + @Override + public void afterInit() { + + } + + @Override + public void shutdown() { + + } + + public CommonPlugin(String module, String pluginName, List needPlugins, boolean isAutoStart) { + this.module = module; + this.pluginName = pluginName; + this.needPlugins = needPlugins; + this.isAutoStart = isAutoStart; + } + + public String getModule() { + return module; + } + + public void setModule(String module) { + this.module = module; + } + + public String getPluginName() { + return pluginName; + } + + public void setPluginName(String pluginName) { + this.pluginName = pluginName; + } + + public List getNeedPlugins() { + return needPlugins; + } + + public void setNeedPlugins(List needPlugins) { + this.needPlugins = needPlugins; + } + + public boolean isAutoStart() { + return isAutoStart; + } + + public void setAutoStart(boolean autoStart) { + isAutoStart = autoStart; + } + + +} diff --git a/common/src/main/java/org/example/plugin/GuardPlugin.java b/common/src/main/java/org/example/plugin/GuardPlugin.java new file mode 100644 index 0000000..9136fd6 --- /dev/null +++ b/common/src/main/java/org/example/plugin/GuardPlugin.java @@ -0,0 +1,45 @@ +package org.example.plugin; + +import org.example.thread.ChopperBotGuardianTask; + +import java.util.List; + +/** + * @author Genius + * @date 2023/07/31 22:55 + **/ +public abstract class GuardPlugin extends CommonPlugin implements ChopperBotGuardianTask { + + private volatile boolean isStop = false; + + protected boolean afterDo = true; + + public GuardPlugin(String module, String pluginName, List needPlugins, boolean isAutoStart) { + super(module, pluginName, needPlugins, isAutoStart); + } + + public abstract void start(); + + @Override + public boolean init() { + if(!afterDo) this.guardian(); + return true; + } + + @Override + public void threadTask() { + while(!isStop){ + start(); + } + } + + @Override + public void shutdown() { + isStop=true; + } + + @Override + public void afterInit() { + if(afterDo) this.guardian(); + } +} diff --git a/common/src/main/java/org/example/plugin/Plugin.java b/common/src/main/java/org/example/plugin/Plugin.java index f5ea451..de5faec 100644 --- a/common/src/main/java/org/example/plugin/Plugin.java +++ b/common/src/main/java/org/example/plugin/Plugin.java @@ -13,5 +13,7 @@ public @interface Plugin { String pluginName(); String[] needPlugin() default {}; + Class pluginClass(); + boolean autoStart() default true; } diff --git a/common/src/main/java/org/example/util/PluginUtil.java b/common/src/main/java/org/example/util/PluginUtil.java index 35e6b36..793f9e1 100644 --- a/common/src/main/java/org/example/util/PluginUtil.java +++ b/common/src/main/java/org/example/util/PluginUtil.java @@ -3,7 +3,9 @@ package org.example.util; import lombok.AllArgsConstructor; import lombok.Data; import org.example.exception.InitException; +import org.example.init.CommonInitMachine; import org.example.init.InitMachine; +import org.example.init.InitPluginRegister; import org.example.init.ModuleInitMachine; import java.util.*; @@ -30,20 +32,77 @@ public class PluginUtil { } } - public static List getAllModuleInit() throws Exception { + public static List getAllModuleInit() throws Exception { List classes = ClassUtil.getClassesInPackage("org.example.init.module"); - List list = new ArrayList<>(); + List list = new ArrayList<>(); for (String aClass : classes) { - list.add((InitMachine) Class.forName(aClass).getDeclaredConstructor().newInstance()); + list.add((CommonInitMachine) Class.forName(aClass).getDeclaredConstructor().newInstance()); } return sortModuleInit(list); - } - private static List sortModuleInit(List list){ - Map needPlugins = new HashMap<>(); - Map NameToInitMachine = new HashMap<>(); - for (InitMachine initMachine : list) { + public static List getModuleAllPluginInit(String moduleName) throws Exception{ + List list = InitPluginRegister.modulePlugin.get(moduleName); + List commonInitMachines = new ArrayList<>(); + for (String name : list) { + commonInitMachines.add(InitPluginRegister.allPlugins.get(name)); + } + return sortModuleAllPluginInit(commonInitMachines); + } + + private static List sortModuleAllPluginInit(List list){ + Map needPlugins = new HashMap<>(); + Map NameToInitMachine = new HashMap<>(); + for (CommonInitMachine initMachine : list) { + String moduleName = initMachine.getPluginName(); + if(!NameToInitMachine.containsKey(moduleName)){ + needPlugins.put(initMachine,0); + NameToInitMachine.put(moduleName,initMachine); + }else{ + throw new InitException("Found same module name!"); + } + } + + for(CommonInitMachine initMachine:list){ + List needPluginsList = InitPluginRegister.moduleFatherAndSonPlugin.get(initMachine.getPluginName()); + needPluginsList.forEach( + plugin->{ + CommonInitMachine obj = NameToInitMachine.get(plugin); + needPlugins.put(obj,needPlugins.get(obj)+1); + } + ); + } + List machines = new ArrayList<>(); + int n = needPlugins.size(); + + while(machines.size(){ + if(v==0){ + machines.add(k); + InitPluginRegister.moduleFatherAndSonPlugin.get(k.getPluginName()).forEach( + h->{ + CommonInitMachine machine = NameToInitMachine.get(h); + needPlugins.put(machine,needPlugins.get(machine)-1); + } + ); + needPlugins.put(k,v-1); + isLoop.set(false); + } + } + ); + if(isLoop.get()){ + throw new InitException("Found loop depend on"); + } + } + return machines; + } + + private static List sortModuleInit(List list){ + Map needPlugins = new HashMap<>(); + Map NameToInitMachine = new HashMap<>(); + for (CommonInitMachine initMachine : list) { String moduleName = ((ModuleInitMachine) initMachine).getModuleName(); if(!NameToInitMachine.containsKey(moduleName)){ needPlugins.put(initMachine,0); @@ -53,16 +112,16 @@ public class PluginUtil { } } - for(InitMachine initMachine:list){ + for(CommonInitMachine initMachine:list){ List needPluginsList = ((ModuleInitMachine) initMachine).getNeedPlugins(); needPluginsList.forEach( plugin->{ - InitMachine obj = NameToInitMachine.get(plugin); + CommonInitMachine obj = NameToInitMachine.get(plugin); needPlugins.put(obj,needPlugins.get(obj)+1); } ); } - List machines = new ArrayList<>(); + List machines = new ArrayList<>(); int n = needPlugins.size(); while(machines.size(){ - InitMachine machine = NameToInitMachine.get(h); + CommonInitMachine machine = NameToInitMachine.get(h); needPlugins.put(machine,needPlugins.get(machine)-1); } ); diff --git a/console/src/main/java/org/example/init/ChopperBotConfigFileInitMachine.java b/console/src/main/java/org/example/init/ChopperBotConfigFileInitMachine.java index 2816610..e44f726 100644 --- a/console/src/main/java/org/example/init/ChopperBotConfigFileInitMachine.java +++ b/console/src/main/java/org/example/init/ChopperBotConfigFileInitMachine.java @@ -30,8 +30,7 @@ public class ChopperBotConfigFileInitMachine extends CommonInitMachine { private boolean initFlag; public ChopperBotConfigFileInitMachine() { - super( ChopperLogFactory.getLogger(LoggerType.System), - PluginName.MODULE_CONFIG_PLUGIN); + super( PluginName.MODULE_CONFIG_PLUGIN,ChopperLogFactory.getLogger(LoggerType.System)); moduleSrcConfigFile = new ModuleSrcConfigFile(); initFlag = true; } @@ -79,7 +78,8 @@ public class ChopperBotConfigFileInitMachine extends CommonInitMachine { moduleSrcConfigFile.setData(data); GlobalFileCache.ModuleSrcConfigFile = new FileCache(moduleSrcConfigFile); } - InitWorld.pluginSetting = JSONObject.parseObject(GlobalFileCache.ModuleSrcConfigFile.get("plugin").toString(),Map.class); + InitPluginRegister.pluginSetting = JSONObject.parseObject(GlobalFileCache.ModuleSrcConfigFile.get("plugin").toString(),Map.class); + InitPluginRegister.allPlugins.put(PluginName.MODULE_CONFIG_PLUGIN,this); }catch (Exception e) { return false; } diff --git a/console/src/main/java/org/example/init/InitWorld.java b/console/src/main/java/org/example/init/InitWorld.java index 2cada1e..87dfcd3 100644 --- a/console/src/main/java/org/example/init/InitWorld.java +++ b/console/src/main/java/org/example/init/InitWorld.java @@ -26,7 +26,7 @@ public class InitWorld { @Autowired ChopperBotConfigFileInitMachine moduleSrcConfigFileInitMachine; - public static Map pluginSetting; + private ConfigurableApplicationContext ctx; @@ -37,6 +37,9 @@ public class InitWorld { @PostConstruct private void init(){ + + InitPluginRegister.initPluginRegister(); + if(moduleSrcConfigFileInitMachine.isInitFlag()){ WorldInitMachine world = null; try { diff --git a/console/src/main/java/org/example/init/WorldInitMachine.java b/console/src/main/java/org/example/init/WorldInitMachine.java index aeaf66d..a09cc43 100644 --- a/console/src/main/java/org/example/init/WorldInitMachine.java +++ b/console/src/main/java/org/example/init/WorldInitMachine.java @@ -6,6 +6,8 @@ import org.example.thread.ChopperBotGuardPool; import org.example.thread.oddjob.OddJobBoy; import org.example.util.PluginUtil; +import java.util.ArrayList; +import java.util.List; import java.util.function.Supplier; /** @@ -20,7 +22,7 @@ public class WorldInitMachine extends ModuleInitMachine{ private static final String githubUrl = "https://github.com/969025903/ChopperBot"; public WorldInitMachine() throws Exception { - super(PluginUtil.getAllModuleInit(), "ChopperBot", ChopperLogFactory.getLogger(LoggerType.System)); + super("ChopperBot",ChopperLogFactory.getLogger(LoggerType.System)); } @@ -29,7 +31,30 @@ public class WorldInitMachine extends ModuleInitMachine{ public boolean init() { ChopperBotGuardPool.init(); OddJobBoy.Boy().guardian(); - return super.init(); + try { + initMachines = PluginUtil.getAllModuleInit(); + return initLogger(()->{ + if(checkNeedPlugin()){ + for (CommonInitMachine initMachine : initMachines) { + if ((initMachine).checkNeedPlugin()) { + if(!initMachine.init()){ + return fail(); + } + (initMachine).registerPlugin(); + }else{ + return false; + } + } + registerPlugin(); + return success(); + + } + return false; + + }); + } catch (Exception e) { + throw new RuntimeException(e); + } } @Override @@ -52,7 +77,7 @@ public class WorldInitMachine extends ModuleInitMachine{ @Override protected boolean initLogger(Supplier init) { - logger.info("🌏 <{}> Wake up,Find {} module need to init,please wait.....","ChopperBot",getInitMachines().size()); + logger.info("🌏 <{}> Wake up,Find {} module need to init,please wait.....","ChopperBot",initMachines); return init.get(); }