1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| protected void connectServer(String host, int port, final String projCode, final String profile) { final String clientMsg = "superdiamond," + projCode + "," + profile; try { client = new Netty4Client(host, port, new ClientChannelInitializer(), clientMsg);
if (client.isConnected()) { client.sendMessage(clientMsg); String message = client.receiveMessage();
if (message != null) { String versionStr = message.substring(0, message.indexOf("\r\n")); LOGGER.info("加载远程配置信息,项目编码:{},Profile:{}, Version:{}", projCode, profile, versionStr.split(" = ")[1]);
FileUtils.saveData(projCode, profile, message); load(new StringReader(message), false); } } else { String message = FileUtils.readConfigFromLocal(projCode, profile); if (message != null) { String versionStr = message.substring(0, message.indexOf("\r\n")); LOGGER.info("加载本地备份配置信息,项目编码:{},Profile:{}, Version:{}", projCode, profile, versionStr.split(" = ")[1]);
load(new StringReader(message), false); } else throw new ConfigurationRuntimeException( "本地没有备份配置数据,PropertiesConfiguration 初始化失败。"); }
reloadExecutorService.submit(new Runnable() { private final String projCodeString = projCode; private final String profileString = profile; @Override public void run() { while(reloadable) { try { if(client.isConnected()) { String message = client.receiveMessage(); if(message != null) { String versionStr = message.substring(0, message.indexOf("\r\n")); LOGGER.info("==================== 重新加载配置信息,项目编码:{},Profile:{}, Version:{}", projCodeString, profileString, versionStr.split(" = ")[1]); FileUtils.saveData(projCodeString, profileString, message); load(new StringReader(message), true); } } else { TimeUnit.SECONDS.sleep(1); } } catch(Exception e) { e.printStackTrace(); } } } }); } catch (Exception e) { if (client != null) { client.close(); } throw new ConfigurationRuntimeException(e.getMessage(), e); } }
|