You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/05/26 15:36:40 UTC

[dubbo] branch 3.0 updated: improve monitor creating, handle errors (#7866)

This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 0fa05b8  improve monitor creating, handle errors (#7866)
0fa05b8 is described below

commit 0fa05b8982ae39c459dcda119dacd171eeaea194
Author: Gong Dewei <ky...@qq.com>
AuthorDate: Wed May 26 23:36:11 2021 +0800

    improve monitor creating, handle errors (#7866)
---
 .../monitor/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 b785116..a620d06 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 org.apache.dubbo.monitor.MonitorService;
 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<String, Monitor> MONITORS = new ConcurrentHashMap<String, Monitor>();
 
-    private static final Map<String, CompletableFuture<Monitor>> FUTURES = new ConcurrentHashMap<String, CompletableFuture<Monitor>>();
+    private static final Map<String, Future<Monitor>> FUTURES = new ConcurrentHashMap<String, Future<Monitor>>();
 
     /**
      * The monitor create executor
@@ -85,10 +83,18 @@ public abstract class AbstractMonitorFactory implements MonitorFactory {
             }
 
             final URL monitorUrl = url;
-            final CompletableFuture<Monitor> 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 abstract class AbstractMonitorFactory implements MonitorFactory {
 
     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<Monitor> 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);
-            }
-        }
-    }
-
 }