diff --git a/snail-job-client/snail-job-client-common/src/main/java/com/aizuda/snailjob/client/common/exception/SnailJobRemoteException.java b/snail-job-client/snail-job-client-common/src/main/java/com/aizuda/snailjob/client/common/exception/SnailJobRemoteException.java new file mode 100644 index 00000000..92c8852e --- /dev/null +++ b/snail-job-client/snail-job-client-common/src/main/java/com/aizuda/snailjob/client/common/exception/SnailJobRemoteException.java @@ -0,0 +1,38 @@ +package com.aizuda.snailjob.client.common.exception; + +import com.aizuda.snailjob.common.core.exception.BaseSnailJobException; + +/** + * @author: opensnail + * @date : 2022-03-03 14:49 + */ +public class SnailJobRemoteException extends BaseSnailJobException { + + public SnailJobRemoteException(String message) { + super(message); + } + + public SnailJobRemoteException(String message, Throwable cause) { + super(message, cause); + } + + public SnailJobRemoteException(Throwable cause) { + super(cause); + } + + public SnailJobRemoteException(String message, Object... arguments) { + super(message, arguments); + } + + public SnailJobRemoteException(String message, Object[] arguments, Throwable cause) { + super(message, arguments, cause); + } + + public SnailJobRemoteException(String message, Object argument, Throwable cause) { + super(message, argument, cause); + } + + public SnailJobRemoteException(String message, Object argument) { + super(message, argument); + } +} diff --git a/snail-job-client/snail-job-client-common/src/main/java/com/aizuda/snailjob/client/common/rpc/client/NettyChannel.java b/snail-job-client/snail-job-client-common/src/main/java/com/aizuda/snailjob/client/common/rpc/client/NettyChannel.java index 2e9b34bc..db9b2188 100644 --- a/snail-job-client/snail-job-client-common/src/main/java/com/aizuda/snailjob/client/common/rpc/client/NettyChannel.java +++ b/snail-job-client/snail-job-client-common/src/main/java/com/aizuda/snailjob/client/common/rpc/client/NettyChannel.java @@ -4,19 +4,15 @@ import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import com.aizuda.snailjob.client.common.cache.GroupVersionCache; import com.aizuda.snailjob.client.common.config.SnailJobProperties; +import com.aizuda.snailjob.client.common.exception.SnailJobRemoteException; import com.aizuda.snailjob.common.core.constant.SystemConstants; import com.aizuda.snailjob.common.core.context.SpringContext; import com.aizuda.snailjob.common.core.enums.HeadersEnum; -import com.aizuda.snailjob.common.log.SnailJobLog; import com.aizuda.snailjob.common.core.util.NetUtil; +import com.aizuda.snailjob.common.log.SnailJobLog; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; -import io.netty.handler.codec.http.DefaultFullHttpRequest; -import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpHeaderNames; -import io.netty.handler.codec.http.HttpHeaderValues; -import io.netty.handler.codec.http.HttpMethod; -import io.netty.handler.codec.http.HttpVersion; +import io.netty.handler.codec.http.*; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.web.ServerProperties; @@ -179,12 +175,16 @@ public class NettyChannel { .set(HeadersEnum.VERSION.getKey(), GroupVersionCache.getVersion()) .set(HeadersEnum.HOST.getKey(), serverConfig.getHost()) .set(HeadersEnum.NAMESPACE.getKey(), Optional.ofNullable(snailJobProperties.getNamespace()).orElse( - SystemConstants.DEFAULT_NAMESPACE)) + SystemConstants.DEFAULT_NAMESPACE)) .set(HeadersEnum.TOKEN.getKey(), Optional.ofNullable(snailJobProperties.getToken()).orElse( SystemConstants.DEFAULT_TOKEN)) ; //发送数据 - CHANNEL.writeAndFlush(request).sync(); + try { + CHANNEL.writeAndFlush(request).sync(); + } catch (Exception exception) { + throw new SnailJobRemoteException("网络异常"); + } } } diff --git a/snail-job-common/snail-job-common-core/src/main/java/com/aizuda/snailjob/common/core/alarm/strategy/WebhookAlarm.java b/snail-job-common/snail-job-common-core/src/main/java/com/aizuda/snailjob/common/core/alarm/strategy/WebhookAlarm.java index 7e93663c..d695630b 100644 --- a/snail-job-common/snail-job-common-core/src/main/java/com/aizuda/snailjob/common/core/alarm/strategy/WebhookAlarm.java +++ b/snail-job-common/snail-job-common-core/src/main/java/com/aizuda/snailjob/common/core/alarm/strategy/WebhookAlarm.java @@ -34,24 +34,21 @@ public class WebhookAlarm extends AbstractAlarm { public boolean syncSendMessage(AlarmContext alarmContext) { WebhookAttribute webhookAttribute = JsonUtil.parseObject(alarmContext.getNotifyAttribute(), WebhookAttribute.class); - for (int i = 0; i < 2; i++) { - try { - WebhookMessage webhookMessage = WebhookMessage.builder().text(alarmContext.getTitle()).build(); + try { + WebhookMessage webhookMessage = WebhookMessage.builder().text(alarmContext.getTitle()).build(); - HttpRequest post = HttpUtil.createPost(webhookAttribute.getWebhookUrl()); - HttpRequest request = post.body(JsonUtil.toJsonString(webhookMessage), ContentTypeEnum.valueOf(webhookAttribute.getContentType()).getMediaType().toString()) - .header(SystemConstants.SECRET, webhookAttribute.getSecret()); - HttpResponse execute = request.execute(); - if (execute.isOk()) { - return true; - } - SnailJobLog.LOCAL.info("发送Webhook告警结果. webHook:[{}],结果: [{}]", webhookAttribute.getWebhookUrl(), execute.body()); - break; - } catch (Exception e) { - SnailJobLog.LOCAL.error("发送Webhook告警异常. webHook:[{}]", webhookAttribute, e); + HttpRequest post = HttpUtil.createPost(webhookAttribute.getWebhookUrl()); + HttpRequest request = post.body(JsonUtil.toJsonString(webhookMessage), ContentTypeEnum.valueOf(webhookAttribute.getContentType()).getMediaType().toString()) + .header(SystemConstants.SECRET, webhookAttribute.getSecret()); + HttpResponse execute = request.execute(); + SnailJobLog.LOCAL.info("发送Webhook告警结果. webHook:[{}],结果: [{}]", webhookAttribute.getWebhookUrl(), execute.body()); + if (execute.isOk()) { + return true; } + } catch (Exception e) { + SnailJobLog.LOCAL.error("发送Webhook告警异常. webHook:[{}]", webhookAttribute, e); + return false; } - return true; } @@ -71,4 +68,3 @@ public class WebhookAlarm extends AbstractAlarm { return true; } } -