v1.0.3一些bug修改和一些模块重构优化

This commit is contained in:
userA
2023-04-26 02:50:00 +08:00
parent 1233ca8a00
commit 3f37176772
22 changed files with 416 additions and 24 deletions

View File

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.example.common.ConfigFile;
import org.example.common.FileType;
import org.example.exception.FileCacheException;
import org.example.util.JsonFileUtil;
import org.example.util.TimeUtil;
@@ -71,7 +72,7 @@ public class FileCache <T extends ConfigFile>{
this.logger = LoggerFactory.getLogger("FileCache:"+this.configFile.getFileName());
this.autoSyncTime = autoSyncTime;
if(!load(getFileName())){
if(!load(getFullFilePath())){
throw new FileCacheException("FileCache Init Error,please Check if your path is correct");
}
@@ -240,7 +241,7 @@ public class FileCache <T extends ConfigFile>{
*/
public void forceSync(){
if(writeByte.get()==0){
logger.debug("未发生版本变化");
logger.info("未发生版本变化");
return;
}
clearWriteBytes();
@@ -248,7 +249,7 @@ public class FileCache <T extends ConfigFile>{
try {
syncChannel.put(temp);
} catch (InterruptedException e) {
logger.debug("自动刷入失败");
logger.error("自动刷入失败");
}
}
@@ -258,25 +259,40 @@ public class FileCache <T extends ConfigFile>{
*/
private boolean sync(ConcurrentHashMap<String,Object> take){
configFile.updateConfigTime(); //刷新配置文件刷入时间
String dir = getFileName();
String dir = getFullFilePath();
configFile.onlyUpdateTime(take);
File file = JsonFileUtil.writeJsonFile(dir, take);
logger.debug("正在写入{}新版本",dir);
return Objects.isNull(file);
}
public BlockingQueue getFileChannel(){
return this.syncChannel;
}
public String getFileName(){
public String getFullFilePath(){
return Paths.get(this.configFile.getFilePath(), this.configFile.getFileName()).toString();
}
public String getFilePath(){
return this.configFile.getFilePath();
}
public String getFileName(){
return this.configFile.getFileName();
}
public long getSyncTime(){
return this.autoSyncTime;
}
public FileType getFileType(){
return this.configFile.getFileType();
}
class SyncMan implements Runnable{
@Override
@@ -300,7 +316,7 @@ public class FileCache <T extends ConfigFile>{
@Override
public boolean equals(Object obj) {
if(obj instanceof FileCache){
if(((FileCache) obj).getFileName().equals(this.getFileName())){
if(((FileCache) obj).getFullFilePath().equals(this.getFullFilePath())){
return true;
}else if(obj.hashCode() == this.hashCode()){
return true;

View File

@@ -75,6 +75,10 @@ public class FileCacheManager {
return false;
}
public List<FileCache> getRunnableFileCaches(){
return this.fileCaches;
}
class Watcher implements Runnable{
@Override
@@ -85,7 +89,7 @@ public class FileCacheManager {
BlockingQueue fileChannel = cache.getFileChannel();
if(fileChannel.isEmpty()){
if(cache.needAutoSync()){
logger.debug("检测到需要强制刷新的文件 {}",cache.getFileName());
logger.info("检测到需要强制刷新的文件 {}",cache.getFileName());
autoSyncer.submit(new AutoSyncer(cache));
}
}

View File

@@ -0,0 +1,16 @@
package org.example.init;
import org.example.cache.FileCacheManagerInstance;
/**
* @author Genius
* @date 2023/04/26 02:09
**/
public class FileCacheManagerInit implements InitMachine{
@Override
public boolean init() {
FileCacheManagerInstance.getInstance().start();
return true;
}
}

View File

@@ -1,6 +1,7 @@
package org.example.pojo.configfile;
import org.example.common.ConfigFile;
import org.example.common.FileType;
import org.example.constpool.ConstPool;
import java.util.Map;
@@ -44,7 +45,7 @@ public class ModuleSrcConfigFile extends ConfigFile<Map<String, ModuleSrcConfigF
public ModuleSrcConfigFile() {
super("E:\\Project\\ChopperBot\\config\\"
, "moduleConfig.json"
, config);
, config, FileType.CHOPPER_BOT);
}
public Map<String,Object> packageConfig() {

View File

@@ -0,0 +1,20 @@
package org.example.pojo.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
/**
* @author Genius
* @date 2023/04/26 02:07
**/
@Data
@Component
@AllArgsConstructor
@NoArgsConstructor
public class ConfigVO {
private String fileName;
private String filePath;
private String moduleType;
}

View File

@@ -0,0 +1,19 @@
package org.example.service;
import org.example.pojo.vo.ConfigVO;
import java.util.List;
/**
* @author Genius
* @date 2023/04/26 00:59
**/
public interface FileService {
//获取所有模块
List<String> getAllModule();
//获取所有文件
List<ConfigVO> getAllConfigs();
}

View File

@@ -0,0 +1,49 @@
package org.example.service.impl;
import org.example.cache.FileCache;
import org.example.cache.FileCacheManager;
import org.example.cache.FileCacheManagerInstance;
import org.example.common.ConfigFile;
import org.example.common.FileType;
import org.example.pojo.vo.ConfigVO;
import org.example.service.FileService;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author Genius
* @date 2023/04/26 01:01
**/
@Service
public class FileServiceImpl implements FileService {
@Override
public List<String> getAllModule() {
List<String> moduleList = new ArrayList<>();
for (FileType value : FileType.values()) {
if(!value.equals(FileType.COMMON)){
moduleList.add(value.getName());
}
}
return moduleList;
}
@Override
public List<ConfigVO> getAllConfigs() {
List<ConfigVO> configVOList = new ArrayList<>();
List<FileCache> runnableFileCaches = FileCacheManagerInstance.getInstance().getRunnableFileCaches();
for (FileCache runnableFileCache : runnableFileCaches) {
if (runnableFileCache.getFileType().equals(FileType.COMMON)) {
continue;
}
configVOList.add(new ConfigVO(runnableFileCache.getFileName()
,runnableFileCache.getFullFilePath()
,runnableFileCache.getFileType().getName()));
}
return configVOList;
}
}

View File

@@ -1,4 +1,65 @@
{
"data":[],
"updateTime":"2023-04-25 23:46:35"
}
"data":[
{
"age":0,
"major":"major0",
"name":"0",
"school":"jsu0"
},
{
"age":1,
"major":"major1",
"name":"1",
"school":"jsu1"
},
{
"age":2,
"major":"major2",
"name":"2",
"school":"jsu2"
},
{
"age":3,
"major":"major3",
"name":"3",
"school":"jsu3"
},
{
"age":4,
"major":"major4",
"name":"4",
"school":"jsu4"
},
{
"age":5,
"major":"major5",
"name":"5",
"school":"jsu5"
},
{
"age":6,
"major":"major6",
"name":"6",
"school":"jsu6"
},
{
"age":7,
"major":"major7",
"name":"7",
"school":"jsu7"
},
{
"age":8,
"major":"major8",
"name":"8",
"school":"jsu8"
},
{
"age":9,
"major":"major9",
"name":"9",
"school":"jsu9"
}
],
"updateTime":"2023-04-26 00:24:47"
}