代码兼容jdk11

This commit is contained in:
bwcx_jzy 2019-08-13 10:22:30 +08:00
parent b0405c58bd
commit 28bc40aa38
2 changed files with 36 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package cn.keepbx.util;
import cn.hutool.cache.impl.TimedCache; import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil; import cn.hutool.system.SystemUtil;
import cn.jiangzeyin.common.DefaultSystemLog; import cn.jiangzeyin.common.DefaultSystemLog;
@ -10,7 +11,6 @@ import cn.keepbx.jpom.system.JpomRuntimeException;
import com.sun.management.OperatingSystemMXBean; import com.sun.management.OperatingSystemMXBean;
import com.sun.tools.attach.*; import com.sun.tools.attach.*;
import sun.jvmstat.monitor.*; import sun.jvmstat.monitor.*;
import sun.management.ConnectorAddressLink;
import javax.management.MBeanServerConnection; import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnector;
@ -21,6 +21,7 @@ import java.io.IOException;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean; import java.lang.management.MemoryMXBean;
import java.lang.management.ThreadMXBean; import java.lang.management.ThreadMXBean;
import java.lang.reflect.Method;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -33,6 +34,26 @@ import java.util.concurrent.TimeUnit;
*/ */
public class JvmUtil { public class JvmUtil {
private static Method importFrom = null;
static {
int v = StrUtil.compareVersion(SystemUtil.getJavaInfo().getVersion(), "11.0");
Class cls = null;
try {
if (v >= 0) {
cls = Class.forName("jdk.internal.agent.ConnectorAddressLink");
} else {
cls = Class.forName("sun.management.ConnectorAddressLink");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (cls != null) {
importFrom = ReflectUtil.getMethod(cls, "importFrom", Integer.class);
}
}
/** /**
* 旧版jpom进程标记 * 旧版jpom进程标记
*/ */
@ -141,13 +162,13 @@ public class JvmUtil {
* @throws AgentLoadException 插件加载 * @throws AgentLoadException 插件加载
* @throws AgentInitializationException 插件初始化 * @throws AgentInitializationException 插件初始化
*/ */
private static JMXServiceURL getJMXServiceURL(VirtualMachine virtualMachine) throws IOException, AgentLoadException, AgentInitializationException { private static JMXServiceURL getJMXServiceURL(VirtualMachine virtualMachine) throws IOException, AgentLoadException, AgentInitializationException, ClassNotFoundException {
String address = virtualMachine.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress"); String address = virtualMachine.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
if (address != null) { if (address != null) {
return new JMXServiceURL(address); return new JMXServiceURL(address);
} }
int pid = Convert.toInt(virtualMachine.id()); int pid = Convert.toInt(virtualMachine.id());
address = ConnectorAddressLink.importFrom(pid); address = importFrom(pid);
if (address != null) { if (address != null) {
return new JMXServiceURL(address); return new JMXServiceURL(address);
} }
@ -160,6 +181,13 @@ public class JvmUtil {
return null; return null;
} }
public static String importFrom(int pid) {
if (importFrom == null) {
throw new JpomRuntimeException("jdk 环境不正常没有找到ConnectorAddressLink");
}
return ReflectUtil.invoke(null, importFrom, pid);
}
/** /**
* 获取当前系统运行的java 程序个数 * 获取当前系统运行的java 程序个数
* *

View File

@ -1,9 +1,9 @@
import cn.hutool.system.SystemUtil; import cn.hutool.system.SystemUtil;
import cn.keepbx.util.JvmUtil;
import com.sun.tools.attach.AgentInitializationException; import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException; import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine; import com.sun.tools.attach.VirtualMachine;
import sun.management.ConnectorAddressLink;
import java.io.IOException; import java.io.IOException;
@ -11,13 +11,13 @@ import java.io.IOException;
* Created by jiangzeyin on 2019/3/20. * Created by jiangzeyin on 2019/3/20.
*/ */
public class TestJavaInfo { public class TestJavaInfo {
public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException { public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException, ClassNotFoundException {
System.out.println(SystemUtil.getJavaRuntimeInfo().getHomeDir()); System.out.println(SystemUtil.getJavaRuntimeInfo().getHomeDir());
VirtualMachine virtualMachine = VirtualMachine.attach("16772"); // VirtualMachine virtualMachine = VirtualMachine.attach("16772");
// String agent = StrUtil.format("{}{}lib{}management-agent.jar", SystemUtil.getJavaRuntimeInfo().getHomeDir(), File.separator, File.separator); // String agent = StrUtil.format("{}{}lib{}management-agent.jar", SystemUtil.getJavaRuntimeInfo().getHomeDir(), File.separator, File.separator);
// virtualMachine.loadAgent(agent); // virtualMachine.loadAgent(agent);
// virtualMachine.loadAgent(agent); // virtualMachine.loadAgent(agent);
ConnectorAddressLink.export(String.valueOf(12044)); JvmUtil.importFrom(7292);
System.out.println(ConnectorAddressLink.importFrom(12044)); System.out.println(JvmUtil.importFrom(7292));
} }
} }