diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/BaseSendMessageService.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/BaseSendMessageService.java index 689cd117..5384f532 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/BaseSendMessageService.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/BaseSendMessageService.java @@ -126,7 +126,7 @@ public class BaseSendMessageService implements InitializingBean, SendMessageServ try { result = httpAgent.httpPostByDiscovery(BASE_PATH + "/notify/list/config", new ThreadPoolNotifyReqDTO(groupKeys)); } catch (Throwable ex) { - log.error("Get dynamic thread pool notify configuration error.", ex); + log.error("Get dynamic thread pool notify configuration error. message :: {}", ex.getMessage()); } if (result != null && result.isSuccess() && result.getData() != null) { diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DiscoveryClient.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DiscoveryClient.java index a342a2fb..33b6d4e9 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DiscoveryClient.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DiscoveryClient.java @@ -71,7 +71,7 @@ public class DiscoveryClient implements DisposableBean { registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo); } catch (Exception ex) { registerResult = Results.failure(ErrorCodeEnum.SERVICE_ERROR); - log.error("{}{} - registration failed :: {}", PREFIX, appPathIdentifier, ex.getMessage(), ex); + log.error("{}{} - registration failed :: {}", PREFIX, appPathIdentifier, ex.getMessage()); } if (log.isInfoEnabled()) { diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/remote/AbstractHealthCheck.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/remote/AbstractHealthCheck.java index 4ad83156..b67145fe 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/remote/AbstractHealthCheck.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/remote/AbstractHealthCheck.java @@ -5,6 +5,8 @@ import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; import java.util.Objects; import java.util.concurrent.ScheduledThreadPoolExecutor; @@ -21,7 +23,7 @@ import static cn.hippo4j.common.constant.Constants.HEALTH_CHECK_INTERVAL; * @date 2021/12/8 20:19 */ @Slf4j -public abstract class AbstractHealthCheck implements ServerHealthCheck, InitializingBean { +public abstract class AbstractHealthCheck implements ServerHealthCheck, InitializingBean, ApplicationListener { /** * Health status @@ -33,6 +35,11 @@ public abstract class AbstractHealthCheck implements ServerHealthCheck, Initiali */ private volatile boolean clientShutdownHook = false; + /** + * Spring context init complete + */ + private boolean contextInitComplete = false; + /** * Health main lock */ @@ -77,7 +84,8 @@ public abstract class AbstractHealthCheck implements ServerHealthCheck, Initiali @Override @SneakyThrows public boolean isHealthStatus() { - while (!healthStatus && !clientShutdownHook) { + while (contextInitComplete + && !healthStatus && !clientShutdownHook) { healthMainLock.lock(); try { healthCondition.await(); @@ -127,4 +135,13 @@ public abstract class AbstractHealthCheck implements ServerHealthCheck, Initiali healthCheckExecutor.scheduleWithFixedDelay(() -> healthCheck(), 0, HEALTH_CHECK_INTERVAL, TimeUnit.SECONDS); } + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + synchronized (ServerHealthCheck.class) { + if (event.getApplicationContext().getParent() == null) { + contextInitComplete = true; + } + } + } + } diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/HttpClientUtil.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/HttpClientUtil.java index 6ba29b43..f156a6a7 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/HttpClientUtil.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/HttpClientUtil.java @@ -1,6 +1,7 @@ package cn.hippo4j.starter.toolkit; import cn.hippo4j.common.toolkit.JSONUtil; +import cn.hippo4j.common.web.exception.ServiceException; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import okhttp3.*; @@ -106,7 +107,7 @@ public class HttpClientUtil { try { return doPost(url, body); } catch (Exception e) { - log.error("httpPost 调用失败. {}", url, e); + log.error("httpPost 调用失败. {} message :: {}", url, e.getMessage()); throw e; } } @@ -165,13 +166,14 @@ public class HttpClientUtil { .url(url) .post(requestBody) .build(); - try (Response resp = hippo4JOkHttpClient.newCall(request).execute()) { + try (Response resp = hippo4JOkHttpClient.newCall(request).execute()) { try (ResponseBody responseBody = resp.body()) { if (resp.code() != HTTP_OK_CODE) { String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s [body] %s", resp.code(), url, jsonBody); - throw new ResponseStatusException(HttpStatus.valueOf(resp.code()), msg); + throw new ServiceException(msg); } - return resp.body().string(); + + return responseBody.string(); } } } @@ -179,13 +181,13 @@ public class HttpClientUtil { @SneakyThrows private byte[] doGet(String url) { Request request = new Request.Builder().get().url(url).build(); - try(Response resp = hippo4JOkHttpClient.newCall(request).execute()){ - try(ResponseBody responseBody = resp.body()){ + try (Response resp = hippo4JOkHttpClient.newCall(request).execute()) { + try (ResponseBody responseBody = resp.body()) { if (resp.code() != HTTP_OK_CODE) { String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url); - throw new ResponseStatusException(HttpStatus.valueOf(resp.code()), msg); + throw new ServiceException(msg); } - return resp.body().bytes(); + return responseBody.bytes(); } } } @@ -204,14 +206,17 @@ public class HttpClientUtil { Call call = hippo4JOkHttpClient.newCall(request); call.timeout().timeout(readTimeoutMs, TimeUnit.MILLISECONDS); - Response resp = call.execute(); - if (resp.code() != HTTP_OK_CODE) { - String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url); - log.error(msg); - throw new RuntimeException(msg); - } + try (Response resp = call.execute()) { + try (ResponseBody responseBody = resp.body()) { + if (resp.code() != HTTP_OK_CODE) { + String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url); + log.error(msg); + throw new ServiceException(msg); + } - return JSONUtil.parseObject(resp.body().string(), clazz); + return JSONUtil.parseObject(responseBody.string(), clazz); + } + } } @SneakyThrows @@ -227,13 +232,17 @@ public class HttpClientUtil { Call call = hippo4JOkHttpClient.newCall(request); call.timeout().timeout(readTimeoutMs, TimeUnit.MILLISECONDS); - Response resp = call.execute(); - if (resp.code() != HTTP_OK_CODE) { - String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s.", resp.code(), url); - log.error(msg); - throw new RuntimeException(msg); + try (Response resp = call.execute()) { + try (ResponseBody responseBody = resp.body()) { + if (resp.code() != HTTP_OK_CODE) { + String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s.", resp.code(), url); + log.error(msg); + throw new ServiceException(msg); + } + + return JSONUtil.parseObject(responseBody.string(), clazz); + } } - return JSONUtil.parseObject(resp.body().string(), clazz); } }