Check if usage operations are supported in MonitorMemory
This closes
@@ -19,6 +19,7 @@ package org.apache.nifi.controller;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.annotation.lifecycle.OnScheduled;
+import org.apache.nifi.annotation.lifecycle.OnStopped;
import org.apache.nifi.components.AllowableValue;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.ValidationContext;
@@ -39,6 +40,7 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
+import java.util.stream.Collectors;
/**
* Reporting task used to monitor usage of memory after Garbage Collection has
@@ -91,7 +93,8 @@ public class MonitorMemory extends AbstractReportingTask {
private static final AllowableValue[] memPoolAllowableValues;
static {
- List<MemoryPoolMXBean> memoryPoolBeans = ManagementFactory.getMemoryPoolMXBeans();
+ // Only allow memory pool beans that support usage thresholds, otherwise we wouldn't report anything anyway
+ List<MemoryPoolMXBean> memoryPoolBeans = ManagementFactory.getMemoryPoolMXBeans().stream().filter(MemoryPoolMXBean::isUsageThresholdSupported).collect(Collectors.toList());
memPoolAllowableValues = new AllowableValue[memoryPoolBeans.size()];
for (int i = 0; i < memPoolAllowableValues.length; i++) {
memPoolAllowableValues[i] = new AllowableValue(memoryPoolBeans.get(i).getName());
@@ -175,10 +178,12 @@ public class MonitorMemory extends AbstractReportingTask {
final double pct = Double.parseDouble(percentage) / 100D;
calculatedThreshold = (long) (monitoredBean.getUsage().getMax() * pct);
}
+ if (monitoredBean.isUsageThresholdSupported()) {
monitoredBean.setUsageThreshold(calculatedThreshold);
}
}
}
+ }
if (monitoredBean == null) {
throw new InitializationException("Found no JVM Memory Pool with name " + desiredMemoryPoolName + "; will not monitor Memory Pool");
@@ -200,7 +205,7 @@ public class MonitorMemory extends AbstractReportingTask {
}
final double percentageUsed = (double) usage.getUsed() / (double) usage.getMax() * 100D;
- if (bean.isUsageThresholdExceeded()) {
+ if (bean.isUsageThresholdSupported() && bean.isUsageThresholdExceeded()) {
if (System.currentTimeMillis() < reportingIntervalMillis + lastReportTime && lastReportTime > 0L) {
return;
}
@@ -223,6 +228,11 @@ public class MonitorMemory extends AbstractReportingTask {
}
}
+ @OnStopped
+ public void onStopped() {
+ monitoredBean = null;
+ }
+
private static class ThresholdValidator implements Validator {
@Override
