mirror of
https://github.com/Geniusay/ChopperBot.git
synced 2026-05-24 05:11:11 +08:00
1,完成整个项目的启动模块
2,完成项目的安全关闭功能
This commit is contained in:
@@ -321,4 +321,9 @@ public class FileCache <T extends ConfigFile>{
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean close(){
|
||||
pool.shutdown();
|
||||
return pool.isShutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import static java.lang.Thread.sleep;
|
||||
*/
|
||||
public class FileCacheManager {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(FileCacheManager.class);
|
||||
private final List<FileCache> fileCaches;
|
||||
|
||||
private final ConcurrentHashMap<String,FileCache> fileCacheMap;
|
||||
@@ -134,4 +133,11 @@ public class FileCacheManager {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean close(){
|
||||
watchPool.shutdown();
|
||||
autoSyncer.shutdown();
|
||||
fileCaches.forEach(FileCache::close);
|
||||
return watchPool.isShutdown()&&autoSyncer.isShutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.example.init;
|
||||
|
||||
import org.example.cache.FileCacheManagerInstance;
|
||||
import org.example.log.FileModuleLogger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* @author Genius
|
||||
@@ -8,9 +10,23 @@ import org.example.cache.FileCacheManagerInstance;
|
||||
**/
|
||||
public class FileCacheManagerInit extends CommonInitMachine{
|
||||
|
||||
public FileCacheManagerInit() {
|
||||
super(FileModuleLogger.logger);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init() {
|
||||
FileCacheManagerInstance.getInstance().start();
|
||||
return true;
|
||||
try {
|
||||
FileCacheManagerInstance.getInstance().start();
|
||||
return success();
|
||||
}catch (Exception e){
|
||||
return fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
super.shutdown();
|
||||
FileCacheManagerInstance.getInstance().close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.example.init;
|
||||
|
||||
import org.example.log.FileModuleLogger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Genius
|
||||
* @date 2023/07/22 18:47
|
||||
**/
|
||||
public class FileModuleInitMachine extends ModuleInitMachine{
|
||||
|
||||
public FileModuleInitMachine() {
|
||||
super(List.of(
|
||||
new ModuleSrcConfigFileInit(),
|
||||
new FileCacheManagerInit()
|
||||
), "FileModule", FileModuleLogger.logger);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.example.init;
|
||||
|
||||
import org.example.log.FileModuleLogger;
|
||||
import org.example.pojo.configfile.ModuleSrcConfigFile;
|
||||
import org.example.util.FileUtil;
|
||||
import org.example.util.JsonFileUtil;
|
||||
@@ -17,35 +18,34 @@ import java.util.Map;
|
||||
|
||||
public class ModuleSrcConfigFileInit extends CommonInitMachine {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(ModuleSrcConfigFileInit.class);
|
||||
|
||||
ModuleSrcConfigFile moduleSrcConfigFile;
|
||||
|
||||
public ModuleSrcConfigFileInit() {
|
||||
moduleSrcConfigFile = new ModuleSrcConfigFile();
|
||||
super(FileModuleLogger.logger);
|
||||
moduleSrcConfigFile = new ModuleSrcConfigFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init() {
|
||||
Path dir = Paths.get(moduleSrcConfigFile.getFilePath());
|
||||
if (!createConfigDirectory(dir)) {
|
||||
return false;
|
||||
return fail("创建Config目录失败");
|
||||
}
|
||||
if (!createConfigFile(dir)) {
|
||||
return false;
|
||||
return fail("创建Config文件失败");
|
||||
}
|
||||
if (!createModuleDirectory()) {
|
||||
return false;
|
||||
return fail("创建模块文件夹失败");
|
||||
}
|
||||
return true;
|
||||
return success();
|
||||
}
|
||||
|
||||
private boolean createConfigDirectory(Path dir) {
|
||||
try {
|
||||
if (!Files.exists(dir)) {
|
||||
Files.createDirectory(dir);
|
||||
logger.info("创建 config 文件夹成功 √ ");
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建配置文件夹失败");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -56,10 +56,8 @@ public class ModuleSrcConfigFileInit extends CommonInitMachine {
|
||||
try {
|
||||
if (!Files.exists(path)) {
|
||||
JsonFileUtil.writeJsonFile(path.toString(),moduleSrcConfigFile.packageConfig());
|
||||
logger.info("创建 {} 配置文件成功 √",moduleSrcConfigFile.getFileName());
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建配置文件失败");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -76,10 +74,8 @@ public class ModuleSrcConfigFileInit extends CommonInitMachine {
|
||||
try {
|
||||
if (!FileUtil.isFileExist(src.getSrc())) {
|
||||
Files.createDirectory(Path.of(src.getSrc()));
|
||||
logger.info("创建 {} 模块文件夹成功 √ ",src.getSrc());
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建 {} 模块文件夹失败 ×",src.getSrc());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user