服务停止时释放对应资源.

pull/12/head
chen.ma 3 years ago
parent c3395f8e7a
commit 00a428c88b

@ -1,14 +1,15 @@
package cn.hippo4j.starter.core; package cn.hippo4j.starter.core;
import cn.hippo4j.starter.remote.HttpAgent;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import cn.hippo4j.common.model.PoolParameterInfo; import cn.hippo4j.common.model.PoolParameterInfo;
import cn.hippo4j.common.toolkit.ContentUtil; import cn.hippo4j.common.toolkit.ContentUtil;
import cn.hippo4j.common.toolkit.GroupKey; import cn.hippo4j.common.toolkit.GroupKey;
import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.starter.remote.HttpAgent;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.net.URLDecoder; import java.net.URLDecoder;
@ -28,7 +29,7 @@ import static cn.hippo4j.common.constant.Constants.*;
* @date 2021/6/20 18:34 * @date 2021/6/20 18:34
*/ */
@Slf4j @Slf4j
public class ClientWorker { public class ClientWorker implements DisposableBean {
private double currentLongingTaskCount = 0; private double currentLongingTaskCount = 0;
@ -93,6 +94,12 @@ public class ClientWorker {
} }
} }
@Override
public void destroy() throws Exception {
Optional.ofNullable(executor).ifPresent((each) -> each.shutdown());
Optional.ofNullable(executorService).ifPresent((each) -> each.shutdown());
}
class LongPollingRunnable implements Runnable { class LongPollingRunnable implements Runnable {
@SneakyThrows @SneakyThrows

@ -1,16 +1,18 @@
package cn.hippo4j.starter.core; package cn.hippo4j.starter.core;
import cn.hippo4j.starter.remote.HttpAgent;
import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder;
import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hippo4j.common.constant.Constants; import cn.hippo4j.common.constant.Constants;
import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.model.InstanceInfo;
import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.common.web.base.Results; import cn.hippo4j.common.web.base.Results;
import cn.hippo4j.common.web.exception.ErrorCodeEnum; import cn.hippo4j.common.web.exception.ErrorCodeEnum;
import cn.hippo4j.starter.remote.HttpAgent;
import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder;
import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import java.util.Optional;
import java.util.concurrent.*; import java.util.concurrent.*;
/** /**
@ -20,7 +22,7 @@ import java.util.concurrent.*;
* @date 2021/7/13 21:51 * @date 2021/7/13 21:51
*/ */
@Slf4j @Slf4j
public class DiscoveryClient { public class DiscoveryClient implements DisposableBean {
private final ThreadPoolExecutor heartbeatExecutor; private final ThreadPoolExecutor heartbeatExecutor;
@ -68,7 +70,7 @@ public class DiscoveryClient {
log.info("{}{} - registering service...", PREFIX, appPathIdentifier); log.info("{}{} - registering service...", PREFIX, appPathIdentifier);
String urlPath = Constants.BASE_PATH + "/apps/register/"; String urlPath = Constants.BASE_PATH + "/apps/register/";
Result registerResult = null; Result registerResult;
try { try {
registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo); registerResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo);
} catch (Exception ex) { } catch (Exception ex) {
@ -83,6 +85,23 @@ public class DiscoveryClient {
return registerResult.isSuccess(); return registerResult.isSuccess();
} }
@Override
public void destroy() throws Exception {
log.info("{}{} - destroy service...", PREFIX, appPathIdentifier);
String urlPath = Constants.BASE_PATH + "/apps/remove/";
Result removeResult;
try {
removeResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo);
if (removeResult.isSuccess()) {
log.info("{}{} - destroy service success...", PREFIX, appPathIdentifier);
}
} catch (Throwable ex) {
log.error("{}{} - destroy service fail...", PREFIX, appPathIdentifier);
}
Optional.ofNullable(scheduler).ifPresent((each) -> each.shutdown());
}
public class HeartbeatThread implements Runnable { public class HeartbeatThread implements Runnable {
@Override @Override
@ -94,7 +113,7 @@ public class DiscoveryClient {
} }
boolean renew() { boolean renew() {
Result renewResult = null; Result renewResult;
try { try {
InstanceInfo.InstanceRenew instanceRenew = new InstanceInfo.InstanceRenew() InstanceInfo.InstanceRenew instanceRenew = new InstanceInfo.InstanceRenew()
.setAppName(instanceInfo.getAppName()) .setAppName(instanceInfo.getAppName())
@ -103,7 +122,6 @@ public class DiscoveryClient {
.setStatus(instanceInfo.getStatus().toString()); .setStatus(instanceInfo.getStatus().toString());
renewResult = httpAgent.httpPostByDiscovery(Constants.BASE_PATH + "/apps/renew", instanceRenew); renewResult = httpAgent.httpPostByDiscovery(Constants.BASE_PATH + "/apps/renew", instanceRenew);
if (StrUtil.equals(ErrorCodeEnum.NOT_FOUND.getCode(), renewResult.getCode())) { if (StrUtil.equals(ErrorCodeEnum.NOT_FOUND.getCode(), renewResult.getCode())) {
long timestamp = instanceInfo.setIsDirtyWithTime(); long timestamp = instanceInfo.setIsDirtyWithTime();
boolean success = register(); boolean success = register();
@ -112,6 +130,7 @@ public class DiscoveryClient {
} }
return success; return success;
} }
return renewResult.isSuccess(); return renewResult.isSuccess();
} catch (Exception ex) { } catch (Exception ex) {
log.error(PREFIX + "{} - was unable to send heartbeat!", appPathIdentifier, ex); log.error(PREFIX + "{} - was unable to send heartbeat!", appPathIdentifier, ex);

Loading…
Cancel
Save