From d1ef300ed9cad23b41cb3aa203e6b148dc4561b2 Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Wed, 6 Jan 2021 12:54:11 +0800 Subject: [PATCH 1/5] free credential instance when serverHttpAgent shutdown. --- .../com/alibaba/nacos/client/config/http/ServerHttpAgent.java | 1 + .../com/alibaba/nacos/client/config/impl/SpasAdapter.java | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 7d4a996a1f1..459d2f4b291 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -453,6 +453,7 @@ public void shutdown() throws NacosException { LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdownThreadPool(executorService, LOGGER); ConfigHttpClientManager.getInstance().shutdown(); + SpasAdapter.freeCredentialInstance(); LOGGER.info("{} do shutdown stop", className); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java index a90694f315a..9927d597637 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java @@ -74,6 +74,10 @@ public static String getAk() { return CredentialService.getInstance().getCredential().getAccessKey(); } + public static void freeCredentialInstance() { + CredentialService.freeInstance(); + } + /** * Sign with hmac SHA1 encrtpt. * From e44f74bc7948fabbc76e005f07dc1356775a7c89 Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Wed, 6 Jan 2021 14:14:17 +0800 Subject: [PATCH 2/5] free credential instance when namingProxy shutdown. --- .../java/com/alibaba/nacos/client/naming/net/NamingProxy.java | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index e40127984b1..5fff83042a5 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -721,6 +721,7 @@ public void shutdown() throws NacosException { String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); + SpasAdapter.freeCredentialInstance(); NamingHttpClientManager.getInstance().shutdown(); NAMING_LOGGER.info("{} do shutdown stop", className); } From 428e506a7c58715d3068e65bf7645751f996f802 Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Wed, 6 Jan 2021 14:16:42 +0800 Subject: [PATCH 3/5] unify the code place --- .../java/com/alibaba/nacos/client/naming/net/NamingProxy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 5fff83042a5..60e09a182aa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -721,8 +721,8 @@ public void shutdown() throws NacosException { String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); - SpasAdapter.freeCredentialInstance(); NamingHttpClientManager.getInstance().shutdown(); + SpasAdapter.freeCredentialInstance(); NAMING_LOGGER.info("{} do shutdown stop", className); } } From 50fc1181fc5641c0523885873504c2f86bd4ab87 Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Wed, 6 Jan 2021 14:32:09 +0800 Subject: [PATCH 4/5] cancel timer when CredentialWatcher stop --- .../alibaba/nacos/client/identify/CredentialWatcher.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java index 689d919a721..9de0bbc5e2d 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java @@ -39,7 +39,7 @@ public class CredentialWatcher { private static final Logger SPAS_LOGGER = LogUtils.logger(CredentialWatcher.class); - private static final long REFRESH_INTERVAL = 10 * 1000; + private static final long REFRESH_INTERVAL = 10 * 1000L; private final CredentialService serviceInstance; @@ -49,6 +49,8 @@ public class CredentialWatcher { private final TimerTask watcher; + private final Timer timer; + private boolean stopped; @SuppressWarnings("PMD.AvoidUseTimerRule") @@ -56,8 +58,8 @@ public CredentialWatcher(String appName, CredentialService serviceInstance) { this.appName = appName; this.serviceInstance = serviceInstance; loadCredential(true); + timer = new Timer(true); watcher = new TimerTask() { - private final Timer timer = new Timer(true); private long modified = 0; @@ -101,6 +103,7 @@ public void stop() { synchronized (watcher) { watcher.cancel(); stopped = true; + timer.cancel(); } } SPAS_LOGGER.info("[{}] {} is stopped", appName, this.getClass().getSimpleName()); From ab1f6e64537731d48a52d52c21a0ea6dd91d2597 Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Wed, 6 Jan 2021 17:05:49 +0800 Subject: [PATCH 5/5] ignore PMD.AvoidUseTimerRule. --- .../com/alibaba/nacos/client/identify/CredentialWatcher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java index 9de0bbc5e2d..059dcc41d03 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java @@ -35,6 +35,7 @@ * * @author Nacos */ +@SuppressWarnings("PMD.AvoidUseTimerRule") public class CredentialWatcher { private static final Logger SPAS_LOGGER = LogUtils.logger(CredentialWatcher.class); @@ -53,7 +54,6 @@ public class CredentialWatcher { private boolean stopped; - @SuppressWarnings("PMD.AvoidUseTimerRule") public CredentialWatcher(String appName, CredentialService serviceInstance) { this.appName = appName; this.serviceInstance = serviceInstance;