|
| 1 | +package cn.keking.config; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.stereotype.Component; |
| 6 | + |
| 7 | +import javax.annotation.PostConstruct; |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Properties; |
| 13 | + |
| 14 | +/** |
| 15 | + * @auther: chenjh |
| 16 | + * @time: 2019/4/10 16:16 |
| 17 | + * @description 每隔1s读取并更新一次配置文件 |
| 18 | + */ |
| 19 | +@Component |
| 20 | +public class ConfigRefreshComponent { |
| 21 | + |
| 22 | + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigRefreshComponent.class); |
| 23 | + |
| 24 | + @PostConstruct |
| 25 | + void refresh() { |
| 26 | + Thread configRefreshThread = new Thread(new ConfigRefreshThread()); |
| 27 | + configRefreshThread.start(); |
| 28 | + } |
| 29 | + |
| 30 | + class ConfigRefreshThread implements Runnable { |
| 31 | + @Override |
| 32 | + public void run() { |
| 33 | + try { |
| 34 | + Properties properties = new Properties(); |
| 35 | + String userDir = System.getProperty("user.dir"); |
| 36 | + Properties properties1 = System.getProperties(); |
| 37 | + if (userDir.endsWith("bin")) { |
| 38 | + userDir = userDir.substring(0, userDir.length() - 4); |
| 39 | + } |
| 40 | + String separator = java.io.File.separator; |
| 41 | + String configFilePath = userDir + separator + "conf" + separator + "application.properties"; |
| 42 | + File file = new File(configFilePath); |
| 43 | + if (!file.exists()) { |
| 44 | + configFilePath = userDir + separator + "jodconverter-web" + separator + "src" + separator + "main" + separator + "conf" + separator + "application.properties"; |
| 45 | + } |
| 46 | + String text = null; |
| 47 | + String media = null; |
| 48 | + String convertedFileCharset = null; |
| 49 | + String[] textArray = {}; |
| 50 | + String[] mediaArray = {}; |
| 51 | + while (true) { |
| 52 | + BufferedReader bufferedReader = new BufferedReader(new FileReader(configFilePath)); |
| 53 | + properties.load(bufferedReader); |
| 54 | + text = properties.get("simText") == null ? "" : properties.get("simText").toString(); |
| 55 | + media = properties.get("media") == null ? "" : properties.get("media").toString(); |
| 56 | + convertedFileCharset = properties.get("converted.file.charset") == null ? "" : properties.get("converted.file.charset").toString(); |
| 57 | + textArray = text.split(","); |
| 58 | + mediaArray = media.split(","); |
| 59 | + ConfigConstants.setSimText(textArray); |
| 60 | + ConfigConstants.setMedia(mediaArray); |
| 61 | + ConfigConstants.setConvertedFileCharset(convertedFileCharset); |
| 62 | + Thread.sleep(1000L); |
| 63 | + } |
| 64 | + } catch (IOException | InterruptedException e) { |
| 65 | + LOGGER.error("读取配置文件异常:{}", e); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments