mirror of
https://github.com/Geniusay/ChopperBot.git
synced 2026-05-23 04:39:41 +08:00
框架大更新
This commit is contained in:
@@ -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<FileCache> fileCaches;
|
||||
private List<FileCache> fileCaches;
|
||||
|
||||
private final ConcurrentHashMap<String,FileCache> fileCacheMap;
|
||||
private ConcurrentHashMap<String,FileCache> fileCacheMap;
|
||||
|
||||
private AtomicLong sleepTime; //睡眠时间
|
||||
|
||||
@@ -32,15 +33,25 @@ public class FileCacheManager {
|
||||
|
||||
private volatile Watcher watcher;
|
||||
|
||||
protected FileCacheManager(List<FileCache> 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<String> 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<FileCache> fileCaches) {
|
||||
this.fileCaches = fileCaches;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<FileCache> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T extends ConfigFile> 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<String> needPlugins, boolean isAutoStart, String moduleName, String name, Class<? extends CommonPlugin> 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<String> needPlugins, boolean isAutoStart, String moduleName, String name, Class<? extends CommonPlugin> 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<Map<String, Object>> {
|
||||
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<String,Object> packageConfig() {
|
||||
return super.packageConfig();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user