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

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 {
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) {

@ -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()) {

@ -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<ContextRefreshedEvent> {
/**
* 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;
}
}
}
}

@ -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;
}
}
@ -169,9 +170,10 @@ public class HttpClientUtil {
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();
}
}
}
@ -183,9 +185,9 @@ public class HttpClientUtil {
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();
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 RuntimeException(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();
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 RuntimeException(msg);
throw new ServiceException(msg);
}
return JSONUtil.parseObject(responseBody.string(), clazz);
}
}
return JSONUtil.parseObject(resp.body().string(), clazz);
}
}

Loading…
Cancel
Save