今日更新内容:

1,InitPluginRegister 增加了插件管理和插件注册,提供了插件依赖接口,每个插件初始化的时候会检查依赖插件是否完备,构成依赖启动
2,添加了一个系统守护线程池,就是一个线程池,待拓展和优化,负责运行一些单例线程独立运行的插件
This commit is contained in:
userA
2023-07-29 23:55:31 +08:00
parent d274d48423
commit 23a5f64be3
31 changed files with 450 additions and 144 deletions

View File

@@ -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<CreeperConfigFile> {
public CreeperConfigInitMachine() {
super(new CreeperConfigFile(), ChopperLogFactory.getLogger(LoggerType.Creeper));
super(PluginName.CREEPER_CONFIG_PLUGIN,
new CreeperConfigFile(),
ChopperLogFactory.getLogger(LoggerType.Creeper)
);
}
@Override

View File

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

View File

@@ -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();
}
}

View File

@@ -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("<TaskCenter> 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();
}
}

View File

@@ -259,7 +259,6 @@ public class FileCache <T extends ConfigFile>{
String dir = getFullFilePath();
configFile.onlyUpdateTime(take);
File file = JsonFileUtil.writeJsonFile(dir, take);
logger.debug("正在写入{}新版本",dir);
return Objects.isNull(file);
}

View File

@@ -22,8 +22,8 @@ public abstract class ConfigInitMachine<T extends ConfigFile> 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<T extends ConfigFile> 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;
}

View File

@@ -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());

View File

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

View File

@@ -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();
}

View File

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

View File

@@ -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<Live>) data);
//查看热度推送插件是否装载如果装载则进行热度推送
if(InitPluginRegister.isRegister(PluginName.HOT_RECOMMENDATION_PLUGIN)){
HeatRecommendationInitMachine.heatRecommendation.sendHotEvent(platform);
}
}
}
@Override

View File

@@ -1,4 +1,4 @@
package org.example.guard;
package org.example.core.guard;
import org.example.cache.FileCache;
import org.example.cache.FileCacheManagerInstance;

View File

@@ -1,4 +1,4 @@
package org.example.guard;
package org.example.core.guard;
import java.util.ArrayList;

View File

@@ -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<String, Live> recommendationLog; //推送日志
public class HeatRecommendation implements ChopperBotGuardianTask {
private Map<String, List<FollowDog>> platformFollowDogMap ; //每个平台的跟风列表
private BlockingQueue<String> hotEventList; //用于接收每一次的热榜更新信息,并进行跟风狗推送
private Reference reference;
public HeatRecommendation(){
recommendationLog = new ConcurrentHashMap<>();
platformFollowDogMap = new ConcurrentHashMap<>();
hotEventList = new ArrayBlockingQueue<>(1024);
List<HotModuleSetting> 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<FollowDog> 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<Live> needRecommend(List<? extends Live> lives,List<String> banLivers,int top){
List<Live> recommendLive = new ArrayList<>();
List<String> livers = new ArrayList<>();
int num = 0;
for (Live live : lives) {
if(recommendationLog.containsKey(live.getLiver())){
if(top<num){
String liver = live.getLiver();
if (!isBan(liver,banLivers)) {
recommendLive.add(live);
num++;
}
}
String liver = live.getLiver();
if (!isBan(liver,banLivers)) {
recommendLive.add(live);
livers.add(liver);
num++;
}
if(num>=top)break;
}
ChopperLogFactory.getLogger(LoggerType.Hot).info("<HeatRecommend> recommend {} lives,lives info:",recommendLive);
ChopperLogFactory.getLogger(LoggerType.Hot).info("<HeatRecommend> 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);
}
}

View File

@@ -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();
}
}

View File

@@ -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<HotModuleConfig> {
public HotModuleConfigInitMachine() {
super(new HotModuleConfig(), ChopperLogFactory.getLogger(LoggerType.Hot));
super(PluginName.HOT_CONFIG_PLUGIN
,new HotModuleConfig()
,ChopperLogFactory.getLogger(LoggerType.Hot));
}
}

View File

@@ -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

View File

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

View File

@@ -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";

View File

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

View File

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

View File

@@ -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<String> 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<String> 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

View File

@@ -7,6 +7,7 @@ package org.example.init;
public interface ComponentInitMachine extends InitMachine{
boolean checkNeedPlugin();
default boolean fail(){
return fail("");
}

View File

@@ -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<String,Class<? extends InitMachine>> registerPluginTable = new ConcurrentHashMap<>();
public static boolean isRegister(String pluginName){
return registerPluginTable.containsKey(pluginName);
}
}

View File

@@ -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<InitMachine> initMachines, String moduleName) {
super(moduleName);
this.initMachines = initMachines;
this.moduleName = moduleName;
}
public ModuleInitMachine(List<InitMachine> initMachines,String moduleName,Logger logger){
super(logger);
super(logger,moduleName);
this.initMachines = initMachines;
this.moduleName = moduleName;
}
public ModuleInitMachine(List<String> needPlugins, Logger logger, List<InitMachine> initMachines, String moduleName) {
super(needPlugins, logger, moduleName);
this.initMachines = initMachines;
this.moduleName = moduleName;
}
public List<InitMachine> getInitMachines() {
return initMachines;

View File

@@ -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();
}
}

View File

@@ -0,0 +1,16 @@
package org.example.thread;
/**
* ChopperBot系统守护线程用于运行一些系统启动到结束都需要使用的线程例如TaskCenterHeatRecommendationOddJobBoy等
*/
public interface ChopperBotGuardianTask {
void threadTask();
default void guardian(){
ChopperBotGuardPool.GuardPool().getThreadPool().submit(
this::threadTask
);
}
}

View File

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

View File

@@ -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<OddJob> 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("<OddJobBoy> 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){
}
}
}
}

View File

@@ -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

View File

@@ -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<Boolean> 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");
}