1,完成模块启动架构搭建

2,完成hotModule模块配置文件创建
3,模块启动日志完成
This commit is contained in:
userA
2023-07-21 02:17:04 +08:00
parent 98e912682f
commit a55490c184
19 changed files with 352 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
package org.example.cache;
import org.example.log.FileModuleLogger;
import org.example.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -75,6 +76,7 @@ public class FileCacheManager {
fileCaches.add(fileCache);
fileCacheMap.put(fileCache.getFullFilePath(),fileCache);
initSleepTime();
FileModuleLogger.logger.info("FileCacheManager add a new FileCache:{}",fileCache.getFullFilePath());
}
return false;
}

View File

@@ -6,7 +6,7 @@ import org.example.cache.FileCacheManagerInstance;
* @author Genius
* @date 2023/04/26 02:09
**/
public class FileCacheManagerInit implements InitMachine{
public class FileCacheManagerInit extends CommonInitMachine{
@Override
public boolean init() {

View File

@@ -15,7 +15,7 @@ import java.util.Map;
* @date 2023/04/20 18:34
**/
public class ModuleSrcConfigFileInit implements InitMachine {
public class ModuleSrcConfigFileInit extends CommonInitMachine {
private Logger logger = LoggerFactory.getLogger(ModuleSrcConfigFileInit.class);

View File

@@ -61,7 +61,6 @@ public class FileCacheTest {
FileCacheManager manager = new FileCacheManager(List.of(fileCache));
manager.start();
System.out.println(fileCache.get("barrage","src"));
fileCache.append(1,"barrage","src");
}
@@ -109,7 +108,6 @@ public class FileCacheTest {
FileCacheManager manager = new FileCacheManager(List.of(fileCache));
manager.start();
System.out.println(manager.addFileCache(fileCache));
}
@Test

View File

@@ -19,4 +19,12 @@ public class FollowDog {
private int top; //前几个
private List<String> banLiver; //去除的主播,可以是正则
@Override
public String toString() {
return "FollowDog{" +
"moduleName='" + moduleName + '\'' +
", top=" + top +
", banLiver=" + banLiver +
'}';
}
}

View File

@@ -2,6 +2,7 @@ package org.example.config;
import org.example.bean.ConfigFile;
import org.example.constpool.ConstPool;
import org.example.constpool.HotModuleConstPool;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -13,25 +14,23 @@ import java.util.Map;
* @date 2023/07/20 00:20
**/
public class HotModuleConfig extends ConfigFile<Map<String,Object>> {
private static final FollowDog allLivesDog = new FollowDog(FollowDog.ALL_LIVES,6,new ArrayList<>());
private static final int FiveMinute = 0x493E0;
private static final String filePath = "./config";
private static final String fileName = "hotmodule.config";
private static final String fileName = "hotModuleConfig.json";
public HotModuleConfig(){
super(filePath,fileName,
super(HotModuleConstPool.HOT_MODULE_CONFIG_ROOT,fileName,
Map.of("Enable", 1,
"Module", List.of(
new ModuleSetting(ConstPool.DOUYU, true, new ArrayList<>(),
false, List.of(allLivesDog), FiveMinute, FiveMinute),
new ModuleSetting(ConstPool.BILIBILI, true, new ArrayList<>(),
false, List.of(allLivesDog), FiveMinute, FiveMinute),
new ModuleSetting(ConstPool.HUYA, true, new ArrayList<>(),
false, List.of(allLivesDog), FiveMinute, FiveMinute),
new ModuleSetting(ConstPool.DOUYU, true, new ArrayList<>(),
false, List.of(allLivesDog), FiveMinute, FiveMinute)
new ModuleSetting(ConstPool.DOUYU, true, new ArrayList<>(), false,
List.of(allLiveDog()), FiveMinute, FiveMinute),
new ModuleSetting(ConstPool.BILIBILI, true, new ArrayList<>(), false,
List.of(allLiveDog()), FiveMinute, FiveMinute),
new ModuleSetting(ConstPool.HUYA, true, new ArrayList<>(), false,
List.of(allLiveDog()), FiveMinute, FiveMinute),
new ModuleSetting(ConstPool.DOUYING, true, new ArrayList<>(), false,
List.of(allLiveDog()), FiveMinute, FiveMinute)
),
"GuardNum",10
)
@@ -39,6 +38,10 @@ public class HotModuleConfig extends ConfigFile<Map<String,Object>> {
}
public static String getFullFilePath(){
return Paths.get(filePath,fileName).toString();
return Paths.get(HotModuleConstPool.HOT_MODULE_CONFIG_ROOT,fileName).toString();
}
private static FollowDog allLiveDog(){
return new FollowDog(FollowDog.ALL_LIVES,6,new ArrayList<>());
}
}

View File

@@ -0,0 +1,13 @@
package org.example.constpool;
import org.example.cache.FileCacheManagerInstance;
import org.example.config.HotModuleConfig;
/**
* @author Genius
* @date 2023/07/21 00:21
**/
public class HotModuleConstPool {
public static final String HOT_MODULE_CONFIG_ROOT = (String) GlobalFileCache.ModuleSrcConfigFile.get("hot","src");
}

View File

@@ -1,8 +1,10 @@
package org.example.guard;
import org.example.cache.FileCache;
import org.example.cache.FileCacheManager;
import org.example.cache.FileCacheManagerInstance;
import org.example.config.HotModuleConfig;
import org.example.constpool.HotModuleConstPool;
import org.example.core.control.impl.DouyuHotLiveLoadTask;
import org.example.core.control.impl.DouyuHotModuleLoadTask;
import org.example.core.processor.hotmodule.DouyuHotLiveProcessor;
@@ -19,6 +21,8 @@ import java.util.concurrent.TimeUnit;
public class HotModuleGuard {
private static final long delayTime = 10*1000;
private FileCache HotModuleFileCache = FileCacheManagerInstance.getInstance().getFileCache(HotModuleConfig.getFullFilePath());
private ScheduledExecutorService hotModuleGuardPool = Executors.newScheduledThreadPool(
(Integer)FileCacheManagerInstance.getInstance().getFileCache(HotModuleConfig.getFullFilePath()).get("GuardNum")
);

View File

@@ -0,0 +1,8 @@
package org.example.guard.task;
/**
* @author Genius
* @date 2023/07/21 02:13
**/
public class Guard {
}

View File

@@ -0,0 +1,56 @@
package org.example.init;
/**
* @author Genius
* @date 2023/07/21 00:18
**/
import org.example.cache.FileCache;
import org.example.cache.FileCacheManagerInstance;
import org.example.config.HotModuleConfig;
import org.example.constpool.HotModuleConstPool;
import org.example.exception.FileCacheException;
import org.example.log.HotModuleLogger;
import org.example.util.FileUtil;
import org.example.util.JsonFileUtil;
import org.slf4j.Logger;
import java.nio.file.Paths;
import java.util.Map;
/**
* 热门模块配置文件初始化机器
*/
public class HotModuleConfigInitMachine extends CommonInitMachine {
private String filePath = HotModuleConfig.getFullFilePath();
public HotModuleConfigInitMachine() {
super(HotModuleLogger.logger);
}
@Override
public boolean init() {
HotModuleConfig hotModuleConfig = new HotModuleConfig();
if(!FileUtil.isFileExist(filePath)){
JsonFileUtil.writeJsonFile(filePath,hotModuleConfig.packageConfig());
try {
FileCache fileCache = new FileCache(hotModuleConfig);
FileCacheManagerInstance.getInstance().addFileCache(fileCache);
} catch (FileCacheException e) {
return fail(e.toString());
}
return success(String.format("[✔]%s is created,{%s} plugin init success!",filePath,this.getClass().toString()));
}else{
Map<String, Object> data = JsonFileUtil.readJsonFile(filePath);
hotModuleConfig.setData(data);
try {
FileCache fileCache = new FileCache(hotModuleConfig);
FileCacheManagerInstance.getInstance().addFileCache(fileCache);
} catch (FileCacheException e) {
return fail(e.toString());
}
}
return success(String.format("[✔]%s read in disk,{%s} plugin init success!",filePath,this.getClass().toString()));
}
}

View File

@@ -0,0 +1,38 @@
package org.example.init;
import org.example.log.HotModuleLogger;
import java.util.List;
/**
* @author Genius
* @date 2023/07/21 00:16
**/
/**
* 整个热门模块的模块初始化类
*/
public class HotModuleInitMachine extends ModuleInitMachine{
public HotModuleInitMachine() {
super(
List.of(new HotModuleConfigInitMachine()),
"HotModule",
HotModuleLogger.logger
);
}
@Override
public boolean init() {
return initLogger(()->{
for (InitMachine initMachine : this.getInitMachines()) {
if(!initMachine.init()){
return fail();
}
}
return success();
});
}
}

View File

@@ -0,0 +1,63 @@
package org.example.init;
/**
* @author Genius
* @date 2023/07/21 00:57
**/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 模块中的小模块初始化抽象类
*/
public abstract class CommonInitMachine implements ComponentInitMachine{
protected Logger logger;
public CommonInitMachine(Logger logger){
this.logger = logger;
}
public CommonInitMachine(){
this.logger = LoggerFactory.getLogger(this.getClass().getName());
}
@Override
public void successLog() {
successLog(String.format("[✔] {%s} init success!",this.getClass().toString()));
}
@Override
public void successLog(String str) {
logger.info(str);
}
@Override
public void failLog() {
failLog(String.format("[❌] {%s} init error!",this.getClass().toString()));
}
@Override
public void failLog(String str) {
logger.error(str);
}
@Override
public boolean fail(String failCause) {
failLog(String.format("[❌] {%s} init error! Execption:{}",this.getClass().toString(),failCause));
return false;
}
@Override
public boolean success() {
successLog();
return true;
}
public boolean success(String str){
successLog(str);
return true;
}
}

View File

@@ -0,0 +1,17 @@
package org.example.init;
/**
* @author Genius
* @date 2023/07/21 00:12
**/
public interface ComponentInitMachine extends InitMachine{
default boolean fail(){
return fail("");
}
boolean fail(String failCause);
boolean success();
}

View File

@@ -1,13 +1,15 @@
package org.example.init;
import org.example.log.InitMachineLogger;
/**
* @author Genius
* @date 2023/04/20 19:36
**/
//模块初始化接口
public interface InitMachine {
public interface InitMachine extends InitMachineLogger {
boolean init();
}

View File

@@ -0,0 +1,90 @@
package org.example.init;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @author Genius
* @date 2023/07/21 00:39
**/
/**
* 模块初始化机器,用于整个模块的初始化
*/
public abstract class ModuleInitMachine extends CommonInitMachine{
private List<InitMachine> initMachines;
private String moduleName;
public ModuleInitMachine(List<InitMachine> initMachines,String moduleName) {
this.initMachines = initMachines;
this.moduleName = moduleName;
}
public ModuleInitMachine(List<InitMachine> initMachines,String moduleName,Logger logger){
super(logger);
this.initMachines = initMachines;
this.moduleName = moduleName;
}
public List<InitMachine> getInitMachines() {
return initMachines;
}
public void setInitMachines(List<InitMachine> initMachines) {
this.initMachines = initMachines;
}
@Override
public void successLog() {
successLog(String.format("✅ <%s> init success! init %s plugins ! ",moduleName,initMachines.size()));
}
@Override
public void successLog(String str) {
logger.info(str);
}
@Override
public void failLog() {
failLog(String.format("⛔ <%s> init error!",moduleName));
}
@Override
public void failLog(String str) {
logger.info(str);
}
protected boolean initLogger(Supplier<Boolean> init){
logger.info("🕑 <{}> start to init,total plugins:{}",moduleName,initMachines.size());
return init.get();
}
@Override
public boolean fail() {
failLog();
return false;
}
@Override
public boolean fail(String failCause) {
failLog(String.format("⛔ <%s> init error! Execption:%s",moduleName,failCause));
return false;
}
@Override
public boolean success() {
successLog();
return true;
}
}

View File

@@ -0,0 +1,12 @@
package org.example.log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Genius
* @date 2023/07/21 00:32
**/
public class FileModuleLogger {
public static final Logger logger = LoggerFactory.getLogger("FileModuleLogger");
}

View File

@@ -0,0 +1,16 @@
package org.example.log;
/**
* 启动日志接口
*/
public interface InitMachineLogger {
void successLog();
void successLog(String str);
void failLog();
void failLog(String str);
}

View File

@@ -35,7 +35,7 @@
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>FileModule</artifactId>
<artifactId>HotModule</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>

View File

@@ -3,6 +3,7 @@ package org.example;
import org.example.exception.FileCacheException;
import org.example.init.FileCacheManagerInit;
import org.example.init.HotModuleInitMachine;
import org.example.init.InitWorld;
import org.example.init.ModuleSrcConfigFileInit;
import org.springframework.boot.SpringApplication;
@@ -30,7 +31,8 @@ public class ConsoleApplication {
.setInitMachines(
List.of(
new ModuleSrcConfigFileInit(),
new FileCacheManagerInit()
new FileCacheManagerInit(),
new HotModuleInitMachine()
)
).start();
}