diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/AbstractConfig.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/AbstractConfig.java index 7f6e723..8a001b5 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/AbstractConfig.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/AbstractConfig.java @@ -19,7 +19,7 @@ import com.meituan.dorado.trace.TraceFactory; import com.meituan.dorado.util.ClazzUtil; -public abstract class AbstractConfig { +public abstract class AbstractConfig implements Disposable { protected String serviceName; // 服务接口, 调用端必配, 服务端若没有配置则取实现类的第一个接口 diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/Disposable.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/Disposable.java new file mode 100644 index 0000000..bc10134 --- /dev/null +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/Disposable.java @@ -0,0 +1,6 @@ +package com.meituan.dorado.config.service; + +public interface Disposable { + + void destroy(); +} diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ProviderConfig.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ProviderConfig.java index fb54c0d..7ff97e3 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ProviderConfig.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ProviderConfig.java @@ -29,7 +29,7 @@ import java.util.List; import java.util.concurrent.BlockingQueue; -public class ProviderConfig { +public class ProviderConfig implements Disposable { private static final Logger LOGGER = LoggerFactory.getLogger(ProviderConfig.class); @@ -66,8 +66,6 @@ public class ProviderConfig { private List filters = Collections.emptyList(); private String env = Constants.EnvType.TEST.getEnvName(); - private volatile ShutDownHook hook; - private volatile boolean destroyed; public void init() { @@ -104,10 +102,7 @@ public synchronized void destroy() { } protected synchronized void addShutDownHook() { - if (hook == null) { - hook = new ShutDownHook(this); - Runtime.getRuntime().addShutdownHook(hook); - } + ShutdownHook.register(this); } public List getServiceList() { @@ -250,17 +245,4 @@ public void setThreadPoolQueue(BlockingQueue threadPoolQueue) { this.threadPoolQueue = threadPoolQueue; } - class ShutDownHook extends Thread { - private ProviderConfig config; - - public ShutDownHook(ProviderConfig config) { - this.config = config; - } - - @Override - public void run() { - hook = null; - config.destroy(); - } - } } diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ReferenceConfig.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ReferenceConfig.java index 744f339..6d1c78f 100644 --- a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ReferenceConfig.java +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ReferenceConfig.java @@ -35,7 +35,7 @@ import java.util.List; import java.util.Map; -public class ReferenceConfig extends AbstractConfig { +public class ReferenceConfig extends AbstractConfig implements Disposable { // 必配项 服务端 appkey private String remoteAppkey; @@ -84,8 +84,6 @@ public class ReferenceConfig extends AbstractConfig { private transient ClusterHandler clusterHandler; private volatile boolean destroyed; - private volatile ShutDownHook hook; - public synchronized T get() { if (destroyed) { @@ -141,10 +139,7 @@ private void configRouter() { } protected synchronized void addShutDownHook() { - if (hook == null) { - hook = new ShutDownHook(this); - Runtime.getRuntime().addShutdownHook(hook); - } + ShutdownHook.register(this); } /** @@ -345,17 +340,4 @@ public void setEnv(String env) { this.env = env; } - class ShutDownHook extends Thread { - private ReferenceConfig config; - - public ShutDownHook(ReferenceConfig config) { - this.config = config; - } - - @Override - public void run() { - hook = null; - config.destroy(); - } - } } diff --git a/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ShutdownHook.java b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ShutdownHook.java new file mode 100644 index 0000000..11c2fba --- /dev/null +++ b/dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ShutdownHook.java @@ -0,0 +1,27 @@ +package com.meituan.dorado.config.service; + +import java.util.LinkedList; +import java.util.List; + +public class ShutdownHook extends Thread { + + private static final List configs = new LinkedList<>(); + + static { + Runtime.getRuntime().addShutdownHook(new ShutdownHook()); + } + + public static synchronized void register(Disposable config) { + configs.add(config); + } + + private ShutdownHook() { + super("DoradoShutdownHook-Thread"); + } + @Override + public void run() { + for (Disposable config : configs) { + config.destroy(); + } + } +} diff --git a/dorado/dorado-doc/manual-user/ShutdownGracefully.md b/dorado/dorado-doc/manual-user/ShutdownGracefully.md index 8f5f094..5fd433e 100644 --- a/dorado/dorado-doc/manual-user/ShutdownGracefully.md +++ b/dorado/dorado-doc/manual-user/ShutdownGracefully.md @@ -5,23 +5,29 @@ Dorado的优雅关闭通过ShutDownHook方式实现,调用端和服务端通 ```java protected synchronized void addShutDownHook() { - if (hook == null) { - hook = new ShutDownHook(this); - Runtime.getRuntime().addShutdownHook(hook); - } + ShutdownHook.register(this); } -class ShutDownHook extends Thread { - private ReferenceConfig config; - - public ShutDownHook(ReferenceConfig config) { - this.config = config; +public class ShutdownHook extends Thread { + + private static final List configs = new LinkedList<>(); + + static { + Runtime.getRuntime().addShutdownHook(new ShutdownHook()); + } + + public static synchronized void register(Disposable config) { + configs.add(config); + } + + private ShutdownHook() { + super("DoradoShutdownHook-Thread"); } - @Override public void run() { - hook = null; - config.destroy(); + for (Disposable config : configs) { + config.destroy(); + } } } ``` \ No newline at end of file