Fixed NoClassDefFoundError if com.sun.management.OperatingSystemMXBean does not exist
@@ -189,6 +189,9 @@ public class GridMetricManager extends GridManagerAdapter<MetricExporterSpi> imp
/** Nonheap memory metrics. */
private final MemoryUsageMetrics nonHeap;
+ /** */
+ private final SunOperatingSystemMXBeanAccessor sunOs;
+
/**
* @param ctx Kernal context.
*/
@@ -214,6 +217,8 @@ public class GridMetricManager extends GridManagerAdapter<MetricExporterSpi> imp
return spiWithSql;
}).get());
+ sunOs = sunOperatingSystemMXBeanAccessor();
+
ctx.addNodeAttribute(ATTR_PHY_RAM, totalSysMemory());
heap = new MemoryUsageMetrics(SYS_METRICS, metricName("memory", "heap"));
@@ -568,8 +573,6 @@ public class GridMetricManager extends GridManagerAdapter<MetricExporterSpi> imp
*/
private long totalSysMemory() {
try {
- com.sun.management.OperatingSystemMXBean sunOs = (com.sun.management.OperatingSystemMXBean)os;
-
return sunOs.getTotalPhysicalMemorySize();
}
catch (RuntimeException ignored) {
@@ -577,6 +580,38 @@ public class GridMetricManager extends GridManagerAdapter<MetricExporterSpi> imp
}
}
+ /** @return Accessor for {@link com.sun.management.OperatingSystemMXBean}. */
+ private SunOperatingSystemMXBeanAccessor sunOperatingSystemMXBeanAccessor() {
+ try {
+ if (os instanceof com.sun.management.OperatingSystemMXBean) {
+ com.sun.management.OperatingSystemMXBean sunOs = (com.sun.management.OperatingSystemMXBean)os;
+
+ return new SunOperatingSystemMXBeanAccessor() {
+ @Override public long getProcessCpuTime() {
+ return sunOs.getProcessCpuTime();
+ }
+
+ @Override public long getTotalPhysicalMemorySize() {
+ return sunOs.getTotalPhysicalMemorySize();
+ }
+ };
+ }
+ } catch (@SuppressWarnings("ErrorNotRethrown") NoClassDefFoundError ignored) {
+ log.warning("The 'com.sun.management.OperatingSystemMXBean' class is not available for class loader. " +
+ "System/JVM memory and CPU statistics may be not available.");
+ }
+
+ return new SunOperatingSystemMXBeanAccessor() {
+ @Override public long getProcessCpuTime() {
+ return U.<Long>property(os, "processCpuTime");
+ }
+
+ @Override public long getTotalPhysicalMemorySize() {
+ return U.<Long>property(os, "totalPhysicalMemorySize");
+ }
+ };
+ }
+
/** */
private class MetricsUpdater implements Runnable {
/** */
@@ -629,8 +664,6 @@ public class GridMetricManager extends GridManagerAdapter<MetricExporterSpi> imp
long cpuTime;
try {
- com.sun.management.OperatingSystemMXBean sunOs = (com.sun.management.OperatingSystemMXBean)os;
-
cpuTime = sunOs.getProcessCpuTime();
}
catch (RuntimeException ignored) {
@@ -695,4 +728,13 @@ public class GridMetricManager extends GridManagerAdapter<MetricExporterSpi> imp
max.value(usage.getMax());
}
}
+
+ /** Accessor for {@link com.sun.management.OperatingSystemMXBean} methods. */
+ private interface SunOperatingSystemMXBeanAccessor {
+ /** @see com.sun.management.OperatingSystemMXBean#getProcessCpuTime() */
+ long getProcessCpuTime();
+
+ /** @see com.sun.management.OperatingSystemMXBean#getTotalPhysicalMemorySize() */
+ long getTotalPhysicalMemorySize();
+ }
}
