From 516999f73d423df75114a2f9c41da465e471be9f Mon Sep 17 00:00:00 2001 From: machen Date: Fri, 17 Mar 2023 21:12:45 +0800 Subject: [PATCH] Optimize active parameter as empty task code --- .../config/DiscoveryConfiguration.java | 12 +++++-- .../DynamicThreadPoolAutoConfiguration.java | 10 ++---- .../starter/core/ClientShutdown.java | 31 +++++++++++++++---- .../springboot/starter/core/ClientWorker.java | 20 ++++++------ .../starter/core/DiscoveryClient.java | 18 +++++------ .../starter/remote/ServerHttpAgent.java | 22 +------------ 6 files changed, 58 insertions(+), 55 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DiscoveryConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DiscoveryConfiguration.java index 5a5c47a1..9e7f2693 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DiscoveryConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DiscoveryConfiguration.java @@ -19,6 +19,7 @@ package cn.hippo4j.springboot.starter.config; import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.core.toolkit.inet.InetUtils; +import cn.hippo4j.springboot.starter.core.ClientShutdown; import cn.hippo4j.springboot.starter.core.DiscoveryClient; import cn.hippo4j.springboot.starter.provider.InstanceInfoProviderFactory; import cn.hippo4j.springboot.starter.remote.HttpAgent; @@ -44,7 +45,14 @@ public class DiscoveryConfiguration { } @Bean - public DiscoveryClient hippo4JDiscoveryClient(HttpAgent httpAgent, InstanceInfo instanceInfo) { - return new DiscoveryClient(httpAgent, instanceInfo); + public ClientShutdown hippo4jClientShutdown() { + return new ClientShutdown(); + } + + @Bean + public DiscoveryClient hippo4JDiscoveryClient(HttpAgent httpAgent, + InstanceInfo instanceInfo, + ClientShutdown hippo4jClientShutdown) { + return new DiscoveryClient(httpAgent, instanceInfo, hippo4jClientShutdown); } } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index ce59d27a..e85a43ea 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -114,9 +114,10 @@ public class DynamicThreadPoolAutoConfiguration { public ClientWorker hippo4jClientWorker(HttpAgent httpAgent, InetUtils hippo4JInetUtils, ServerHealthCheck serverHealthCheck, - DynamicThreadPoolBannerHandler dynamicThreadPoolBannerHandlers) { + DynamicThreadPoolBannerHandler dynamicThreadPoolBannerHandlers, + ClientShutdown hippo4jClientShutdown) { String identify = IdentifyUtil.generate(environment, hippo4JInetUtils); - return new ClientWorker(httpAgent, identify, serverHealthCheck, dynamicThreadPoolBannerHandlers.getVersion()); + return new ClientWorker(httpAgent, identify, serverHealthCheck, dynamicThreadPoolBannerHandlers.getVersion(), hippo4jClientShutdown); } @Bean @@ -247,9 +248,4 @@ public class DynamicThreadPoolAutoConfiguration { public ThreadPoolPluginRegisterPostProcessor threadPoolPluginRegisterPostProcessor() { return new ThreadPoolPluginRegisterPostProcessor(); } - - @Bean - public ClientShutdown hippo4jClientShutdown() { - return new ClientShutdown(); - } } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientShutdown.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientShutdown.java index bf6a148d..bd12c789 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientShutdown.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientShutdown.java @@ -17,29 +17,48 @@ package cn.hippo4j.springboot.starter.core; +import lombok.Getter; + import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import lombok.Getter; /** - * Client Shutdown + * Called when the application is closed to avoid data exceptions caused by + * long polling on the client side. + * + * @version 1.5.0 + * @see */ public class ClientShutdown { @Getter private volatile boolean prepareClose = false; - - private CountDownLatch countDownLatch = new CountDownLatch(1); - private final static Long TIME_OUT_SECOND = 1L; + private static final int DEFAULT_COUNT = 1; + private final CountDownLatch countDownLatch = new CountDownLatch(DEFAULT_COUNT); + + /** + * Called when the application is closed. + * + * @throws InterruptedException + */ public void prepareDestroy() throws InterruptedException { prepareClose = true; countDownLatch.await(TIME_OUT_SECOND, TimeUnit.SECONDS); } + /** + * Decrements the count of the latch, releasing all waiting threads if + * the count reaches zero. + * + *

If the current count is greater than zero then it is decremented. + * If the new count is zero then all waiting threads are re-enabled for + * thread scheduling purposes. + * + *

If the current count equals zero then nothing happens. + */ public void countDown() { countDownLatch.countDown(); } - } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientWorker.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientWorker.java index a5413cfe..5af0460d 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientWorker.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ClientWorker.java @@ -60,30 +60,30 @@ import static cn.hippo4j.common.constant.Constants.WORD_SEPARATOR; public class ClientWorker implements DisposableBean { private final long timeout; - - private final HttpAgent agent; - private final String identify; - private final String version; + private final HttpAgent agent; private final ServerHealthCheck serverHealthCheck; - private final ScheduledExecutorService executorService; + private final ClientShutdown hippo4jClientShutdown; private final CountDownLatch awaitApplicationComplete = new CountDownLatch(1); - private final CountDownLatch cacheCondition = new CountDownLatch(1); - private final ConcurrentHashMap cacheMap = new ConcurrentHashMap<>(16); @SuppressWarnings("all") - public ClientWorker(HttpAgent httpAgent, String identify, ServerHealthCheck serverHealthCheck, String version) { + public ClientWorker(HttpAgent httpAgent, + String identify, + ServerHealthCheck serverHealthCheck, + String version, + ClientShutdown hippo4jClientShutdown) { this.agent = httpAgent; this.identify = identify; this.timeout = CONFIG_LONG_POLL_TIMEOUT; this.version = version; this.serverHealthCheck = serverHealthCheck; + this.hippo4jClientShutdown = hippo4jClientShutdown; ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, runnable -> { Thread thread = new Thread(runnable); thread.setName("client.worker.executor"); @@ -122,7 +122,9 @@ public class ClientWorker implements DisposableBean { @Override @SneakyThrows public void run() { - if (executorService.isShutdown()) { + if (executorService.isShutdown() || hippo4jClientShutdown.isPrepareClose()) { + hippo4jClientShutdown.countDown(); + log.info("The task of monitoring dynamic thread pool changes has stopped."); return; } if (cacheMapInitEmptyFlag) { diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/DiscoveryClient.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/DiscoveryClient.java index fe619f60..38a22368 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/DiscoveryClient.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/DiscoveryClient.java @@ -43,20 +43,18 @@ import static cn.hippo4j.common.constant.Constants.BASE_PATH; public class DiscoveryClient implements DisposableBean { private final ScheduledExecutorService scheduler; - private final HttpAgent httpAgent; - private final InstanceInfo instanceInfo; + private final ClientShutdown hippo4jClientShutdown; private volatile long lastSuccessfulHeartbeatTimestamp = -1; - private static final String PREFIX = "DiscoveryClient_"; - private final String appPathIdentifier; - public DiscoveryClient(HttpAgent httpAgent, InstanceInfo instanceInfo) { + public DiscoveryClient(HttpAgent httpAgent, InstanceInfo instanceInfo, ClientShutdown hippo4jClientShutdown) { this.httpAgent = httpAgent; this.instanceInfo = instanceInfo; + this.hippo4jClientShutdown = hippo4jClientShutdown; this.appPathIdentifier = instanceInfo.getAppName().toUpperCase() + "/" + instanceInfo.getInstanceId(); this.scheduler = new ScheduledThreadPoolExecutor( new Integer(1), @@ -104,7 +102,7 @@ public class DiscoveryClient implements DisposableBean { .setGroupKey(groupKeyIp); clientCloseResult = httpAgent.httpPostByDiscovery(clientCloseUrlPath, clientCloseHookReq); if (clientCloseResult.isSuccess()) { - log.info("{}{} -client close hook success.", PREFIX, appPathIdentifier); + log.info("{}{} - client close hook success.", PREFIX, appPathIdentifier); } } catch (Throwable ex) { if (ex instanceof ShutdownExecuteException) { @@ -115,9 +113,9 @@ public class DiscoveryClient implements DisposableBean { } private void prepareDestroy() throws InterruptedException { - this.scheduler.shutdownNow(); - // try to make sure the ClientWorker is closed first - ApplicationContextHolder.getBean(ClientShutdown.class).prepareDestroy(); + scheduler.shutdownNow(); + // Try to make sure the ClientWorker is closed first. + hippo4jClientShutdown.prepareDestroy(); } public class HeartbeatThread implements Runnable { @@ -133,7 +131,7 @@ public class DiscoveryClient implements DisposableBean { private boolean renew() { Result renewResult; try { - if (this.scheduler.isShutdown()) { + if (scheduler.isShutdown()) { return false; } InstanceInfo.InstanceRenew instanceRenew = new InstanceInfo.InstanceRenew() diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/remote/ServerHttpAgent.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/remote/ServerHttpAgent.java index 5c41aced..c507d12d 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/remote/ServerHttpAgent.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/remote/ServerHttpAgent.java @@ -19,12 +19,11 @@ package cn.hippo4j.springboot.starter.remote; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.constant.Constants; +import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.toolkit.http.HttpUtil; import cn.hippo4j.common.web.base.Result; -import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; import cn.hippo4j.springboot.starter.config.BootstrapProperties; -import cn.hippo4j.springboot.starter.core.ClientShutdown; import cn.hippo4j.springboot.starter.security.SecurityProxy; import java.util.HashMap; @@ -32,12 +31,10 @@ import java.util.Map; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import lombok.extern.slf4j.Slf4j; /** * Server http agent. */ -@Slf4j public class ServerHttpAgent implements HttpAgent { private final BootstrapProperties dynamicThreadPoolProperties; @@ -48,8 +45,6 @@ public class ServerHttpAgent implements HttpAgent { private ServerHealthCheck serverHealthCheck; - private ClientShutdown clientShutdown; - private ScheduledExecutorService executorService; private final long securityInfoRefreshIntervalMills = TimeUnit.SECONDS.toMillis(5); @@ -115,9 +110,6 @@ public class ServerHttpAgent implements HttpAgent { public Result httpPostByConfig(String path, Map headers, Map paramValues, long readTimeoutMs) { isHealthStatus(); injectSecurityInfo(paramValues); - if (isPrepareClose()) { - return new Result(); - } return HttpUtil.post(buildUrl(path), headers, paramValues, readTimeoutMs, Result.class); } @@ -137,18 +129,6 @@ public class ServerHttpAgent implements HttpAgent { serverHealthCheck.isHealthStatus(); } - private boolean isPrepareClose() { - if (clientShutdown == null) { - clientShutdown = ApplicationContextHolder.getBean(ClientShutdown.class); - } - if (clientShutdown.isPrepareClose()) { - clientShutdown.countDown(); - log.info("client prepare shutdown"); - return true; - } - return false; - } - private Map injectSecurityInfo(Map params) { if (StringUtil.isNotBlank(securityProxy.getAccessToken())) { params.put(Constants.ACCESS_TOKEN, securityProxy.getAccessToken());