提交 74370269 作者: 方治民

feat: 新增 XxlJobUtil 实现 success/fail 方法搭配 @XxlJob 的 ReturnT<T> 返回值形式同时兼容…

feat: 新增 XxlJobUtil 实现 success/fail 方法搭配 @XxlJob 的 ReturnT<T> 返回值形式同时兼容 UptimeNotice 到 UptimePushAspect 切面的数据提取上报
上级 59f27a58
/* (C) 2024 YiRing, Inc. */ /* (C) 2024 YiRing, Inc. */
package com.yiring.app.job; package com.yiring.app.job;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob; 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;
...@@ -8,12 +9,12 @@ import com.yiring.common.core.Retriever; ...@@ -8,12 +9,12 @@ import com.yiring.common.core.Retriever;
import com.yiring.common.core.UptimeNotice; 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 com.yiring.common.util.XxlJobUtil;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -22,7 +23,8 @@ import org.springframework.stereotype.Component; ...@@ -22,7 +23,8 @@ import org.springframework.stereotype.Component;
*/ */
@Slf4j @Slf4j
@Component @Component
@EnableScheduling // TODO: 测试 @Scheduled 任务调度需要开启
//@EnableScheduling
@RequiredArgsConstructor @RequiredArgsConstructor
public class TestJob { public class TestJob {
...@@ -62,13 +64,13 @@ public class TestJob { ...@@ -62,13 +64,13 @@ public class TestJob {
@SuppressWarnings("unused") @SuppressWarnings("unused")
@UptimePush(key = "BiCC4Jgoa5", group = "Test", name = "[TestNoticeHandler] 测试任务调度") @UptimePush(key = "BiCC4Jgoa5", group = "Test", name = "[TestNoticeHandler] 测试任务调度")
@XxlJob("TestNoticeHandler") @XxlJob("TestNoticeHandler")
public UptimeNotice testNoticeHandler() { public ReturnT<UptimeNotice> testNoticeHandler() {
Random random = new Random(); Random random = new Random();
int randomCount = random.nextInt(10); int randomCount = random.nextInt(10);
if (randomCount < 5) { if (randomCount < 5) {
throw new UptimeException("TestNoticeHandler test err"); return XxlJobUtil.fail("TestNoticeHandler fail");
} }
return UptimeNotice.ok("测试任务调度成功"); return XxlJobUtil.success("TestNoticeHandler success");
} }
} }
/* (C) 2023 YiRing, Inc. */ /* (C) 2023 YiRing, Inc. */
package com.yiring.common.aspect; package com.yiring.common.aspect;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.yiring.common.annotation.UptimePush; import com.yiring.common.annotation.UptimePush;
import com.yiring.common.core.Redis; import com.yiring.common.core.Redis;
...@@ -50,10 +51,21 @@ public class UptimePushAspect { ...@@ -50,10 +51,21 @@ public class UptimePushAspect {
try { try {
result = point.proceed(); result = point.proceed();
// 针对有返回值的 Job 判断是否为 UptimeNotice 类实例 if (Objects.nonNull(result)) {
if (result instanceof UptimeNotice value) { // 针对有返回值的 Job 判断是否为 UptimeNotice 类实例
result = value.getMsg(); if (result instanceof UptimeNotice value) {
status = value.getStatus(); result = value.getMsg();
status = value.getStatus();
} else if ("com.xxl.job.core.biz.model.ReturnT".equals(result.getClass().getName())) {
// 针对 XxlJobUtil.success 和 XxlJobUtil.fail 返回值的 Job 判断是否为 UptimeNotice 类实例
Object content = ReflectUtil.getFieldValue(result, "content");
if (content instanceof UptimeNotice value) {
result = value.getMsg();
status = value.getStatus();
} else {
result = ReflectUtil.getFieldValue(result, "msg");
}
}
} }
// 开始重复计数 // 开始重复计数
......
/* (C) 2024 YiRing, Inc. */
package com.yiring.common.util;
import com.xxl.job.core.biz.model.ReturnT;
import com.yiring.common.core.UptimeNotice;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
/**
* XxlJob 工具类
*
* @author Jim
*/
@SuppressWarnings("unused")
@Slf4j
@UtilityClass
public class XxlJobUtil {
/**
* 成功返回
*
* @param message 返回消息
* @return ReturnT
*/
public ReturnT<UptimeNotice> success(String message) {
ReturnT<UptimeNotice> result = new ReturnT<>();
result.setMsg(message);
result.setCode(ReturnT.SUCCESS_CODE);
result.setContent(UptimeNotice.ok(message));
return result;
}
/**
* 失败返回
*
* @param message 返回消息
* @return ReturnT
*/
public ReturnT<UptimeNotice> fail(String message) {
ReturnT<UptimeNotice> result = new ReturnT<>();
result.setMsg(message);
result.setCode(ReturnT.FAIL_CODE);
result.setContent(UptimeNotice.no(message));
return result;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论