refactor(core): 重构任务上下文与日志时间字段

- 将 XxlJobContext 中的 logDateTime 字段重命名为 jobLogTime
- 更新构造函数参数顺序并移除旧的日志时间获取方法
- 在 XxlJobHelper 中新增 getJobLogTime 方法替代原有的 getLogDateTime
- 优化日志格式化字符串拼接逻辑
- 统一代码注释风格并完善上下文管理工具类注释
- 调整 TriggerRequest 类字段分组注释以提高可读性
- 修正 JobThread 和 TriggerCallbackThread 中的日志相关调用顺序
- 设置 logger 为 final 类
3.3.0-release
xuxueli 1 month ago
parent cbf6933cce
commit 3ee773e215

@ -27,14 +27,14 @@ public class XxlJobContext {
// ---------------------- for log ----------------------
/**
* job log filename
* job log timestamp
*/
private final String jobLogFileName;
private final long jobLogTime;
/**
* log date time
* job log filename
*/
private final long logDateTime;
private final String jobLogFileName;
// ---------------------- for shard ----------------------
@ -66,11 +66,16 @@ public class XxlJobContext {
private String handleMsg;
public XxlJobContext(long jobId, String jobParam, String jobLogFileName, long logDateTime, int shardIndex, int shardTotal) {
public XxlJobContext(long jobId,
String jobParam,
long jobLogTime,
String jobLogFileName,
int shardIndex,
int shardTotal) {
this.jobId = jobId;
this.jobParam = jobParam;
this.jobLogTime = jobLogTime;
this.jobLogFileName = jobLogFileName;
this.logDateTime = logDateTime;
this.shardIndex = shardIndex;
this.shardTotal = shardTotal;
@ -89,8 +94,8 @@ public class XxlJobContext {
return jobLogFileName;
}
public long getLogDateTime() {
return logDateTime;
public long getJobLogTime() {
return jobLogTime;
}
public int getShardIndex() {
@ -119,12 +124,21 @@ public class XxlJobContext {
// ---------------------- tool ----------------------
private static InheritableThreadLocal<XxlJobContext> contextHolder = new InheritableThreadLocal<XxlJobContext>(); // support for child thread of job handler)
/**
* xxl-job context store
*/
private static final InheritableThreadLocal<XxlJobContext> contextHolder = new InheritableThreadLocal<XxlJobContext>(); // support for child thread of job handler)
/**
* set xxl-job context
*/
public static void setXxlJobContext(XxlJobContext xxlJobContext){
contextHolder.set(xxlJobContext);
}
/**
* get xxl-job context
*/
public static XxlJobContext getXxlJobContext(){
return contextHolder.get();
}

@ -51,31 +51,31 @@ public class XxlJobHelper {
// ---------------------- for log ----------------------
/**
* current JobLogFileName
* current job log time
*
* @return logFileName
* @return logDateTime
*/
public static String getJobLogFileName() {
public static long getJobLogTime() {
XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext();
if (xxlJobContext == null) {
return null;
return -1;
}
return xxlJobContext.getJobLogFileName();
return xxlJobContext.getJobLogTime();
}
/**
* current LogDateTime
* current job log filename
*
* @return logDateTime
* @return logFileName
*/
public static long getLogDateTime() {
public static String getJobLogFileName() {
XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext();
if (xxlJobContext == null) {
return -1;
return null;
}
return xxlJobContext.getLogDateTime();
return xxlJobContext.getJobLogFileName();
}
// ---------------------- for shard ----------------------
@ -164,13 +164,11 @@ public class XxlJobHelper {
StackTraceElement[] stackTraceElements = new Throwable().getStackTrace();
StackTraceElement callInfo = stackTraceElements[1];*/
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(DateTool.formatDateTime(new Date())).append(" ")
.append("["+ callInfo.getClassName() + "#" + callInfo.getMethodName() +"]").append("-")
.append("["+ callInfo.getLineNumber() +"]").append("-")
.append("["+ Thread.currentThread().getName() +"]").append(" ")
.append(appendLog!=null?appendLog:"");
String formatAppendLog = stringBuffer.toString();
String formatAppendLog = DateTool.formatDateTime(new Date()) + " " +
"[" + callInfo.getClassName() + "#" + callInfo.getMethodName() + "]" + "-" +
"[" + callInfo.getLineNumber() + "]" + "-" +
"[" + Thread.currentThread().getName() + "]" + " " +
(appendLog != null ? appendLog : "");
// appendlog
String logFileName = xxlJobContext.getJobLogFileName();

@ -8,20 +8,25 @@ import java.io.Serializable;
public class TriggerRequest implements Serializable{
private static final long serialVersionUID = 42L;
// job base info
private int jobId;
// job execute info
private String executorHandler;
private String executorParams;
private String executorBlockStrategy;
private int executorTimeout;
// log info
private long logId;
private long logDateTime;
// glue info
private String glueType;
private String glueSource;
private long glueUpdatetime;
// broadcast info
private int broadcastIndex;
private int broadcastTotal;

@ -23,7 +23,7 @@ import java.util.concurrent.*;
* @author xuxueli 2016-1-16 19:52:47
*/
public class JobThread extends Thread{
private static Logger logger = LoggerFactory.getLogger(JobThread.class);
private static final Logger logger = LoggerFactory.getLogger(JobThread.class);
private int jobId;
private IJobHandler handler;
@ -53,9 +53,6 @@ public class JobThread extends Thread{
/**
* new trigger to queue
*
* @param triggerParam
* @return
*/
public Response<String> pushTriggerQueue(TriggerRequest triggerParam) {
// avoid repeat
@ -71,8 +68,6 @@ public class JobThread extends Thread{
/**
* kill job thread
*
* @param stopReason
*/
public void toStop(String stopReason) {
/**
@ -86,7 +81,6 @@ public class JobThread extends Thread{
/**
* is running job
* @return
*/
public boolean isRunningOrHasQueue() {
return running || triggerQueue.size()>0;
@ -121,8 +115,8 @@ public class JobThread extends Thread{
XxlJobContext xxlJobContext = new XxlJobContext(
triggerParam.getJobId(),
triggerParam.getExecutorParams(),
logFileName,
triggerParam.getLogDateTime(),
triggerParam.getLogDateTime(),
logFileName,
triggerParam.getBroadcastIndex(),
triggerParam.getBroadcastTotal());

@ -207,8 +207,8 @@ public class TriggerCallbackThread {
XxlJobContext.setXxlJobContext(new XxlJobContext(
-1,
null,
logFileName,
-1,
logFileName,
-1,
-1));
XxlJobHelper.log(logContent);

Loading…
Cancel
Save