From 5fa2de43ec2dfd7cd5e1db9f5e66f373d6ec91d5 Mon Sep 17 00:00:00 2001 From: Viktor Somogyi Date: Fri, 1 Nov 2019 19:53:21 +0100 Subject: [PATCH] MINOR: Replace some Java 7 style code with Java 8 style (#7623) Reviewers: Mickael Maison , Manikumar Reddy --- .../kafka/common/metrics/JmxReporter.java | 39 ++++++++----------- .../apache/kafka/common/metrics/Metrics.java | 32 +++++---------- 2 files changed, 25 insertions(+), 46 deletions(-) diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java b/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java index 3f852cf1a53..3129564faed 100644 --- a/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java +++ b/clients/src/main/java/org/apache/kafka/common/metrics/JmxReporter.java @@ -16,30 +16,26 @@ */ package org.apache.kafka.common.metrics; -import java.lang.management.ManagementFactory; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import org.apache.kafka.common.KafkaException; +import org.apache.kafka.common.MetricName; +import org.apache.kafka.common.utils.Sanitizer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.management.Attribute; import javax.management.AttributeList; import javax.management.AttributeNotFoundException; import javax.management.DynamicMBean; -import javax.management.InvalidAttributeValueException; import javax.management.JMException; import javax.management.MBeanAttributeInfo; -import javax.management.MBeanException; import javax.management.MBeanInfo; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; -import javax.management.ReflectionException; - -import org.apache.kafka.common.KafkaException; -import org.apache.kafka.common.MetricName; -import org.apache.kafka.common.utils.Sanitizer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.lang.management.ManagementFactory; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * Register metrics in JMX as dynamic mbeans based on the metric names @@ -49,7 +45,7 @@ public class JmxReporter implements MetricsReporter { private static final Logger log = LoggerFactory.getLogger(JmxReporter.class); private static final Object LOCK = new Object(); private String prefix; - private final Map mbeans = new HashMap(); + private final Map mbeans = new HashMap<>(); public JmxReporter() { this(""); @@ -175,7 +171,7 @@ public class JmxReporter implements MetricsReporter { private final ObjectName objectName; private final Map metrics; - public KafkaMbean(String mbeanName) throws MalformedObjectNameException { + KafkaMbean(String mbeanName) throws MalformedObjectNameException { this.metrics = new HashMap<>(); this.objectName = new ObjectName(mbeanName); } @@ -184,12 +180,12 @@ public class JmxReporter implements MetricsReporter { return objectName; } - public void setAttribute(String name, KafkaMetric metric) { + void setAttribute(String name, KafkaMetric metric) { this.metrics.put(name, metric); } @Override - public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException { + public Object getAttribute(String name) throws AttributeNotFoundException { if (this.metrics.containsKey(name)) return this.metrics.get(name).metricValue(); else @@ -209,7 +205,7 @@ public class JmxReporter implements MetricsReporter { return list; } - public KafkaMetric removeAttribute(String name) { + KafkaMetric removeAttribute(String name) { return this.metrics.remove(name); } @@ -232,15 +228,12 @@ public class JmxReporter implements MetricsReporter { } @Override - public Object invoke(String name, Object[] params, String[] sig) throws MBeanException, ReflectionException { + public Object invoke(String name, Object[] params, String[] sig) { throw new UnsupportedOperationException("Set not allowed."); } @Override - public void setAttribute(Attribute attribute) throws AttributeNotFoundException, - InvalidAttributeValueException, - MBeanException, - ReflectionException { + public void setAttribute(Attribute attribute) { throw new UnsupportedOperationException("Set not allowed."); } diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java b/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java index 3fc73e131e2..7f00adf1273 100644 --- a/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java +++ b/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java @@ -38,7 +38,6 @@ import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import static java.util.Collections.emptyList; @@ -137,29 +136,20 @@ public class Metrics implements Closeable { this.reporters = Objects.requireNonNull(reporters); this.time = time; for (MetricsReporter reporter : reporters) - reporter.init(new ArrayList()); + reporter.init(new ArrayList<>()); // Create the ThreadPoolExecutor only if expiration of Sensors is enabled. if (enableExpiration) { this.metricsScheduler = new ScheduledThreadPoolExecutor(1); // Creating a daemon thread to not block shutdown - this.metricsScheduler.setThreadFactory(new ThreadFactory() { - public Thread newThread(Runnable runnable) { - return KafkaThread.daemon("SensorExpiryThread", runnable); - } - }); + this.metricsScheduler.setThreadFactory(runnable -> KafkaThread.daemon("SensorExpiryThread", runnable)); this.metricsScheduler.scheduleAtFixedRate(new ExpireSensorTask(), 30, 30, TimeUnit.SECONDS); } else { this.metricsScheduler = null; } addMetric(metricName("count", "kafka-metrics-count", "total number of registered metrics"), - new Measurable() { - @Override - public double measure(MetricConfig config, long now) { - return metrics.size(); - } - }); + (config, now) -> metrics.size()); } /** @@ -186,7 +176,7 @@ public class Metrics implements Closeable { * @param description A human-readable description to include in the metric */ public MetricName metricName(String name, String group, String description) { - return metricName(name, group, description, new HashMap()); + return metricName(name, group, description, new HashMap<>()); } /** @@ -196,7 +186,7 @@ public class Metrics implements Closeable { * @param group logical group name of the metrics to which this metric belongs */ public MetricName metricName(String name, String group) { - return metricName(name, group, "", new HashMap()); + return metricName(name, group, "", new HashMap<>()); } /** @@ -227,7 +217,7 @@ public class Metrics implements Closeable { private static Map getTags(String... keyValue) { if ((keyValue.length % 2) != 0) throw new IllegalArgumentException("keyValue needs to be specified in pairs"); - Map tags = new LinkedHashMap(); + Map tags = new LinkedHashMap<>(); for (int i = 0; i < keyValue.length; i += 2) tags.put(keyValue[i], keyValue[i + 1]); @@ -245,7 +235,7 @@ public class Metrics implements Closeable { * @return the string containing the HTML table; never null */ public static String toHtmlTable(String domain, Iterable allMetrics) { - Map> beansAndAttributes = new TreeMap>(); + Map> beansAndAttributes = new TreeMap<>(); try (Metrics metrics = new Metrics()) { for (MetricNameTemplate template : allMetrics) { @@ -257,7 +247,7 @@ public class Metrics implements Closeable { MetricName metricName = metrics.metricName(template.name(), template.group(), template.description(), tags); String mBeanName = JmxReporter.getMBeanName(domain, metricName); if (!beansAndAttributes.containsKey(mBeanName)) { - beansAndAttributes.put(mBeanName, new TreeMap()); + beansAndAttributes.put(mBeanName, new TreeMap<>()); } Map attrAndDesc = beansAndAttributes.get(mBeanName); if (!attrAndDesc.containsKey(template.name())) { @@ -405,11 +395,7 @@ public class Metrics implements Closeable { this.sensors.put(name, s); if (parents != null) { for (Sensor parent : parents) { - List children = childrenSensors.get(parent); - if (children == null) { - children = new ArrayList<>(); - childrenSensors.put(parent, children); - } + List children = childrenSensors.computeIfAbsent(parent, k -> new ArrayList<>()); children.add(s); } }