Commit 3610c118 LN

韩华配置修改时自动重连

1 个父辈 6581cb82
......@@ -28,13 +28,17 @@ public class EquipConfigInfo {
try {
if (apiConfigMap != null) {
Object result = apiConfigMap.get(key);
if (result != null) {
if (result instanceof String && defValue instanceof Integer) {
return (T) (Integer) Integer.parseInt((String) result);
}
else if (result != null) {
return (T) result;
}
}
} catch (Exception exception) {
log.error("GetConfigValue 出错: type=" + equipType + ",key=" + key + ", error =" + exception.toString());
}
if (defValue != null) {
return (T) defValue;
......@@ -42,4 +46,5 @@ public class EquipConfigInfo {
return null;
}
}
......@@ -98,41 +98,30 @@ public class TMSCommunicator implements WsMsgReceivedListener {
@Autowired
private IStoragePosManager storagePosManager;
// @Autowired
// private IDataLogManager dataLogManager;
// @Autowired
// private IComponentManager componentManager;
@Autowired
private TMSUtil tmsUtil;
// private static int gRequestID = 1000;
// public static int GetRequestID()
// {
// return gRequestID++;
// }
private boolean needCheck=false;
@PostConstruct
public void init() {
EquipConfigInfo equipConfigInfo=equipConfigUtil.getConfigCache(EquipmentType.HANWHA.name());
EquipConfigInfo equipConfigInfo = equipConfigUtil.getConfigCache(EquipmentType.HANWHA.name());
// host = dataCache.getConfigCache("hanwha.host", host);
// String webPortStr = dataCache.getConfigCache("hanwha.webPort", configWebPort);
// String apiPortStr = dataCache.getConfigCache("hanwha.apiPort", configApiPort);
host=equipConfigInfo.GetConfigValue("host","");
webPort = equipConfigInfo.GetConfigValue("webPort",0);
apiPort = equipConfigInfo.GetConfigValue("apiPort",0);
enableApi=equipConfigInfo.isEnableApi();
apiName = dataCache.getConfigCache("api.name",apiName);
boolean enable= apiName != null && apiName.equalsIgnoreCase("hanwha") ;
if(!enable){
log.info("apiName=" + apiName+",不需要连接");
}else if(!enableApi){
log.info("enableApi=" + enableApi+",不需要连接");
}
else if (ObjectUtil.isEmpty(host) || webPort==0 || apiPort==0) {
host = equipConfigInfo.GetConfigValue("host", "");
webPort = equipConfigInfo.GetConfigValue("webPort", 0);
apiPort = equipConfigInfo.GetConfigValue("apiPort", 0);
enableApi = equipConfigInfo.isEnableApi();
apiName = dataCache.getConfigCache("api.name", apiName);
updateServerInfo(host, webPort, apiPort,enableApi);
boolean enable = apiName != null && apiName.equalsIgnoreCase("hanwha");
if (!enable) {
log.info("apiName=" + apiName + ",不需要连接");
} else if (!enableApi) {
log.info("enableApi=" + enableApi + ",不需要连接");
} else if (ObjectUtil.isEmpty(host) || webPort == 0 || apiPort == 0) {
// else if (ObjectUtil.isEmpty(host) || ObjectUtil.isEmpty(webPortStr) || ObjectUtil.isEmpty((apiPortStr))) {
log.info("配置不完整,不需要连接");
} else {
......@@ -140,7 +129,6 @@ public class TMSCommunicator implements WsMsgReceivedListener {
// apiPort = Integer.parseInt(apiPortStr);
String apiName = dataCache.getConfigCache("api.name", "");
if (apiName != null && apiName.equalsIgnoreCase("hanwha")) {
updateServerInfo(host, webPort, apiPort);
Thread closeTask = new Thread(new Runnable() {
@Override
public void run() {
......@@ -148,39 +136,118 @@ public class TMSCommunicator implements WsMsgReceivedListener {
Thread.sleep(20000);
start();
Thread.sleep(5000);
needCheck=true;
}catch (Exception e){
needCheck = true;
} catch (Exception e) {
log.error("开始连接webService出错:" + e.getMessage());
}
}
} );
});
closeTask.start();
// StartCheckRun();
}
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
@Override
public void run() {
log.info("启动 webService 状态检查");
while(true) {
try {
if(waitSync&&System.currentTimeMillis()>syncTime){
SyncData();
}
TimeUnit.SECONDS.sleep(6);
if (needCheck) {
stateCheck();
}
} catch (Exception e) {
log.error("webService" + e.getMessage());
StartCheckRun();
}
private void StartCheckRun() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
@Override
public void run() {
log.info("启动 webService 状态检查");
while (true) {
try {
if (isEnable() && apiName != null && apiName.equalsIgnoreCase("hanwha")) {
if (waitSync && System.currentTimeMillis() > syncTime) {
SyncData();
}
TimeUnit.SECONDS.sleep(6);
if (needCheck) {
stateCheck();
}
} else {
if (isConnected()) {
//当前需要禁用webSocket连接
close();
}
TimeUnit.SECONDS.sleep(6);
}
EquipConfigInfo equipConfigInfo = equipConfigUtil.getConfigCache(EquipmentType.HANWHA.name());
ResetConfig(equipConfigInfo);
} catch (Exception e) {
log.error("webService" + e.getMessage());
}
});
// start();
}
}
});
}
private void ResetConfig(EquipConfigInfo nconfig) {
try {
//重新设置韩华接口时调用
String nhost = nconfig.GetConfigValue("host", "");
int nwebPort = nconfig.GetConfigValue("webPort", 0);
int napiPort = nconfig.GetConfigValue("apiPort", 0);
if (enableApi == nconfig.enableApi && nhost == host && webPort == nwebPort && napiPort == apiPort) {
return;
}
if (nconfig.enableApi) {
//当前启用
if (enableApi) {
//如果ip端口不一致,需要关闭webSocket重新连接
String nwsURL = String.format("ws://%s:%d/", nhost, nwebPort);
if (!wsURL.equals(nwsURL)) {
log.info("更改韩华配置,wsURL = " + wsURL + ",需要更改为:" + nwsURL + ",关闭webSocket连接重新连接");
updateServerInfo(nhost, nwebPort, napiPort, nconfig.enableApi);
close();
} else {
updateServerInfo(nhost, nwebPort, napiPort, nconfig.enableApi);
}
} else {
//当前禁用
updateServerInfo(nhost, nwebPort, napiPort, nconfig.enableApi);
log.info("更改韩华配置,之前禁用连接,现在启用连接,开始启动webSocket连接");
Thread closeTask = new Thread(new Runnable() {
@Override
public void run() {
if (isEnable()) {
try {
Thread.sleep(5000);
start();
Thread.sleep(5000);
needCheck = true;
} catch (Exception e) {
log.error("开始连接webService出错:" + e.getMessage());
}
}
}
});
closeTask.start();
}
} else {
//如果原来启用当前禁用
if (enableApi) {
log.info("更改韩华配置,之前启用连接,现在禁用连接,关闭webSocket连接,更新配置");
updateServerInfo(nhost, nwebPort, napiPort, nconfig.enableApi);
close();//关闭连接
}
}
} catch (Exception exception) {
log.error("ResetConfig error :" + exception.toString());
}
}
private void stateCheck(){
if(websocket.isOpen()||websocket.isConnecting()){
......@@ -190,11 +257,11 @@ public class TMSCommunicator implements WsMsgReceivedListener {
}
}
private void updateServerInfo(String serverHost, int wport, int aport) {
private void updateServerInfo(String serverHost, int wport, int aport,boolean enableApiParam) {
host = serverHost;
webPort = wport;
apiPort = aport;
enableApi=enableApiParam;
wsURL = String.format("ws://%s:%d/", serverHost, wport);
apiURL = String.format("http://%s:%d/", serverHost, aport);
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!