From a59c2f79fc7aff60e178614bb68c9c1de3c4d7c6 Mon Sep 17 00:00:00 2001 From: "ronghuichao@kanzhun.com" Date: Thu, 13 Jun 2019 09:57:21 +0800 Subject: [PATCH] Optimize shutdown hook Add thread name --- .../dorado/config/service/AbstractConfig.java | 2 +- .../dorado/config/service/Disposable.java | 6 +++++ .../dorado/config/service/ProviderConfig.java | 22 ++------------- .../config/service/ReferenceConfig.java | 22 ++------------- .../dorado/config/service/ShutdownHook.java | 27 +++++++++++++++++++ 5 files changed, 38 insertions(+), 41 deletions(-) create mode 100644 dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/Disposable.java create mode 100644 dorado/dorado-core/src/main/java/com/meituan/dorado/config/service/ShutdownHook.java 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 095967d..c6007fd 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 ece9cc3..e90a164 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 @@ -26,7 +26,7 @@ import java.util.Collections; import java.util.List; -public class ProviderConfig { +public class ProviderConfig implements Disposable { protected String appkey; // mns, zookeeper://address?k=v&k=v; 没配置则从SPI中获取 @@ -56,8 +56,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() { @@ -85,10 +83,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() { @@ -199,17 +194,4 @@ public void setEnv(String env) { this.env = env; } - 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 372b52f..d3d386c 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) { @@ -142,10 +140,7 @@ private void configRouter() { } protected synchronized void addShutDownHook() { - if (hook == null) { - hook = new ShutDownHook(this); - Runtime.getRuntime().addShutdownHook(hook); - } + ShutdownHook.register(this); } /** @@ -342,17 +337,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..8c17b45 --- /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("ShutdownHook"); + } + @Override + public void run() { + for (Disposable config : configs) { + config.destroy(); + } + } +}