最近项目里面要做组件监控,于是整理了一下jvm常用的监控指标记录下来,废话不多讲,直接上代码
类名 | 说明 |
JvmRuntimeInfo | jvm运行时的指标统计实体类 |
JvmMonitor | jvm运行时的指标统计执行类 |
Builder | 通用构建对象的Builder |
public class JvmRuntimeInfo {
//组件名称
private String componentName = "suc-data-dispatcher";
//总内存
private String totalMemory;
//可用内存
private String freeMemory;
//已使用内存
private String usedMemory;
//gc执行次数
private long gcCount;
//最近一次gc耗时
private long gcAvgTime;
//总线程数量
private int activeThreadCount;
//守护线程数量
private int daemonThreadCount;
//类加载数量
private int classLoadedCount;
//卸载类数量
private long classUnloadedCount;
public String getTotalMemory() {
return totalMemory;
}
public void setTotalMemory(String totalMemory) {
this.totalMemory = totalMemory;
}
public String getFreeMemory() {
return freeMemory;
}
public void setFreeMemory(String freeMemory) {
this.freeMemory = freeMemory;
}
public String getUsedMemory() {
return usedMemory;
}
public void setUsedMemory(String usedMemory) {
this.usedMemory = usedMemory;
}
public long getGcCount() {
return gcCount;
}
public void setGcCount(long gcCount) {
this.gcCount = gcCount;
}
public long getGcAvgTime() {
return gcAvgTime;
}
public void setGcAvgTime(long gcAvgTime) {
this.gcAvgTime = gcAvgTime;
}
public long getActiveThreadCount() {
return activeThreadCount;
}
public void setActiveThreadCount(int activeThreadCount) {
this.activeThreadCount = activeThreadCount;
}
public int getDaemonThreadCount() {
return daemonThreadCount;
}
public void setDaemonThreadCount(int daemonThreadCount) {
this.daemonThreadCount = daemonThreadCount;
}
public int getClassLoadedCount() {
return classLoadedCount;
}
public void setClassLoadedCount(int classLoadedCount) {
this.classLoadedCount = classLoadedCount;
}
public long getClassUnloadedCount() {
return classUnloadedCount;
}
public void setClassUnloadedCount(long classUnloadedCount) {
this.classUnloadedCount = classUnloadedCount;
}
}
public class JvmMonitor {
public static JvmRuntimeInfo getJvmRuntimeInfo() {
Runtime runtime = Runtime.getRuntime();
long gcCount = ManagementFactory.getGarbageCollectorMXBeans().parallelStream().mapToLong(mxBean -> mxBean.getCollectionCount()).sum();
long gcTime = ManagementFactory.getGarbageCollectorMXBeans().parallelStream().mapToLong(mxBean -> mxBean.getCollectionTime()).sum();
return Builder.of(JvmRuntimeInfo.class)
.with(JvmRuntimeInfo::setTotalMemory, storageConvert(runtime.totalMemory()))
.with(JvmRuntimeInfo::setFreeMemory, storageConvert(runtime.freeMemory()))
.with(JvmRuntimeInfo::setUsedMemory, storageConvert(runtime.totalMemory() - runtime.freeMemory()))
.with(JvmRuntimeInfo::setGcCount, gcCount)
.with(JvmRuntimeInfo::setGcAvgTime, gcCount == 0 ? gcTime : (gcTime / gcCount))
.with(JvmRuntimeInfo::setActiveThreadCount, ManagementFactory.getThreadMXBean().getThreadCount())
.with(JvmRuntimeInfo::setActiveThreadCount, ManagementFactory.getThreadMXBean().getDaemonThreadCount())
.with(JvmRuntimeInfo::setClassLoadedCount, ManagementFactory.getClassLoadingMXBean().getLoadedClassCount())
.with(JvmRuntimeInfo::setClassUnloadedCount, ManagementFactory.getClassLoadingMXBean().getUnloadedClassCount())
.build();
}
private static String storageConvert(long size) {
if (size < 1024) {
return size + "B";
} else if (size >= 1024 * 1024 * 1024 * 1024l) {
return size / (1024 * 1024 * 1024 * 1024l) + "TB";
} else if (size >= 1024 * 1024 * 1024l) {
return size / (1024 * 1024 * 1024l) + "GB";
} else if (size >= 1024 * 1024l) {
return size / (1024 * 1024l) + "MB";
} else if (size >= 1024) {
return size / 1024 + "KB";
}
return "0B";
}
}
public class Builder<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(Builder.class);
public T instace;
private Builder(T instace) {
this.instace = instace;
}
public static <T> Builder<T> of(Class<T> tClass) {
T instance = null;
try {
instance = tClass.getDeclaredConstructor().newInstance();
} catch (InvocationTargetException e) {
LOGGER.error(tClass.getName(), e);
} catch (NoSuchMethodException e) {
LOGGER.error(tClass.getName(), e);
} catch (InstantiationException e) {
LOGGER.error(tClass.getName(), e);
} catch (IllegalAccessException e) {
LOGGER.error(tClass.getName(), e);
}
return new Builder<T>(instance);
}
public <U> Builder<T> with(BiConsumer<T, U> setter, U value) {
if (null != setter) {
setter.accept(instace, value);
}
return this;
}
public T build() {
return instace;
}
}
public class JvmMonitorApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(JvmMonitorApplication.class);
public static void main(String[] args) {
CompletableFuture.runAsync(() -> {
while (true) {
JvmRuntimeInfo jvmRuntimeInfo = JvmMonitor.getJvmRuntimeInfo();
LOGGER.info(JSON.toJSONString(jvmRuntimeInfo));
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
LOGGER.error("monitor_exception", e);
}
}
});
while (true) {
}
}
}