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

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

@ -27,14 +27,14 @@ public class XxlJobContext {
// ---------------------- for log ---------------------- // ---------------------- 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 ---------------------- // ---------------------- for shard ----------------------
@ -66,11 +66,16 @@ public class XxlJobContext {
private String handleMsg; 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.jobId = jobId;
this.jobParam = jobParam; this.jobParam = jobParam;
this.jobLogTime = jobLogTime;
this.jobLogFileName = jobLogFileName; this.jobLogFileName = jobLogFileName;
this.logDateTime = logDateTime;
this.shardIndex = shardIndex; this.shardIndex = shardIndex;
this.shardTotal = shardTotal; this.shardTotal = shardTotal;
@ -89,8 +94,8 @@ public class XxlJobContext {
return jobLogFileName; return jobLogFileName;
} }
public long getLogDateTime() { public long getJobLogTime() {
return logDateTime; return jobLogTime;
} }
public int getShardIndex() { public int getShardIndex() {
@ -119,12 +124,21 @@ public class XxlJobContext {
// ---------------------- tool ---------------------- // ---------------------- 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){ public static void setXxlJobContext(XxlJobContext xxlJobContext){
contextHolder.set(xxlJobContext); contextHolder.set(xxlJobContext);
} }
/**
* get xxl-job context
*/
public static XxlJobContext getXxlJobContext(){ public static XxlJobContext getXxlJobContext(){
return contextHolder.get(); return contextHolder.get();
} }

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

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

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

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

Loading…
Cancel
Save