提交 93521943 作者: 方治民

feat: 优化 UptimePushAspect 在 XxlJob 调度时对于正常返回值的消息和状态处理,补充 XxlJob 调度示例

上级 ca111cef
/* (C) 2024 YiRing, Inc. */ /* (C) 2024 YiRing, Inc. */
package com.yiring.app.job; package com.yiring.app.job;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yiring.common.annotation.UptimePush; import com.yiring.common.annotation.UptimePush;
import com.yiring.common.constant.DateFormatter; import com.yiring.common.constant.DateFormatter;
import com.yiring.common.core.Retriever; import com.yiring.common.core.Retriever;
import com.yiring.common.core.UptimeNotice;
import com.yiring.common.exception.UptimeException; import com.yiring.common.exception.UptimeException;
import com.yiring.common.util.Uptime; import com.yiring.common.util.Uptime;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -32,12 +34,12 @@ public class TestJob { ...@@ -32,12 +34,12 @@ public class TestJob {
@UptimePush( @UptimePush(
key = "BiCC4Jgoa5", key = "BiCC4Jgoa5",
group = "Test", group = "Test",
name = "测试任务调度", name = "[TestScheduled] 测试任务调度",
retryCount = 3, retryCount = 3,
retryStatus = Uptime.Status.UP retryStatus = Uptime.Status.UP
) )
@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS) @Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
public void test() { public void testScheduled() {
String time = LocalDateTime.now().format(DateFormatter.DATE_TIME); String time = LocalDateTime.now().format(DateFormatter.DATE_TIME);
log.info("TestJobHandler: {}", time); log.info("TestJobHandler: {}", time);
...@@ -56,4 +58,17 @@ public class TestJob { ...@@ -56,4 +58,17 @@ public class TestJob {
log.info("[Test] result: {}", result); log.info("[Test] result: {}", result);
} }
@SuppressWarnings("unused")
@UptimePush(key = "BiCC4Jgoa5", group = "Test", name = "[TestNoticeHandler] 测试任务调度")
@XxlJob("TestNoticeHandler")
public UptimeNotice testNoticeHandler() {
Random random = new Random();
int randomCount = random.nextInt(10);
if (randomCount < 5) {
throw new UptimeException("TestNoticeHandler test err");
}
return UptimeNotice.ok("测试任务调度成功");
}
} }
...@@ -32,12 +32,12 @@ public @interface UptimePush { ...@@ -32,12 +32,12 @@ public @interface UptimePush {
String name() default ""; String name() default "";
/** /**
* 分组名 * 分组名称
*/ */
String group() default ""; String group() default "";
/** /**
* 重试次数 * 重试次数, 默认为 0 不重试
*/ */
int retryCount() default 0; int retryCount() default 0;
......
...@@ -41,6 +41,8 @@ public class UptimePushAspect { ...@@ -41,6 +41,8 @@ public class UptimePushAspect {
int retryCount = annotation.retryCount(); int retryCount = annotation.retryCount();
// 获取当前的重试次数 // 获取当前的重试次数
Integer currentRetryCount = redis.get(redisKey, Integer.class); Integer currentRetryCount = redis.get(redisKey, Integer.class);
// 默认状态 UP
Uptime.Status status = Uptime.Status.UP;
Object result = null; Object result = null;
String err = null; String err = null;
...@@ -51,7 +53,7 @@ public class UptimePushAspect { ...@@ -51,7 +53,7 @@ public class UptimePushAspect {
// 针对有返回值的 Job 判断是否为 UptimeNotice 类实例 // 针对有返回值的 Job 判断是否为 UptimeNotice 类实例
if (result instanceof UptimeNotice value) { if (result instanceof UptimeNotice value) {
result = value.getMsg(); result = value.getMsg();
throw new UptimeException(value.getMsg()); status = value.getStatus();
} }
// 开始重复计数 // 开始重复计数
...@@ -64,9 +66,6 @@ public class UptimePushAspect { ...@@ -64,9 +66,6 @@ public class UptimePushAspect {
throw e; throw e;
} }
} finally { } finally {
// 获取重试时的状态标记
Uptime.Status status = Uptime.Status.UP;
// 构建消息内容集合 // 构建消息内容集合
List<String> texts = new ArrayList<>(); List<String> texts = new ArrayList<>();
if (StrUtil.isNotBlank(annotation.group())) { if (StrUtil.isNotBlank(annotation.group())) {
...@@ -86,6 +85,7 @@ public class UptimePushAspect { ...@@ -86,6 +85,7 @@ public class UptimePushAspect {
// 判断是否有异常消息 // 判断是否有异常消息
if (StrUtil.isNotBlank(err)) { if (StrUtil.isNotBlank(err)) {
// 有异常时默认状态为 DOWN
status = Uptime.Status.DOWN; status = Uptime.Status.DOWN;
if (retryCount > 0) { if (retryCount > 0) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论