From 576099c38d32e2e8e1eadb9246e8e742358bec31 Mon Sep 17 00:00:00 2001 From: "boxing.yi" Date: Tue, 4 Aug 2020 00:17:00 +0800 Subject: [PATCH] =?UTF-8?q?#176=20loadAgent=E7=9A=84=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vip/vjtools/vjtop/data/jmx/JmxClient.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java b/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java index eaa7793b..95d1813f 100755 --- a/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java +++ b/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java @@ -54,6 +54,8 @@ import com.sun.management.HotSpotDiagnosticMXBean; import com.sun.management.OperatingSystemMXBean; import com.sun.management.ThreadMXBean; +import com.sun.tools.attach.AgentInitializationException; +import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.VirtualMachine; import com.vip.vjtools.vjtop.util.Utils; @@ -238,7 +240,14 @@ public String attachToGetConnectorAddress() throws Exception { String home = vm.getSystemProperties().getProperty("java.home"); int version = Utils.getJavaMajorVersion(vm.getSystemProperties().getProperty("java.specification.version")); - if (version <= 8) { + if (version >= 8) { + vm.startLocalManagementAgent(); + agentProps = vm.getAgentProperties(); + address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); + if (address != null) { + return address; + } + } else { // Normally in ${java.home}/jre/lib/management-agent.jar but might // be in ${java.home}/lib in build environments. String agentPath = home + File.separator + "jre" + File.separator + "lib" + File.separator @@ -252,7 +261,21 @@ public String attachToGetConnectorAddress() throws Exception { } } agentPath = f.getCanonicalPath(); - vm.loadAgent(agentPath, "com.sun.management.jmxremote"); + try { + vm.loadAgent(agentPath, "com.sun.management.jmxremote"); + } catch (AgentLoadException x) { + // 高版本 attach 低版本jdk 抛异常:com.sun.tools.attach.AgentLoadException: 0,实际上是成功的; + // 根因: HotSpotVirtualMachine.loadAgentLibrary 高版本jdk实现不一样了 + if (!"0".equals(x.getMessage())) { + IOException ioe = new IOException(x.getMessage()); + ioe.initCause(x); + throw ioe; + } + } catch (AgentInitializationException x) { + IOException ioe = new IOException(x.getMessage()); + ioe.initCause(x); + throw ioe; + } // 4. 再次获取connector address @@ -262,9 +285,6 @@ public String attachToGetConnectorAddress() throws Exception { if (address == null) { throw new IOException("Fails to find connector address"); } - } else { - // for jdk9 or later - vm.startLocalManagementAgent(); } agentProps = vm.getAgentProperties();