支持部分配置在运行时动态改变

This commit is contained in:
陈精华
2019-04-11 14:45:22 +08:00
committed by kl
parent bf08c2c26f
commit 5af3a97720
7 changed files with 129 additions and 10 deletions

View File

@ -0,0 +1,38 @@
package cn.keking.config;
/**
* @auther: chenjh
* @time: 2019/4/10 17:22
* @description
*/
public class ConfigConstants {
private static String[] simText = {};
private static String[] media = {};
private static String convertedFileCharset;
public static String[] getSimText() {
return simText;
}
public static void setSimText(String[] simText) {
ConfigConstants.simText = simText;
}
public static String[] getMedia() {
return media;
}
public static void setMedia(String[] media) {
ConfigConstants.media = media;
}
public static String getConvertedFileCharset() {
return convertedFileCharset;
}
public static void setConvertedFileCharset(String convertedFileCharset) {
ConfigConstants.convertedFileCharset = convertedFileCharset;
}
}

View File

@ -0,0 +1,69 @@
package cn.keking.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* @auther: chenjh
* @time: 2019/4/10 16:16
* @description 每隔1s读取并更新一次配置文件
*/
@Component
public class ConfigRefreshComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigRefreshComponent.class);
@PostConstruct
void refresh() {
Thread configRefreshThread = new Thread(new ConfigRefreshThread());
configRefreshThread.start();
}
class ConfigRefreshThread implements Runnable {
@Override
public void run() {
try {
Properties properties = new Properties();
String userDir = System.getProperty("user.dir");
Properties properties1 = System.getProperties();
if (userDir.endsWith("bin")) {
userDir = userDir.substring(0, userDir.length() - 4);
}
String separator = java.io.File.separator;
String configFilePath = userDir + separator + "conf" + separator + "application.properties";
File file = new File(configFilePath);
if (!file.exists()) {
configFilePath = userDir + separator + "jodconverter-web" + separator + "src" + separator + "main" + separator + "conf" + separator + "application.properties";
}
String text = null;
String media = null;
String convertedFileCharset = null;
String[] textArray = {};
String[] mediaArray = {};
while (true) {
BufferedReader bufferedReader = new BufferedReader(new FileReader(configFilePath));
properties.load(bufferedReader);
text = properties.get("simText") == null ? "" : properties.get("simText").toString();
media = properties.get("media") == null ? "" : properties.get("media").toString();
convertedFileCharset = properties.get("converted.file.charset") == null ? "" : properties.get("converted.file.charset").toString();
textArray = text.split(",");
mediaArray = media.split(",");
ConfigConstants.setSimText(textArray);
ConfigConstants.setMedia(mediaArray);
ConfigConstants.setConvertedFileCharset(convertedFileCharset);
Thread.sleep(1000L);
}
} catch (IOException | InterruptedException e) {
LOGGER.error("读取配置文件异常:{}", e);
}
}
}
}

View File

@ -1,5 +1,6 @@
package cn.keking.utils;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute;
import cn.keking.model.FileType;
import cn.keking.service.cache.CacheService;
@ -63,6 +64,12 @@ public class FileUtils {
* @return
*/
public FileType typeFromUrl(String url) {
if (ConfigConstants.getSimText() != null && ConfigConstants.getSimText().length > 0) {
simText = ConfigConstants.getSimText();
}
if (ConfigConstants.getMedia() != null && ConfigConstants.getMedia().length > 0) {
media = ConfigConstants.getMedia();
}
String nonPramStr = url.substring(0, url.indexOf("?") != -1 ? url.indexOf("?") : url.length());
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
@ -217,6 +224,9 @@ public class FileUtils {
*/
public void doActionConvertedFile(String outFilePath) {
StringBuffer sb = new StringBuffer();
if(ConfigConstants.getConvertedFileCharset() != null) {
charset = ConfigConstants.getConvertedFileCharset();
}
try (InputStream inputStream = new FileInputStream(outFilePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))){
String line;