初始化项目
This commit is contained in:
23
xtools-app-common/xtools-app-common-job/pom.xml
Normal file
23
xtools-app-common/xtools-app-common-job/pom.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-app-common</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-app-common-job</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- 项目模块 begin -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-app-common-cache</artifactId>
|
||||
</dependency>
|
||||
<!-- 项目模块 end -->
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,133 @@
|
||||
package xtools.app.common.job.base;
|
||||
|
||||
import xtools.app.common.cache.enums.AppCache;
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.boot.cache.redis.base.RedisService;
|
||||
import xtools.boot.cache.redis.utils.RedisUtils;
|
||||
import xtools.boot.core.interfaces.JobInterface;
|
||||
import xtools.boot.log.LogBus;
|
||||
import xtools.boot.log.enums.LogBusBaseType;
|
||||
import xtools.boot.log.holder.LogTrackHolder;
|
||||
import xtools.core.UuidUtils;
|
||||
import xtools.core.enums.LogLevel;
|
||||
import xtools.core.extend.CheckUtils;
|
||||
import xtools.core.time.InstantUtils;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>Title : BaseJob</p>
|
||||
* <p>Description : BaseJob</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 1.0.0
|
||||
* @date : 2026/2/23 17:36
|
||||
*/
|
||||
public abstract class BaseJob implements Runnable, BaseParams, JobInterface {
|
||||
|
||||
/**
|
||||
* 锁参数
|
||||
*/
|
||||
private static final AppCache LOCK_PARAM = AppCache.LOCK_JOB;
|
||||
|
||||
/**
|
||||
* 运行作业
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
ScopedValue.where(LogTrackHolder.getScoped(), LogTrackHolder.newMain()).run(this::exec);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行作业
|
||||
*/
|
||||
private void exec() {
|
||||
// 获取执行的class名称
|
||||
String className = getClass().getSimpleName();
|
||||
// 标题
|
||||
String title = "[" + className + "]";
|
||||
// 获取本次作业名称
|
||||
String jobName = null;
|
||||
// 获取redis服务
|
||||
RedisService redisService = null;
|
||||
// 锁
|
||||
boolean locked = false;
|
||||
try {
|
||||
// 记录日志
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.JOB).title(title + "开始执行").save();
|
||||
|
||||
jobName = className + CP_LINE + InstantUtils.format(Instant.now(), "yyyyMMddHHmmssSSS") + CP_LINE + UuidUtils.get();
|
||||
redisService = RedisUtils.get();
|
||||
// 获取超时时间
|
||||
Long expireTime = expireTime();
|
||||
if (!CheckUtils.id(expireTime)) {
|
||||
expireTime = LOCK_PARAM.expireTime();
|
||||
}
|
||||
// 加锁
|
||||
locked = redisService.tryLock(LOCK_PARAM.key() + className, jobName, expireTime);
|
||||
if (!locked) {
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.JOB).title(title + "已存在").save();
|
||||
return;
|
||||
}
|
||||
|
||||
runJob();
|
||||
} catch (Exception e) {
|
||||
LogBus.init(LogLevel.ERROR, LogBusBaseType.JOB).title(title + "运行异常").error(e).save();
|
||||
} finally {
|
||||
if (Objects.nonNull(redisService) && locked) {
|
||||
boolean released = redisService.releaseLock(LOCK_PARAM.key() + className, jobName);
|
||||
if (!released) {
|
||||
LogBus.init(LogLevel.ERROR, LogBusBaseType.JOB).title(title + "锁释放异常").save();
|
||||
} else {
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.JOB).title(title + "执行成功").save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行任务
|
||||
*/
|
||||
@Override
|
||||
public void execute() {
|
||||
// 标题
|
||||
String title = "[" + getClass().getSimpleName() + "]";
|
||||
ScopedValue.where(LogTrackHolder.getScoped(), LogTrackHolder.newMain()).run(() -> {
|
||||
Exception e = null;
|
||||
try {
|
||||
// 记录日志
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.JOB).title(title + "开始执行").save();
|
||||
runJob();
|
||||
} catch (Exception ex) {
|
||||
e = ex;
|
||||
} finally {
|
||||
if (Objects.nonNull(e)) {
|
||||
LogBus.init(LogLevel.ERROR, LogBusBaseType.JOB).title(title + "运行异常").error(e).save();
|
||||
} else {
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.JOB).title(title + "执行成功").save();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取锁超时时间
|
||||
*
|
||||
* @return 锁超时时间
|
||||
*/
|
||||
public Long expireTime() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行作业
|
||||
*
|
||||
* @throws Exception 运行异常
|
||||
*/
|
||||
public abstract void runJob() throws Exception;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user