From 99e97cbf35b2f69f0ef3fa9a00605373974033bd Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Thu, 5 Aug 2021 22:59:43 +0800 Subject: [PATCH] fix auto import local node Time Too soon --- .../java/io/jpom/system/init/CheckPath.java | 2 +- .../jpom/system/init/AutoImportLocalNode.java | 130 +++++++++--------- 2 files changed, 65 insertions(+), 67 deletions(-) diff --git a/modules/common/src/main/java/io/jpom/system/init/CheckPath.java b/modules/common/src/main/java/io/jpom/system/init/CheckPath.java index e485ac4f7..589b061c4 100644 --- a/modules/common/src/main/java/io/jpom/system/init/CheckPath.java +++ b/modules/common/src/main/java/io/jpom/system/init/CheckPath.java @@ -23,7 +23,7 @@ import java.util.Map; * @author jiangzeyin * @date 2019/3/26 */ -@PreLoadClass +@PreLoadClass(value = Integer.MIN_VALUE) public class CheckPath { /** * 待检查的类 diff --git a/modules/server/src/main/java/io/jpom/system/init/AutoImportLocalNode.java b/modules/server/src/main/java/io/jpom/system/init/AutoImportLocalNode.java index de47f1b9d..ca3016382 100644 --- a/modules/server/src/main/java/io/jpom/system/init/AutoImportLocalNode.java +++ b/modules/server/src/main/java/io/jpom/system/init/AutoImportLocalNode.java @@ -20,8 +20,6 @@ import io.jpom.system.ConfigBean; import io.jpom.system.ServerConfigBean; import io.jpom.util.JsonFileUtil; import io.jpom.util.JvmUtil; -import sun.jvmstat.monitor.MonitoredVm; -import sun.jvmstat.monitor.VmIdentifier; import java.io.File; import java.util.List; @@ -35,71 +33,71 @@ import java.util.List; @PreLoadClass public class AutoImportLocalNode { - private static final String AGENT_MAIN_CLASS = "io.jpom.JpomAgentApplication"; - private static NodeService nodeService; + private static final String AGENT_MAIN_CLASS = "io.jpom.JpomAgentApplication"; + private static NodeService nodeService; - @PreLoadMethod - private static void install() { - File file = FileUtil.file(ConfigBean.getInstance().getDataPath(), ServerConfigBean.INSTALL); - if (file.exists()) { - return; - } - JSONObject jsonObject = new JSONObject(); - jsonObject.put("installId", IdUtil.fastSimpleUUID()); - jsonObject.put("installTime", DateTime.now().toString()); - jsonObject.put("desc", "请勿删除此文件,服务端安装id和插件端互通关联"); - JsonFileUtil.saveJson(file.getAbsolutePath(), jsonObject); - } + @PreLoadMethod + private static void install() { + File file = FileUtil.file(ConfigBean.getInstance().getDataPath(), ServerConfigBean.INSTALL); + if (file.exists()) { + return; + } + JSONObject jsonObject = new JSONObject(); + jsonObject.put("installId", IdUtil.fastSimpleUUID()); + jsonObject.put("installTime", DateTime.now().toString()); + jsonObject.put("desc", "请勿删除此文件,服务端安装id和插件端互通关联"); + JsonFileUtil.saveJson(file.getAbsolutePath(), jsonObject); + } - @PreLoadMethod - private static void loadAgent() { - nodeService = SpringUtil.getBean(NodeService.class); - List list = nodeService.list(); - if (list != null && !list.isEmpty()) { - return; - } - // - try { - List monitoredVms = JvmUtil.listMainClass(AGENT_MAIN_CLASS); - monitoredVms.forEach(monitoredVm -> { - VmIdentifier vmIdentifier = monitoredVm.getVmIdentifier(); - findPid(vmIdentifier.getUserInfo()); - }); - } catch (Exception e) { - DefaultSystemLog.getLog().error("自动添加本机节点错误", e); - } - } + @PreLoadMethod + private static void loadAgent() { + nodeService = SpringUtil.getBean(NodeService.class); + List list = nodeService.list(); + if (list != null && !list.isEmpty()) { + return; + } + // + try { + List monitoredVms = JvmUtil.listMainClass(AGENT_MAIN_CLASS); + monitoredVms.forEach(monitoredVm -> { + sun.jvmstat.monitor.VmIdentifier vmIdentifier = monitoredVm.getVmIdentifier(); + findPid(vmIdentifier.getUserInfo()); + }); + } catch (Exception e) { + DefaultSystemLog.getLog().error("自动添加本机节点错误", e); + } + } - private static void findPid(String pid) { - File file = ConfigBean.getInstance().getApplicationJpomInfo(Type.Agent); - if (!file.exists() || file.isDirectory()) { - return; - } - // 比较进程id - String json = FileUtil.readString(file, CharsetUtil.CHARSET_UTF_8); - JpomManifest jpomManifest = JSONObject.parseObject(json, JpomManifest.class); - if (!pid.equals(String.valueOf(jpomManifest.getPid()))) { - return; - } - // 判断自动授权文件是否存在 - String path = ConfigBean.getInstance().getAgentAutoAuthorizeFile(jpomManifest.getDataPath()); - if (!FileUtil.exist(path)) { - return; - } - json = FileUtil.readString(path, CharsetUtil.CHARSET_UTF_8); - AgentAutoUser autoUser = JSONObject.parseObject(json, AgentAutoUser.class); - // 判断授权信息 - // - NodeModel nodeModel = new NodeModel(); - nodeModel.setUrl(StrUtil.format("127.0.0.1:{}", jpomManifest.getPort())); - nodeModel.setName("本机"); - nodeModel.setId("localhost"); - // - nodeModel.setLoginPwd(autoUser.getAgentPwd()); - nodeModel.setLoginName(autoUser.getAgentName()); - // - nodeModel.setOpenStatus(true); - nodeService.addItem(nodeModel); - Console.log("自动添加本机节点成功:" + nodeModel.getId()); - } + private static void findPid(String pid) { + File file = ConfigBean.getInstance().getApplicationJpomInfo(Type.Agent); + if (!file.exists() || file.isDirectory()) { + return; + } + // 比较进程id + String json = FileUtil.readString(file, CharsetUtil.CHARSET_UTF_8); + JpomManifest jpomManifest = JSONObject.parseObject(json, JpomManifest.class); + if (!pid.equals(String.valueOf(jpomManifest.getPid()))) { + return; + } + // 判断自动授权文件是否存在 + String path = ConfigBean.getInstance().getAgentAutoAuthorizeFile(jpomManifest.getDataPath()); + if (!FileUtil.exist(path)) { + return; + } + json = FileUtil.readString(path, CharsetUtil.CHARSET_UTF_8); + AgentAutoUser autoUser = JSONObject.parseObject(json, AgentAutoUser.class); + // 判断授权信息 + // + NodeModel nodeModel = new NodeModel(); + nodeModel.setUrl(StrUtil.format("127.0.0.1:{}", jpomManifest.getPort())); + nodeModel.setName("本机"); + nodeModel.setId("localhost"); + // + nodeModel.setLoginPwd(autoUser.getAgentPwd()); + nodeModel.setLoginName(autoUser.getAgentName()); + // + nodeModel.setOpenStatus(true); + nodeService.addItem(nodeModel); + Console.log("自动添加本机节点成功:" + nodeModel.getId()); + } }