From 0fa05b8982ae39c459dcda119dacd171eeaea194 Mon Sep 17 00:00:00 2001 From: Gong Dewei Date: Wed, 26 May 2021 23:36:11 +0800 Subject: [PATCH] improve monitor creating, handle errors (#7866) --- .../support/AbstractMonitorFactory.java | 44 ++++++------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/AbstractMonitorFactory.java b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/AbstractMonitorFactory.java index b7851167d25..a620d06b0b4 100644 --- a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/AbstractMonitorFactory.java +++ b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/AbstractMonitorFactory.java @@ -27,9 +27,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Map; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.SynchronousQueue; @@ -55,7 +53,7 @@ public abstract class AbstractMonitorFactory implements MonitorFactory { */ private static final Map MONITORS = new ConcurrentHashMap(); - private static final Map> FUTURES = new ConcurrentHashMap>(); + private static final Map> FUTURES = new ConcurrentHashMap>(); /** * The monitor create executor @@ -85,10 +83,18 @@ public Monitor getMonitor(URL url) { } final URL monitorUrl = url; - final CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> AbstractMonitorFactory.this.createMonitor(monitorUrl)); - FUTURES.put(key, completableFuture); - completableFuture.thenRunAsync(new MonitorListener(key), EXECUTOR); - + future = EXECUTOR.submit(() -> { + try { + Monitor m = createMonitor(monitorUrl); + MONITORS.put(key, m); + FUTURES.remove(key); + return m; + } catch (Throwable e) { + logger.warn("Create monitor failed, monitor data will not be collected until you fix this problem. monitorUrl: " + monitorUrl, e); + return null; + } + }); + FUTURES.put(key, future); return null; } finally { // unlock @@ -98,28 +104,4 @@ public Monitor getMonitor(URL url) { protected abstract Monitor createMonitor(URL url); - - class MonitorListener implements Runnable { - - private String key; - - public MonitorListener(String key) { - this.key = key; - } - - @Override - public void run() { - try { - CompletableFuture completableFuture = AbstractMonitorFactory.FUTURES.get(key); - AbstractMonitorFactory.MONITORS.put(key, completableFuture.get()); - AbstractMonitorFactory.FUTURES.remove(key); - } catch (InterruptedException e) { - logger.warn("Thread was interrupted unexpectedly, monitor will never be got."); - AbstractMonitorFactory.FUTURES.remove(key); - } catch (ExecutionException e) { - logger.warn("Create monitor failed, monitor data will not be collected until you fix this problem. ", e); - } - } - } - }