diff --git a/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java b/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java index 613db97..a4567d7 100644 --- a/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java +++ b/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java @@ -2,6 +2,7 @@ package org.example.init; import org.example.config.CreeperConfigFile; import org.example.constpool.CreeperModuleConstPool; +import org.example.constpool.PluginName; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; import org.example.util.FileUtil; @@ -19,7 +20,10 @@ import java.nio.file.Paths; public class CreeperConfigInitMachine extends ConfigInitMachine { public CreeperConfigInitMachine() { - super(new CreeperConfigFile(), ChopperLogFactory.getLogger(LoggerType.Creeper)); + super(PluginName.CREEPER_CONFIG_PLUGIN, + new CreeperConfigFile(), + ChopperLogFactory.getLogger(LoggerType.Creeper) + ); } @Override diff --git a/CreeperModule/src/main/java/org/example/init/CreeperModuleInitMachine.java b/CreeperModule/src/main/java/org/example/init/CreeperModuleInitMachine.java index ff3362a..7a442b4 100644 --- a/CreeperModule/src/main/java/org/example/init/CreeperModuleInitMachine.java +++ b/CreeperModule/src/main/java/org/example/init/CreeperModuleInitMachine.java @@ -7,6 +7,9 @@ import org.slf4j.LoggerFactory; import java.util.List; +import static org.example.constpool.ConstPool.CREEPER; +import static org.example.constpool.ConstPool.FILE; + /** * @author Genius * @date 2023/07/29 01:58 @@ -14,10 +17,12 @@ import java.util.List; public class CreeperModuleInitMachine extends ModuleInitMachine{ public CreeperModuleInitMachine() { - super(List.of( + super(List.of(FILE), + ChopperLogFactory.getLogger(LoggerType.Creeper), + List.of( new CreeperConfigInitMachine(), - new TaskCenterInitMachine() - ), "Creeper", ChopperLogFactory.getLogger(LoggerType.Creeper)); + new TaskCenterInitMachine()), + CREEPER); } diff --git a/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java b/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java index 52ff58e..018beb4 100644 --- a/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java +++ b/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java @@ -1,8 +1,13 @@ package org.example.init; +import org.example.constpool.PluginName; +import org.example.log.ChopperLogFactory; +import org.example.log.LoggerType; import org.example.taskcenter.TaskCenter; import org.example.thread.oddjob.OddJobBoy; +import org.slf4j.Logger; +import java.util.List; import java.util.Objects; /** @@ -11,20 +16,28 @@ import java.util.Objects; **/ 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 false; + if(checkNeedPlugin()){ + try { + TaskCenter center = TaskCenter.center(); + if(center==null){ + return fail(); + } + center.guardian(); + }catch (Exception e){ + return fail(); } - OddJobBoy.Boy().addWork( - center::work - ); - }catch (InterruptedException e){ - return false; + registerPlugin(); + return success(); } - return true; + return false; } @Override @@ -38,6 +51,6 @@ public class TaskCenterInitMachine extends CommonInitMachine{ @Override public void afterInit() { //爬虫任务恢复 - Objects.requireNonNull(TaskCenter.center()).restoreTaskCenter(); + TaskCenter.center().restoreTaskCenter(); } } diff --git a/CreeperModule/src/main/java/org/example/taskcenter/TaskCenter.java b/CreeperModule/src/main/java/org/example/taskcenter/TaskCenter.java index 03ca13b..1fd4ed4 100644 --- a/CreeperModule/src/main/java/org/example/taskcenter/TaskCenter.java +++ b/CreeperModule/src/main/java/org/example/taskcenter/TaskCenter.java @@ -8,12 +8,14 @@ import org.example.cache.FileCacheManagerInstance; import org.example.config.CreeperConfigFile; import org.example.config.CreeperLogConfigFile; import org.example.config.TaskCenterConfig; +import org.example.constpool.PluginName; import org.example.exception.FileCacheException; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; import org.example.taskcenter.handler.BootStrapTaskHandler; import org.example.taskcenter.request.ReptileRequest; import org.example.taskcenter.task.ReptileTask; +import org.example.thread.ChopperBotGuardianTask; import org.example.util.ConfigFileUtil; import org.example.util.FileUtil; import org.example.util.JsonFileUtil; @@ -34,7 +36,7 @@ import java.util.concurrent.locks.ReentrantLock; * 2,记录正在运行的爬虫任务 * 3,未完成的爬虫任务修复 */ -public class TaskCenter { +public class TaskCenter implements ChopperBotGuardianTask { private long waitingQueueTime; //等待队列时间 @@ -149,13 +151,15 @@ public class TaskCenter { CreeperLogConfigFile configFile = new CreeperLogConfigFile(new ArrayList<>()); String oldFilePath = creeperLogFileCache.getFullFilePath(); if (newLogFile(configFile)) { - FileCacheManagerInstance.getInstance().deleteFileCache(oldFilePath); + if(!oldFilePath.equals(creeperLogFileCache.getFullFilePath())){ + FileCacheManagerInstance.getInstance().deleteFileCache(oldFilePath); + } } } public boolean newLogFile(CreeperLogConfigFile configFile){ if (ConfigFileUtil.createConfigFile(CreeperLogConfigFile.getFullFilePath(),configFile)) { - creeperLogFileCache = FileCacheManagerInstance.getInstance().getFileCache(CreeperConfigFile.getFullFilePath()); + creeperLogFileCache = FileCacheManagerInstance.getInstance().getFileCache(CreeperLogConfigFile.getFullFilePath()); return true; } return false; @@ -165,6 +169,8 @@ public class TaskCenter { * 恢复当日因异常关闭而没有完成的任务 */ public void restoreTaskCenter(){ + ChopperLogFactory.getLogger(LoggerType.Creeper). + info("<{}> start to restore...", PluginName.TASK_CENTER_PLUGIN); JSONArray tasks = (JSONArray)creeperLogFileCache.get("task"); int restoreNum = 0; if(tasks.size()>0){ @@ -182,7 +188,8 @@ public class TaskCenter { } } } - ChopperLogFactory.getLogger(LoggerType.Creeper).info(" Find {} reptile task need restore,already insert waiting queue",restoreNum); + ChopperLogFactory.getLogger(LoggerType.Creeper).info("<{}> Find {} reptile task need restore,already insert waiting queue", + PluginName.TASK_CENTER_PLUGIN,restoreNum); } public boolean shutdown(){ @@ -197,4 +204,8 @@ public class TaskCenter { } } + @Override + public void threadTask() { + this.work(); + } } diff --git a/FileModule/src/main/java/org/example/cache/FileCache.java b/FileModule/src/main/java/org/example/cache/FileCache.java index a7b3e02..e01c20a 100644 --- a/FileModule/src/main/java/org/example/cache/FileCache.java +++ b/FileModule/src/main/java/org/example/cache/FileCache.java @@ -259,7 +259,6 @@ public class FileCache { String dir = getFullFilePath(); configFile.onlyUpdateTime(take); File file = JsonFileUtil.writeJsonFile(dir, take); - logger.debug("正在写入{}新版本",dir); return Objects.isNull(file); } diff --git a/FileModule/src/main/java/org/example/init/ConfigInitMachine.java b/FileModule/src/main/java/org/example/init/ConfigInitMachine.java index c878813..c35a7ee 100644 --- a/FileModule/src/main/java/org/example/init/ConfigInitMachine.java +++ b/FileModule/src/main/java/org/example/init/ConfigInitMachine.java @@ -22,8 +22,8 @@ public abstract class ConfigInitMachine extends CommonInit private String filePath; - public ConfigInitMachine(T configFile, Logger logger) { - super(logger); + public ConfigInitMachine(String pluginName,T configFile, Logger logger) { + super(logger,pluginName); this.configFile = configFile; filePath = Paths.get(configFile.getFilePath(), configFile.getFileName()).toString(); } @@ -32,7 +32,11 @@ public abstract class ConfigInitMachine extends CommonInit @Override public boolean init() { - return ConfigFileUtil.createConfigFile(filePath,configFile,logger,this.getClass().getName()); + if (ConfigFileUtil.createConfigFile(filePath,configFile,logger,pluginName)) { + registerPlugin(); + return true; + } + return false; } diff --git a/FileModule/src/main/java/org/example/init/FileCacheManagerInit.java b/FileModule/src/main/java/org/example/init/FileCacheManagerInit.java index cb87db5..3248ff4 100644 --- a/FileModule/src/main/java/org/example/init/FileCacheManagerInit.java +++ b/FileModule/src/main/java/org/example/init/FileCacheManagerInit.java @@ -1,6 +1,7 @@ package org.example.init; import org.example.cache.FileCacheManagerInstance; +import org.example.constpool.PluginName; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; @@ -11,13 +12,14 @@ import org.example.log.LoggerType; public class FileCacheManagerInit extends CommonInitMachine{ public FileCacheManagerInit() { - super( ChopperLogFactory.getLogger(LoggerType.File)); + super(ChopperLogFactory.getLogger(LoggerType.File), PluginName.FILE_CACHE_PLUGIN); } @Override public boolean init() { try { FileCacheManagerInstance.getInstance().start(); + registerPlugin(); return success(); }catch (Exception e){ return fail(e.getMessage()); diff --git a/FileModule/src/main/java/org/example/init/FileModuleInitMachine.java b/FileModule/src/main/java/org/example/init/FileModuleInitMachine.java index 1ce9fcc..b95bf39 100644 --- a/FileModule/src/main/java/org/example/init/FileModuleInitMachine.java +++ b/FileModule/src/main/java/org/example/init/FileModuleInitMachine.java @@ -1,5 +1,6 @@ package org.example.init; +import org.example.constpool.ConstPool; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; @@ -15,7 +16,7 @@ public class FileModuleInitMachine extends ModuleInitMachine{ super(List.of( new ModuleSrcConfigFileInit(), new FileCacheManagerInit() - ), "FileModule", ChopperLogFactory.getLogger(LoggerType.File)); + ), ConstPool.FILE, ChopperLogFactory.getLogger(LoggerType.File)); } } diff --git a/FileModule/src/main/java/org/example/init/ModuleSrcConfigFileInit.java b/FileModule/src/main/java/org/example/init/ModuleSrcConfigFileInit.java index 33a68f6..a9f1963 100644 --- a/FileModule/src/main/java/org/example/init/ModuleSrcConfigFileInit.java +++ b/FileModule/src/main/java/org/example/init/ModuleSrcConfigFileInit.java @@ -1,5 +1,6 @@ package org.example.init; +import org.example.constpool.PluginName; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; import org.example.pojo.configfile.ModuleSrcConfigFile; @@ -21,7 +22,8 @@ public class ModuleSrcConfigFileInit extends CommonInitMachine { ModuleSrcConfigFile moduleSrcConfigFile; public ModuleSrcConfigFileInit() { - super( ChopperLogFactory.getLogger(LoggerType.File)); + super( ChopperLogFactory.getLogger(LoggerType.File), + PluginName.MODULE_CONFIG_PLUGIN); moduleSrcConfigFile = new ModuleSrcConfigFile(); } diff --git a/HotModule/src/main/java/org/example/core/HotModuleDataCenter.java b/HotModule/src/main/java/org/example/core/HotModuleDataCenter.java index fe6c208..0dfad09 100644 --- a/HotModule/src/main/java/org/example/core/HotModuleDataCenter.java +++ b/HotModule/src/main/java/org/example/core/HotModuleDataCenter.java @@ -27,7 +27,7 @@ import java.util.concurrent.ConcurrentHashMap; /** * 热门模块的数据中心 */ -public class HotModuleDataCenter extends CommonInitMachine { +public class HotModuleDataCenter{ /** * 设计这个参数的原因是,虽然HotModule可以存放HotLive数组,但是每天模块更新后会将整个hotModuleListPool替换,导致模块下的热门直播清空, @@ -121,7 +121,6 @@ public class HotModuleDataCenter extends CommonInitMachine { return this.getModuleList(platform).findHotModule(moduleId); } - @Override public boolean init() { return HotModuleDataCenter.DataCenter()!=null; } diff --git a/HotModule/src/main/java/org/example/guard/Guard.java b/HotModule/src/main/java/org/example/core/guard/Guard.java similarity index 83% rename from HotModule/src/main/java/org/example/guard/Guard.java rename to HotModule/src/main/java/org/example/core/guard/Guard.java index ff94b96..c39062f 100644 --- a/HotModule/src/main/java/org/example/guard/Guard.java +++ b/HotModule/src/main/java/org/example/core/guard/Guard.java @@ -1,11 +1,14 @@ -package org.example.guard; +package org.example.core.guard; import lombok.AllArgsConstructor; import lombok.Data; import org.example.bean.Live; 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.init.HeatRecommendationInitMachine; +import org.example.init.InitPluginRegister; import org.example.log.ResultLogger; import org.slf4j.Logger; @@ -60,7 +63,12 @@ public class Guard implements Runnable, ResultLogger { HotModuleDataCenter.DataCenter().addModuleList(platform,(HotModuleList) data); }else if(clazzName.contains("live")){ HotModuleDataCenter.DataCenter().addLiveList(platform,(List) data); + //查看热度推送插件是否装载,如果装载则进行热度推送 + if(InitPluginRegister.isRegister(PluginName.HOT_RECOMMENDATION_PLUGIN)){ + HeatRecommendationInitMachine.heatRecommendation.sendHotEvent(platform); + } } + } @Override diff --git a/HotModule/src/main/java/org/example/guard/HotModuleGuard.java b/HotModule/src/main/java/org/example/core/guard/HotModuleGuard.java similarity index 99% rename from HotModule/src/main/java/org/example/guard/HotModuleGuard.java rename to HotModule/src/main/java/org/example/core/guard/HotModuleGuard.java index 1adbe88..f20982f 100644 --- a/HotModule/src/main/java/org/example/guard/HotModuleGuard.java +++ b/HotModule/src/main/java/org/example/core/guard/HotModuleGuard.java @@ -1,4 +1,4 @@ -package org.example.guard; +package org.example.core.guard; import org.example.cache.FileCache; import org.example.cache.FileCacheManagerInstance; diff --git a/HotModule/src/main/java/org/example/guard/HotModuleGuardInstance.java b/HotModule/src/main/java/org/example/core/guard/HotModuleGuardInstance.java similarity index 95% rename from HotModule/src/main/java/org/example/guard/HotModuleGuardInstance.java rename to HotModule/src/main/java/org/example/core/guard/HotModuleGuardInstance.java index ef464d7..d583fff 100644 --- a/HotModule/src/main/java/org/example/guard/HotModuleGuardInstance.java +++ b/HotModule/src/main/java/org/example/core/guard/HotModuleGuardInstance.java @@ -1,4 +1,4 @@ -package org.example.guard; +package org.example.core.guard; import java.util.ArrayList; 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 23832ea..192e181 100644 --- a/HotModule/src/main/java/org/example/core/recommend/HeatRecommendation.java +++ b/HotModule/src/main/java/org/example/core/recommend/HeatRecommendation.java @@ -12,16 +12,16 @@ import org.example.cache.FileCacheManagerInstance; import org.example.config.FollowDog; import org.example.config.HotModuleConfig; import org.example.config.HotModuleSetting; +import org.example.constpool.PluginName; import org.example.core.HotModuleDataCenter; +import org.example.exception.InitException; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; import org.example.taskcenter.TaskCenter; import org.example.taskcenter.request.LiveReptileRequest; +import org.example.thread.ChopperBotGuardianTask; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; @@ -30,25 +30,23 @@ import java.util.regex.Pattern; /** * 根据热门的直播以及看门狗设置来推荐热门直播间到系统 */ -public class HeatRecommendation { - - private Map recommendationLog; //推送日志 +public class HeatRecommendation implements ChopperBotGuardianTask { private Map> platformFollowDogMap ; //每个平台的跟风列表 private BlockingQueue hotEventList; //用于接收每一次的热榜更新信息,并进行跟风狗推送 - private Reference reference; public HeatRecommendation(){ - recommendationLog = new ConcurrentHashMap<>(); 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)); }); @@ -58,14 +56,18 @@ public class HeatRecommendation { platformFollowDogMap.put(module.getPlatform(),module.getFollowDogs()); } } - - this.reference = new Reference(); } + + /** + * 处理阻塞队列中的热门推送提醒,并且从热点数据中心获取热门直播,进行推送 + * @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){ //发送给爬虫队列 @@ -91,22 +93,28 @@ public class HeatRecommendation { } } + /** + * 查看热门直播,对比banliver名单,选取前top个名额进行推送推荐名单 + * @param lives + * @param banLivers + * @param top + * @return + */ private List needRecommend(List lives,List banLivers,int top){ List recommendLive = new ArrayList<>(); + List livers = new ArrayList<>(); int num = 0; for (Live live : lives) { - if(recommendationLog.containsKey(live.getLiver())){ - if(top=top)break; } - ChopperLogFactory.getLogger(LoggerType.Hot).info(" recommend {} lives,lives info:",recommendLive); + ChopperLogFactory.getLogger(LoggerType.Hot).info(" recommend {} lives,liver info:",livers); return recommendLive; } @@ -119,21 +127,15 @@ public class HeatRecommendation { return false; } - public Reference getReference(){ - return reference; + public void sendHotEvent(String platform){ + hotEventList.offer(platform); } - - - - class Reference implements Runnable{ - - @Override - public void run() { - try { - handlerHotEvent(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + @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 new file mode 100644 index 0000000..48d013f --- /dev/null +++ b/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java @@ -0,0 +1,45 @@ +package org.example.init; + +import org.example.constpool.PluginName; +import org.example.core.recommend.HeatRecommendation; +import org.example.log.ChopperLogFactory; +import org.example.log.LoggerType; +import org.slf4j.Logger; + +import java.util.List; + +/** + * @author Genius + * @date 2023/07/29 14:48 + **/ +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 + ); + } + + @Override + public boolean init() { + if(checkNeedPlugin()){ + try { + heatRecommendation = new HeatRecommendation(); + }catch (Exception e){ + return fail(); + } + registerPlugin(); + return success(); + } + return false; + } + + @Override + public void afterInit() { + + heatRecommendation.guardian(); + } +} diff --git a/HotModule/src/main/java/org/example/init/HotModuleConfigInitMachine.java b/HotModule/src/main/java/org/example/init/HotModuleConfigInitMachine.java index 58a2f6e..5f988f5 100644 --- a/HotModule/src/main/java/org/example/init/HotModuleConfigInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HotModuleConfigInitMachine.java @@ -9,6 +9,7 @@ import org.example.cache.FileCache; import org.example.cache.FileCacheManagerInstance; import org.example.config.CreeperLogConfigFile; import org.example.config.HotModuleConfig; +import org.example.constpool.PluginName; import org.example.exception.FileCacheException; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; @@ -24,7 +25,9 @@ import java.util.Map; public class HotModuleConfigInitMachine extends ConfigInitMachine { public HotModuleConfigInitMachine() { - super(new HotModuleConfig(), ChopperLogFactory.getLogger(LoggerType.Hot)); + super(PluginName.HOT_CONFIG_PLUGIN + ,new HotModuleConfig() + ,ChopperLogFactory.getLogger(LoggerType.Hot)); } } diff --git a/HotModule/src/main/java/org/example/init/HotModuleGuardInitMachine.java b/HotModule/src/main/java/org/example/init/HotModuleGuardInitMachine.java index 46bd1d8..7915533 100644 --- a/HotModule/src/main/java/org/example/init/HotModuleGuardInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HotModuleGuardInitMachine.java @@ -6,9 +6,10 @@ import org.example.cache.FileCache; import org.example.cache.FileCacheManagerInstance; import org.example.config.HotModuleConfig; import org.example.config.HotModuleSetting; +import org.example.constpool.PluginName; import org.example.core.control.HotModuleLoadTask; -import org.example.guard.HotModuleGuardInstance; -import org.example.guard.Guard; +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.util.ClassUtil; @@ -28,9 +29,20 @@ public class HotModuleGuardInitMachine extends CommonInitMachine{ public HotModuleGuardInitMachine() { - super(ChopperLogFactory.getLogger(LoggerType.Hot)); + super( List.of(PluginName.HOT_CONFIG_PLUGIN, + PluginName.FILE_CACHE_PLUGIN), + + ChopperLogFactory.getLogger(LoggerType.Hot), + PluginName.HOT_GUARD_PLUGIN); } + /** + * 初始化整个热度监控的环境 + * 1,获取热度监控的所有配置,并进行读取和初始化 + * 2,获得所有守卫并进行初始化 + * 3,初始化热度监控插件 + * @throws Exception + */ private void envInit() throws Exception { FileCache HotModuleFileCache = FileCacheManagerInstance.getInstance().getFileCache(HotModuleConfig.getFullFilePath()); @@ -70,12 +82,16 @@ public class HotModuleGuardInitMachine extends CommonInitMachine{ @Override public boolean init() { - try { - envInit(); - } catch (Exception e) { - return fail(e.getMessage()); + if(checkNeedPlugin()){ + try { + envInit(); + } catch (Exception e) { + return fail(e.getMessage()); + } + registerPlugin(); + return success(); } - return success(); + return false; } @Override diff --git a/HotModule/src/main/java/org/example/init/HotModuleInitMachine.java b/HotModule/src/main/java/org/example/init/HotModuleInitMachine.java index 1530808..52dbdfd 100644 --- a/HotModule/src/main/java/org/example/init/HotModuleInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HotModuleInitMachine.java @@ -1,5 +1,7 @@ package org.example.init; +import org.example.constpool.ConstPool; +import org.example.core.recommend.HeatRecommendation; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; @@ -17,9 +19,14 @@ public class HotModuleInitMachine extends ModuleInitMachine{ public HotModuleInitMachine() { super( - List.of(new HotModuleConfigInitMachine(),new HotModuleGuardInitMachine()), - "HotModule", - ChopperLogFactory.getLogger(LoggerType.Hot) + List.of(ConstPool.FILE), + ChopperLogFactory.getLogger(LoggerType.Hot), + List.of( + new HotModuleConfigInitMachine(), //热门模块配置文件插件 + new HotModuleGuardInitMachine(), //平台热门直播,热门模块监控插件 + new HeatRecommendationInitMachine() //平台热门直播推送插件 + ), + ConstPool.HOT ); } } diff --git a/common/src/main/java/org/example/constpool/ConstPool.java b/common/src/main/java/org/example/constpool/ConstPool.java index 07cf45d..f91a398 100644 --- a/common/src/main/java/org/example/constpool/ConstPool.java +++ b/common/src/main/java/org/example/constpool/ConstPool.java @@ -10,13 +10,14 @@ import java.util.List; public class ConstPool { /**模块名**/ - public static final String ACCOUNT = "account"; - public static final String SECTION = "section"; - public static final String BARRAGE = "barrage"; - public static final String CREEPER = "creeper"; - public static final String SECTION_WORK = "section_work"; - public static final String HOT = "hot"; - public static final String PUBLISH = "publish"; + public static final String FILE = "File"; + public static final String ACCOUNT = "Account"; + public static final String SECTION = "Section"; + public static final String BARRAGE = "Barrage"; + public static final String CREEPER = "Creeper"; + public static final String SECTION_WORK = "SectionWork"; + public static final String HOT = "HotModule"; + public static final String PUBLISH = "Publish"; /**其他**/ public static final String NULL_TIME = "nil"; diff --git a/common/src/main/java/org/example/constpool/PluginName.java b/common/src/main/java/org/example/constpool/PluginName.java new file mode 100644 index 0000000..4168891 --- /dev/null +++ b/common/src/main/java/org/example/constpool/PluginName.java @@ -0,0 +1,25 @@ +package org.example.constpool; + +/** + * @author Genius + * @date 2023/07/29 15:12 + **/ + +/** + * ChopperBot 插件名称 + */ +public class PluginName { + + //File + public static String MODULE_CONFIG_PLUGIN = "ModuleConfig"; + public static String FILE_CACHE_PLUGIN = "FileCache"; + + //Creeper + public static String CREEPER_CONFIG_PLUGIN = "CreeperConfig"; + public static 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"; +} diff --git a/common/src/main/java/org/example/exception/InitException.java b/common/src/main/java/org/example/exception/InitException.java new file mode 100644 index 0000000..4f32fe5 --- /dev/null +++ b/common/src/main/java/org/example/exception/InitException.java @@ -0,0 +1,23 @@ +package org.example.exception; + +/** + * @author Genius + * @date 2023/07/29 14:59 + **/ + +/** + * 初始化报错 + */ +public class InitException extends RuntimeException{ + + private String message; + + public InitException(String message) { + this.message = message; + } + + @Override + public String getMessage() { + return message; + } +} diff --git a/common/src/main/java/org/example/init/CommonInitMachine.java b/common/src/main/java/org/example/init/CommonInitMachine.java index 4daefd4..dfd44c4 100644 --- a/common/src/main/java/org/example/init/CommonInitMachine.java +++ b/common/src/main/java/org/example/init/CommonInitMachine.java @@ -8,6 +8,8 @@ package org.example.init; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; import java.util.function.Supplier; /** @@ -15,20 +17,59 @@ import java.util.function.Supplier; */ public abstract class CommonInitMachine implements ComponentInitMachine{ + protected List needPlugins; //初始化时需要的插件 + + protected String pluginName; + protected Logger logger; - public CommonInitMachine(Logger logger){ - this.logger = logger; + + + /** + * 注册插件 + */ + public void registerPlugin(){ + InitPluginRegister.registerPluginTable.put(pluginName,this.getClass()); } - public CommonInitMachine(){ - this.logger = LoggerFactory.getLogger(this.getClass().getName()); + /** + * 检查该插件需要的其他插件 + * @return + */ + @Override + public boolean checkNeedPlugin() { + for (String needPlugin : needPlugins) { + if(!InitPluginRegister.registerPluginTable.containsKey(needPlugin)){ + fail(String.format("Missing {%s} plugin,please check your plugin init!",needPlugin)); + return false; + } + } + return true; + } + + + public CommonInitMachine(List needPlugins, Logger logger, String pluginName) { + this.needPlugins = needPlugins; + this.logger = logger; + this.pluginName = pluginName; + } + + 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!",this.getClass().toString())); + successLog(String.format("[✔] {%s} init success!",pluginName)); } @Override @@ -38,7 +79,7 @@ public abstract class CommonInitMachine implements ComponentInitMachine{ @Override public void failLog() { - failLog(String.format("[❌] {%s} init error!",this.getClass().toString())); + failLog(String.format("[❌] {%s} init error!",pluginName)); } @Override @@ -48,7 +89,7 @@ public abstract class CommonInitMachine implements ComponentInitMachine{ @Override public boolean fail(String failCause) { - failLog(String.format("[❌] {%s} init error! Execption:{%s}",this.getClass().toString(),failCause)); + failLog(String.format("[❌] {%s} init error! Execption:{%s}",pluginName,failCause)); return false; } @@ -69,7 +110,7 @@ public abstract class CommonInitMachine implements ComponentInitMachine{ } private void shutdownLog(){ - logger.info("[🆖] {} close success.",this.getClass().getName()); + logger.info("[🆖] {} close success.",pluginName); } @Override diff --git a/common/src/main/java/org/example/init/ComponentInitMachine.java b/common/src/main/java/org/example/init/ComponentInitMachine.java index 3d1ef53..dcc5c9f 100644 --- a/common/src/main/java/org/example/init/ComponentInitMachine.java +++ b/common/src/main/java/org/example/init/ComponentInitMachine.java @@ -7,6 +7,7 @@ package org.example.init; public interface ComponentInitMachine extends InitMachine{ + boolean checkNeedPlugin(); default boolean fail(){ return fail(""); } diff --git a/common/src/main/java/org/example/init/InitPluginRegister.java b/common/src/main/java/org/example/init/InitPluginRegister.java new file mode 100644 index 0000000..3cb7e63 --- /dev/null +++ b/common/src/main/java/org/example/init/InitPluginRegister.java @@ -0,0 +1,21 @@ +package org.example.init; + +/** + * @author Genius + * @date 2023/07/29 15:21 + **/ + +import java.util.concurrent.ConcurrentHashMap; + +/** + * 插件注册中心 + */ +public class InitPluginRegister { + + public static ConcurrentHashMap> registerPluginTable = new ConcurrentHashMap<>(); + + public static boolean isRegister(String pluginName){ + return registerPluginTable.containsKey(pluginName); + } + +} diff --git a/common/src/main/java/org/example/init/ModuleInitMachine.java b/common/src/main/java/org/example/init/ModuleInitMachine.java index 948afdc..2c818ea 100644 --- a/common/src/main/java/org/example/init/ModuleInitMachine.java +++ b/common/src/main/java/org/example/init/ModuleInitMachine.java @@ -1,5 +1,6 @@ package org.example.init; +import lombok.extern.java.Log; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,18 +24,36 @@ public abstract class ModuleInitMachine extends CommonInitMachine{ private String moduleName; + @Override + public boolean checkNeedPlugin() { + for (String needPlugin : needPlugins) { + if(!InitPluginRegister.registerPluginTable.containsKey(needPlugin)){ + fail(String.format("Missing {%s} module,please check your module init!",needPlugin)); + return false; + } + } + return true; + } + @Override public boolean init() { return initLogger(()->{ - for (InitMachine initMachine : this.getInitMachines()) { - if(!initMachine.init()){ - return fail(); + if(checkNeedPlugin()){ + for (InitMachine initMachine : this.getInitMachines()) { + if(!initMachine.init()){ + return fail(); + } } + registerPlugin(); + return success(); } - return success(); + return false; + }); } + + @Override public void afterInit() { for(InitMachine initMachine:this.getInitMachines()){ @@ -43,16 +62,22 @@ public abstract class ModuleInitMachine extends CommonInitMachine{ } public ModuleInitMachine(List initMachines, String moduleName) { + super(moduleName); this.initMachines = initMachines; this.moduleName = moduleName; } public ModuleInitMachine(List initMachines,String moduleName,Logger logger){ - super(logger); + super(logger,moduleName); this.initMachines = initMachines; 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() { return initMachines; diff --git a/common/src/main/java/org/example/thread/ChopperBotGuardPool.java b/common/src/main/java/org/example/thread/ChopperBotGuardPool.java new file mode 100644 index 0000000..fc3fdd3 --- /dev/null +++ b/common/src/main/java/org/example/thread/ChopperBotGuardPool.java @@ -0,0 +1,51 @@ +package org.example.thread; + +/** + * @author Genius + * @date 2023/07/24 23:51 + **/ + +import lombok.Data; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + + +/** + * ChopperBot系统的线程池 + * TODO 待完善 + */ +@Data +public class ChopperBotGuardPool { + + private ExecutorService threadPool; + + private volatile static ChopperBotGuardPool pool; + + private ChopperBotGuardPool() { + this.threadPool = Executors.newFixedThreadPool(10,new NamedThreadFactory("GuardThread")); + } + + public static ChopperBotGuardPool GuardPool(){ + if(pool==null){ + synchronized (ChopperBotGuardPool.class){ + if(pool==null){ + init(); + } + } + } + return pool; + } + public static void init(){ + pool = new ChopperBotGuardPool(); + } + + public ExecutorService getThreadPool() { + return threadPool; + } + + public void shutdown(){ + threadPool.shutdown(); + } + +} diff --git a/common/src/main/java/org/example/thread/ChopperBotGuardianTask.java b/common/src/main/java/org/example/thread/ChopperBotGuardianTask.java new file mode 100644 index 0000000..ae6a6d8 --- /dev/null +++ b/common/src/main/java/org/example/thread/ChopperBotGuardianTask.java @@ -0,0 +1,16 @@ +package org.example.thread; + + +/** + * ChopperBot系统守护线程,用于运行一些系统启动到结束都需要使用的线程,例如:TaskCenter,HeatRecommendation,OddJobBoy等 + */ +public interface ChopperBotGuardianTask { + + void threadTask(); + + default void guardian(){ + ChopperBotGuardPool.GuardPool().getThreadPool().submit( + this::threadTask + ); + } +} diff --git a/common/src/main/java/org/example/thread/ChopperBotThreadPool.java b/common/src/main/java/org/example/thread/ChopperBotThreadPool.java deleted file mode 100644 index 28ec5d8..0000000 --- a/common/src/main/java/org/example/thread/ChopperBotThreadPool.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.example.thread; - -/** - * @author Genius - * @date 2023/07/24 23:51 - **/ - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - - -/** - * ChopperBot系统的线程池 - * TODO 待完善 - */ -public class ChopperBotThreadPool { - - public static ExecutorService threadPool = Executors.newFixedThreadPool(10); -} diff --git a/common/src/main/java/org/example/thread/oddjob/OddJobBoy.java b/common/src/main/java/org/example/thread/oddjob/OddJobBoy.java index 0b38717..ab628f0 100644 --- a/common/src/main/java/org/example/thread/oddjob/OddJobBoy.java +++ b/common/src/main/java/org/example/thread/oddjob/OddJobBoy.java @@ -1,5 +1,8 @@ package org.example.thread.oddjob; +import org.example.log.ChopperLogFactory; +import org.example.log.LoggerType; +import org.example.thread.ChopperBotGuardianTask; import org.example.thread.NamedThreadFactory; import java.util.concurrent.*; @@ -12,16 +15,13 @@ import java.util.concurrent.*; /** * ChopperBot系统中专门用来处理异步事件的类 */ -public class OddJobBoy { +public class OddJobBoy implements ChopperBotGuardianTask { private static volatile OddJobBoy Instance; private BlockingQueue oddjobs; - private ExecutorService home; - private OddJobBoy(){ - home = Executors.newSingleThreadExecutor(new NamedThreadFactory("OddJobBoy")); oddjobs = new ArrayBlockingQueue<>(1024); } @@ -30,7 +30,6 @@ public class OddJobBoy { synchronized (OddJobBoy.class){ if(Instance==null){ Instance = new OddJobBoy(); - Instance.work(); } } } @@ -41,26 +40,16 @@ public class OddJobBoy { oddjobs.put(job); } - private void work(){ - home.submit(new Boy()); - } - public boolean relax(){ - home.shutdown(); - return home.isShutdown(); - } + @Override + public void threadTask() { + while(true){ + try { + OddJob job = oddjobs.take(); + ChopperLogFactory.getLogger(LoggerType.System).info(" boy get a odd job:{},Processing...",job); + job.doJob(); + }catch (InterruptedException e){ - class Boy implements Runnable{ - - @Override - public void run() { - while(true){ - try { - OddJob job = oddjobs.take(); - job.doJob(); - }catch (InterruptedException e){ - - } } } } diff --git a/console/src/main/java/org/example/init/InitWorld.java b/console/src/main/java/org/example/init/InitWorld.java index 76e0f85..3822548 100644 --- a/console/src/main/java/org/example/init/InitWorld.java +++ b/console/src/main/java/org/example/init/InitWorld.java @@ -1,15 +1,10 @@ package org.example.init; -import org.example.guard.HotModuleGuardInstance; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; -import java.util.ArrayList; -import java.util.List; /** * @author Genius diff --git a/console/src/main/java/org/example/init/SystemInitMachine.java b/console/src/main/java/org/example/init/SystemInitMachine.java index 37e8768..1e61433 100644 --- a/console/src/main/java/org/example/init/SystemInitMachine.java +++ b/console/src/main/java/org/example/init/SystemInitMachine.java @@ -1,7 +1,9 @@ package org.example.init; +import com.genius.assistant.util.path.PathUtils; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; +import org.example.thread.ChopperBotGuardPool; import org.example.thread.oddjob.OddJobBoy; import java.util.List; @@ -22,6 +24,15 @@ public class SystemInitMachine extends ModuleInitMachine{ ), "ChopperBot", ChopperLogFactory.getLogger(LoggerType.System)); } + + + @Override + public boolean init() { + ChopperBotGuardPool.init(); + OddJobBoy.Boy().guardian(); + return super.init(); + } + @Override public void afterInit() { try { @@ -38,6 +49,8 @@ public class SystemInitMachine extends ModuleInitMachine{ } + + @Override protected boolean initLogger(Supplier init) { logger.info("🌏 <{}> Wake up,Find {} module need to init,please wait.....","ChopperBot",getInitMachines().size()); @@ -47,9 +60,12 @@ public class SystemInitMachine extends ModuleInitMachine{ @Override public void shutdown() { logger.info("🌏 <{}> is shutting down,{} modules need to be closed,please wait.....","ChopperBot",getInitMachines().size()); + + ChopperBotGuardPool.GuardPool().shutdown(); this.getInitMachines().forEach( InitMachine::shutdown ); + logger.info("🌏 <{}> all modules have been closed. Good Bye~🤗","ChopperBot"); }