重构日志打印及连接流异常捕获.

pull/41/head v1.0.0-alpha
chen.ma 3 years ago
parent f71e49ff44
commit cab4bbbad3

@ -126,7 +126,7 @@ public class BaseSendMessageService implements InitializingBean, SendMessageServ
try { try {
result = httpAgent.httpPostByDiscovery(BASE_PATH + "/notify/list/config", new ThreadPoolNotifyReqDTO(groupKeys)); result = httpAgent.httpPostByDiscovery(BASE_PATH + "/notify/list/config", new ThreadPoolNotifyReqDTO(groupKeys));
} catch (Throwable ex) { } 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) { if (result != null && result.isSuccess() && result.getData() != null) {

@ -71,7 +71,7 @@ public class DiscoveryClient implements DisposableBean {
registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo); registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo);
} catch (Exception ex) { } catch (Exception ex) {
registerResult = Results.failure(ErrorCodeEnum.SERVICE_ERROR); 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()) { if (log.isInfoEnabled()) {

@ -5,6 +5,8 @@ import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ScheduledThreadPoolExecutor; 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 * @date 2021/12/8 20:19
*/ */
@Slf4j @Slf4j
public abstract class AbstractHealthCheck implements ServerHealthCheck, InitializingBean { public abstract class AbstractHealthCheck implements ServerHealthCheck, InitializingBean, ApplicationListener<ContextRefreshedEvent> {
/** /**
* Health status * Health status
@ -33,6 +35,11 @@ public abstract class AbstractHealthCheck implements ServerHealthCheck, Initiali
*/ */
private volatile boolean clientShutdownHook = false; private volatile boolean clientShutdownHook = false;
/**
* Spring context init complete
*/
private boolean contextInitComplete = false;
/** /**
* Health main lock * Health main lock
*/ */
@ -77,7 +84,8 @@ public abstract class AbstractHealthCheck implements ServerHealthCheck, Initiali
@Override @Override
@SneakyThrows @SneakyThrows
public boolean isHealthStatus() { public boolean isHealthStatus() {
while (!healthStatus && !clientShutdownHook) { while (contextInitComplete
&& !healthStatus && !clientShutdownHook) {
healthMainLock.lock(); healthMainLock.lock();
try { try {
healthCondition.await(); healthCondition.await();
@ -127,4 +135,13 @@ public abstract class AbstractHealthCheck implements ServerHealthCheck, Initiali
healthCheckExecutor.scheduleWithFixedDelay(() -> healthCheck(), 0, HEALTH_CHECK_INTERVAL, TimeUnit.SECONDS); 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;
}
}
}
} }

@ -1,6 +1,7 @@
package cn.hippo4j.starter.toolkit; package cn.hippo4j.starter.toolkit;
import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.common.web.exception.ServiceException;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.*; import okhttp3.*;
@ -106,7 +107,7 @@ public class HttpClientUtil {
try { try {
return doPost(url, body); return doPost(url, body);
} catch (Exception e) { } catch (Exception e) {
log.error("httpPost 调用失败. {}", url, e); log.error("httpPost 调用失败. {} message :: {}", url, e.getMessage());
throw e; throw e;
} }
} }
@ -165,13 +166,14 @@ public class HttpClientUtil {
.url(url) .url(url)
.post(requestBody) .post(requestBody)
.build(); .build();
try (Response resp = hippo4JOkHttpClient.newCall(request).execute()) { try (Response resp = hippo4JOkHttpClient.newCall(request).execute()) {
try (ResponseBody responseBody = resp.body()) { try (ResponseBody responseBody = resp.body()) {
if (resp.code() != HTTP_OK_CODE) { if (resp.code() != HTTP_OK_CODE) {
String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s [body] %s", resp.code(), url, jsonBody); 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 @SneakyThrows
private byte[] doGet(String url) { private byte[] doGet(String url) {
Request request = new Request.Builder().get().url(url).build(); Request request = new Request.Builder().get().url(url).build();
try(Response resp = hippo4JOkHttpClient.newCall(request).execute()){ try (Response resp = hippo4JOkHttpClient.newCall(request).execute()) {
try(ResponseBody responseBody = resp.body()){ try (ResponseBody responseBody = resp.body()) {
if (resp.code() != HTTP_OK_CODE) { if (resp.code() != HTTP_OK_CODE) {
String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url); 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 call = hippo4JOkHttpClient.newCall(request);
call.timeout().timeout(readTimeoutMs, TimeUnit.MILLISECONDS); call.timeout().timeout(readTimeoutMs, TimeUnit.MILLISECONDS);
Response resp = call.execute(); try (Response resp = call.execute()) {
if (resp.code() != HTTP_OK_CODE) { try (ResponseBody responseBody = resp.body()) {
String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url); if (resp.code() != HTTP_OK_CODE) {
log.error(msg); String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url);
throw new RuntimeException(msg); log.error(msg);
} throw new ServiceException(msg);
}
return JSONUtil.parseObject(resp.body().string(), clazz); return JSONUtil.parseObject(responseBody.string(), clazz);
}
}
} }
@SneakyThrows @SneakyThrows
@ -227,13 +232,17 @@ public class HttpClientUtil {
Call call = hippo4JOkHttpClient.newCall(request); Call call = hippo4JOkHttpClient.newCall(request);
call.timeout().timeout(readTimeoutMs, TimeUnit.MILLISECONDS); call.timeout().timeout(readTimeoutMs, TimeUnit.MILLISECONDS);
Response resp = call.execute(); try (Response resp = call.execute()) {
if (resp.code() != HTTP_OK_CODE) { try (ResponseBody responseBody = resp.body()) {
String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s.", resp.code(), url); if (resp.code() != HTTP_OK_CODE) {
log.error(msg); String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s.", resp.code(), url);
throw new RuntimeException(msg); log.error(msg);
throw new ServiceException(msg);
}
return JSONUtil.parseObject(responseBody.string(), clazz);
}
} }
return JSONUtil.parseObject(resp.body().string(), clazz);
} }
} }

Loading…
Cancel
Save