jdk文档上描述ManagementFactory:
ManagementFactory类是用于获取Java平台的受管Bean的工厂类。 该类由静态方法组成,每个方法返回一个或多个表示Java虚拟机组件的管理接口的平台MXBeans
一、打印系统信息
代码:
public static void main(String[] args) throws Exception {
ManagementFactoryTest managementFactoryTest = new ManagementFactoryTest();
Method method = ManagementFactoryTest.class.getMethod("printSystemInfo");
method.invoke(managementFactoryTest);
}
public void printSystemInfo() {
System.out.println("打印系统信息-----------");
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
System.out.println("返回最后一分钟的系统负载平均值:" + operatingSystemMXBean.getSystemLoadAverage());
System.out.println("返回操作系统版本:" + operatingSystemMXBean.getVersion());
System.out.println("返回操作系统架构:" + operatingSystemMXBean.getArch());
System.out.println("返回操作系统名称:" + operatingSystemMXBean.getName());
System.out.println("返回可用于Java虚拟机的处理器数量:" + operatingSystemMXBean.getAvailableProcessors());
}
输出信息:
打印系统信息-----------
Disconnected from the target VM, address: '127.0.0.1:54855', transport: 'socket'
返回最后一分钟的系统负载平均值:2.84033203125
返回操作系统版本:10.13.6
返回操作系统架构:x86_64
返回操作系统名称:Mac OS X
返回可用于Java虚拟机的处理器数量:4
二、打印Java虚拟机编译系统信息
代码:
public static void main(String[] args) throws Exception {
ManagementFactoryTest managementFactoryTest = new ManagementFactoryTest();
Method method = ManagementFactoryTest.class.getMethod("printCompilationInfo");
method.invoke(managementFactoryTest);
}
public void printCompilationInfo() {
System.out.println("打印Java虚拟机编译系统信息-----------");
CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
System.out.println("返回即时(JIT)编译器的名称:" + compilationMXBean.getName());
System.out.println("返回在编译中花费的大概累积的经过时间(以毫秒为单位):" + compilationMXBean.getTotalCompilationTime());
System.out.println("测试Java虚拟机是否支持对编译时间的监视:" + compilationMXBean.isCompilationTimeMonitoringSupported());
}
运行结果:
打印Java虚拟机编译系统信息-----------
Disconnected from the target VM, address: '127.0.0.1:58858', transport: 'socket'
返回即时(JIT)编译器的名称:HotSpot 64-Bit Tiered Compilers
返回在编译中花费的大概累积的经过时间(以毫秒为单位):41
测试Java虚拟机是否支持对编译时间的监视:true
三、输出内存信息
代码:
public static void main(String[] args) throws Exception {
ManagementFactoryTest managementFactoryTest = new ManagementFactoryTest();
Method method = ManagementFactoryTest.class.getMethod("printMemoryInfo");
method.invoke(managementFactoryTest);
}
public void printMemoryInfo() {
System.out.println("打印Java内存系统信息-----------");
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
System.out.println("测试是否启用了内存系统的详细输出:" + memoryMXBean.isVerbose());
System.out.println("返回正在等待完成的对象的大致数量:" + memoryMXBean.getObjectPendingFinalizationCount());
System.out.println("返回用于对象分配的堆的当前内存使用情况:" + memoryMXBean.getHeapMemoryUsage());
System.out.println("返回Java虚拟机使用的非堆内存的当前内存使用情况:" + memoryMXBean.getNonHeapMemoryUsage());
}
运行结果:
打印Java内存系统信息-----------
测试是否启用了内存系统的详细输出:false
返回正在等待完成的对象的大致数量:0
返回用于对象分配的堆的当前内存使用情况:init = 134217728(131072K) used = 5497296(5368K) committed = 128974848(125952K) max = 1908932608(1864192K)
返回Java虚拟机使用的非堆内存的当前内存使用情况:init = 2555904(2496K) used = 4966400(4850K) committed = 8060928(7872K) max = -1(-1K)
四、获取运行时信息
代码:
public static void main(String[] args) throws Exception {
ManagementFactoryTest managementFactoryTest = new ManagementFactoryTest();
Method method = ManagementFactoryTest.class.getMethod("printRuntimeInfo");
method.invoke(managementFactoryTest);
}
public void printRuntimeInfo() {
System.out.println("打印Java运行时信息-----------");
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
System.out.println("返回bootstrap类加载器用于搜索类文件的引导类路径:" + runtimeMXBean.getBootClassPath());
System.out.println("返回系统类加载器用于搜索类文件的Java类路径" + runtimeMXBean.getClassPath());
System.out.println("返回表示正在运行的Java虚拟机的名称" + runtimeMXBean.getName());
System.out.println("返回Java虚拟机规范名称" + runtimeMXBean.getSpecName());
System.out.println("返回Java虚拟机规范供应商" + runtimeMXBean.getSpecVendor());
System.out.println("返回Java虚拟机实现名称" + runtimeMXBean.getVmName());
}
运行结果:
打印Java运行时信息-----------
返回bootstrap类加载器用于搜索类文件的引导类路径:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/classes:/Users/apple/Library/Caches/IntelliJIdea2018.2/captureAgent/debugger-agent-storage.jar
返回系统类加载器用于搜索类文件的Java类路径/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/tools.jar:/Users/apple/IdeaProjects/maven_project/cz-parent/cz-consumer/target/classes:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter-web/2.1.0.RELEASE/spring-boot-starter-web-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter/2.1.0.RELEASE/spring-boot-starter-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot/2.1.0.RELEASE/spring-boot-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-autoconfigure/2.1.0.RELEASE/spring-boot-autoconfigure-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter-logging/2.1.0.RELEASE/spring-boot-starter-logging-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Applications/Java/Apache/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Applications/Java/Apache/repository/org/apache/logging/log4j/log4j-to-slf4j/2.11.1/log4j-to-slf4j-2.11.1.jar:/Applications/Java/Apache/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar:/Applications/Java/Apache/repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar:/Applications/Java/Apache/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Applications/Java/Apache/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter-json/2.1.0.RELEASE/spring-boot-starter-json-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/core/jackson-databind/2.9.7/jackson-databind-2.9.7.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/core/jackson-core/2.9.7/jackson-core-2.9.7.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.7/jackson-datatype-jdk8-2.9.7.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.7/jackson-datatype-jsr310-2.9.7.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.7/jackson-module-parameter-names-2.9.7.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter-tomcat/2.1.0.RELEASE/spring-boot-starter-tomcat-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.12/tomcat-embed-core-9.0.12.jar:/Applications/Java/Apache/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.12/tomcat-embed-el-9.0.12.jar:/Applications/Java/Apache/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.12/tomcat-embed-websocket-9.0.12.jar:/Applications/Java/Apache/repository/org/hibernate/validator/hibernate-validator/6.0.13.Final/hibernate-validator-6.0.13.Final.jar:/Applications/Java/Apache/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/Applications/Java/Apache/repository/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar:/Applications/Java/Apache/repository/com/fasterxml/classmate/1.4.0/classmate-1.4.0.jar:/Applications/Java/Apache/repository/org/springframework/spring-web/5.1.2.RELEASE/spring-web-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-beans/5.1.2.RELEASE/spring-beans-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-webmvc/5.1.2.RELEASE/spring-webmvc-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-aop/5.1.2.RELEASE/spring-aop-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-context/5.1.2.RELEASE/spring-context-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-expression/5.1.2.RELEASE/spring-expression-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Applications/Java/Apache/repository/org/springframework/spring-core/5.1.2.RELEASE/spring-core-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-jcl/5.1.2.RELEASE/spring-jcl-5.1.2.RELEASE.jar:/Users/apple/IdeaProjects/maven_project/cz-parent/cz-api/target/classes:/Applications/Java/Apache/repository/com/alibaba/boot/dubbo-spring-boot-starter/0.2.0/dubbo-spring-boot-starter-0.2.0.jar:/Applications/Java/Apache/repository/com/alibaba/dubbo/2.6.2/dubbo-2.6.2.jar:/Applications/Java/Apache/repository/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.jar:/Applications/Java/Apache/repository/org/jboss/netty/netty/3.2.5.Final/netty-3.2.5.Final.jar:/Applications/Java/Apache/repository/org/apache/zookeeper/zookeeper/3.4.9/zookeeper-3.4.9.jar:/Applications/Java/Apache/repository/jline/jline/0.9.94/jline-0.9.94.jar:/Applications/Java/Apache/repository/io/netty/netty/3.10.5.Final/netty-3.10.5.Final.jar:/Applications/Java/Apache/repository/org/apache/curator/curator-framework/2.12.0/curator-framework-2.12.0.jar:/Applications/Java/Apache/repository/org/apache/curator/curator-client/2.12.0/curator-client-2.12.0.jar:/Applications/Java/Apache/repository/com/google/guava/guava/16.0.1/guava-16.0.1.jar:/Applications/Java/Apache/repository/com/alibaba/boot/dubbo-spring-boot-autoconfigure/0.2.0/dubbo-spring-boot-autoconfigure-0.2.0.jar:/Applications/Java/install/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Users/apple/Library/Caches/IntelliJIdea2018.2/captureAgent/debugger-agent.jar
返回表示正在运行的Java虚拟机的名称866@juans.local
返回Java虚拟机规范名称Java Virtual Machine Specification
返回Java虚拟机规范供应商Oracle Corporation
返回Java虚拟机实现名称Java HotSpot(TM) 64-Bit Server VM
五、Dubbo中获取cpu可利用率的示例
public class LoadStatusChecker implements StatusChecker {
public LoadStatusChecker() {
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage");
load = (Double)method.invoke(operatingSystemMXBean);
} catch (Throwable var5) {
load = -1.0D;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0.0D ? Level.UNKNOWN : (load < (double)cpu ? Level.OK : Level.WARN), (load < 0.0D ? "" : "load:" + load + ",") + "cpu:" + cpu);
}
}