diff --git a/pulsar-broker/src/main/java/com/yahoo/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java b/pulsar-broker/src/main/java/com/yahoo/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java index 5c7e5457a0980..31d6e06d6a19d 100644 --- a/pulsar-broker/src/main/java/com/yahoo/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java +++ b/pulsar-broker/src/main/java/com/yahoo/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java @@ -33,6 +33,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Class that will return the broker host usage. @@ -120,8 +121,8 @@ private double getTotalCpuLimit() { * this would include iowait, irq and steal. */ private CpuStat getTotalCpuUsage() { - try { - String[] words = Files.lines(Paths.get("/proc/stat")) + try (Stream stream = Files.lines(Paths.get("/proc/stat"))) { + String[] words = stream .findFirst() .get().split("\\s+"); @@ -146,8 +147,8 @@ private ResourceUsage getMemUsage() { } private List getNics() { - try { - return Files.list(Paths.get("/sys/class/net/")) + try (Stream stream = Files.list(Paths.get("/sys/class/net/"))) { + return stream .filter(this::isPhysicalNic) .map(path -> path.getFileName().toString()) .collect(Collectors.toList());