Skip to content

Commit 3ee773e

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

5 files changed

Lines changed: 48 additions & 37 deletions

File tree

xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobContext.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public class XxlJobContext {
2727
// ---------------------- for log ----------------------
2828

2929
/**
30-
* job log filename
30+
* job log timestamp
3131
*/
32-
private final String jobLogFileName;
33-
32+
private final long jobLogTime;
33+
3434
/**
35-
* log date time
35+
* job log filename
3636
*/
37-
private final long logDateTime;
37+
private final String jobLogFileName;
3838

3939
// ---------------------- for shard ----------------------
4040

@@ -66,11 +66,16 @@ public class XxlJobContext {
6666
private String handleMsg;
6767

6868

69-
public XxlJobContext(long jobId, String jobParam, String jobLogFileName, long logDateTime, int shardIndex, int shardTotal) {
69+
public XxlJobContext(long jobId,
70+
String jobParam,
71+
long jobLogTime,
72+
String jobLogFileName,
73+
int shardIndex,
74+
int shardTotal) {
7075
this.jobId = jobId;
7176
this.jobParam = jobParam;
77+
this.jobLogTime = jobLogTime;
7278
this.jobLogFileName = jobLogFileName;
73-
this.logDateTime = logDateTime;
7479
this.shardIndex = shardIndex;
7580
this.shardTotal = shardTotal;
7681

@@ -89,8 +94,8 @@ public String getJobLogFileName() {
8994
return jobLogFileName;
9095
}
9196

92-
public long getLogDateTime() {
93-
return logDateTime;
97+
public long getJobLogTime() {
98+
return jobLogTime;
9499
}
95100

96101
public int getShardIndex() {
@@ -119,12 +124,21 @@ public String getHandleMsg() {
119124

120125
// ---------------------- tool ----------------------
121126

122-
private static InheritableThreadLocal<XxlJobContext> contextHolder = new InheritableThreadLocal<XxlJobContext>(); // support for child thread of job handler)
127+
/**
128+
* xxl-job context store
129+
*/
130+
private static final InheritableThreadLocal<XxlJobContext> contextHolder = new InheritableThreadLocal<XxlJobContext>(); // support for child thread of job handler)
123131

132+
/**
133+
* set xxl-job context
134+
*/
124135
public static void setXxlJobContext(XxlJobContext xxlJobContext){
125136
contextHolder.set(xxlJobContext);
126137
}
127138

139+
/**
140+
* get xxl-job context
141+
*/
128142
public static XxlJobContext getXxlJobContext(){
129143
return contextHolder.get();
130144
}

xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobHelper.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,31 @@ public static String getJobParam() {
5151
// ---------------------- for log ----------------------
5252

5353
/**
54-
* current JobLogFileName
54+
* current job log time
5555
*
56-
* @return logFileName
56+
* @return logDateTime
5757
*/
58-
public static String getJobLogFileName() {
58+
public static long getJobLogTime() {
5959
XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext();
6060
if (xxlJobContext == null) {
61-
return null;
61+
return -1;
6262
}
6363

64-
return xxlJobContext.getJobLogFileName();
64+
return xxlJobContext.getJobLogTime();
6565
}
6666

6767
/**
68-
* current LogDateTime
68+
* current job log filename
6969
*
70-
* @return logDateTime
70+
* @return logFileName
7171
*/
72-
public static long getLogDateTime() {
72+
public static String getJobLogFileName() {
7373
XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext();
7474
if (xxlJobContext == null) {
75-
return -1;
75+
return null;
7676
}
7777

78-
return xxlJobContext.getLogDateTime();
78+
return xxlJobContext.getJobLogFileName();
7979
}
8080

8181
// ---------------------- for shard ----------------------
@@ -164,13 +164,11 @@ private static boolean logDetail(StackTraceElement callInfo, String appendLog) {
164164
StackTraceElement[] stackTraceElements = new Throwable().getStackTrace();
165165
StackTraceElement callInfo = stackTraceElements[1];*/
166166

167-
StringBuffer stringBuffer = new StringBuffer();
168-
stringBuffer.append(DateTool.formatDateTime(new Date())).append(" ")
169-
.append("["+ callInfo.getClassName() + "#" + callInfo.getMethodName() +"]").append("-")
170-
.append("["+ callInfo.getLineNumber() +"]").append("-")
171-
.append("["+ Thread.currentThread().getName() +"]").append(" ")
172-
.append(appendLog!=null?appendLog:"");
173-
String formatAppendLog = stringBuffer.toString();
167+
String formatAppendLog = DateTool.formatDateTime(new Date()) + " " +
168+
"[" + callInfo.getClassName() + "#" + callInfo.getMethodName() + "]" + "-" +
169+
"[" + callInfo.getLineNumber() + "]" + "-" +
170+
"[" + Thread.currentThread().getName() + "]" + " " +
171+
(appendLog != null ? appendLog : "");
174172

175173
// appendlog
176174
String logFileName = xxlJobContext.getJobLogFileName();

xxl-job-core/src/main/java/com/xxl/job/core/openapi/model/TriggerRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@
88
public class TriggerRequest implements Serializable{
99
private static final long serialVersionUID = 42L;
1010

11+
// job base info
1112
private int jobId;
1213

14+
// job execute info
1315
private String executorHandler;
1416
private String executorParams;
1517
private String executorBlockStrategy;
1618
private int executorTimeout;
1719

20+
// log info
1821
private long logId;
1922
private long logDateTime;
2023

24+
// glue info
2125
private String glueType;
2226
private String glueSource;
2327
private long glueUpdatetime;
2428

29+
// broadcast info
2530
private int broadcastIndex;
2631
private int broadcastTotal;
2732

xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author xuxueli 2016-1-16 19:52:47
2424
*/
2525
public class JobThread extends Thread{
26-
private static Logger logger = LoggerFactory.getLogger(JobThread.class);
26+
private static final Logger logger = LoggerFactory.getLogger(JobThread.class);
2727

2828
private int jobId;
2929
private IJobHandler handler;
@@ -53,9 +53,6 @@ public IJobHandler getHandler() {
5353

5454
/**
5555
* new trigger to queue
56-
*
57-
* @param triggerParam
58-
* @return
5956
*/
6057
public Response<String> pushTriggerQueue(TriggerRequest triggerParam) {
6158
// avoid repeat
@@ -71,8 +68,6 @@ public Response<String> pushTriggerQueue(TriggerRequest triggerParam) {
7168

7269
/**
7370
* kill job thread
74-
*
75-
* @param stopReason
7671
*/
7772
public void toStop(String stopReason) {
7873
/**
@@ -86,7 +81,6 @@ public void toStop(String stopReason) {
8681

8782
/**
8883
* is running job
89-
* @return
9084
*/
9185
public boolean isRunningOrHasQueue() {
9286
return running || triggerQueue.size()>0;
@@ -121,8 +115,8 @@ public void run() {
121115
XxlJobContext xxlJobContext = new XxlJobContext(
122116
triggerParam.getJobId(),
123117
triggerParam.getExecutorParams(),
124-
logFileName,
125-
triggerParam.getLogDateTime(),
118+
triggerParam.getLogDateTime(),
119+
logFileName,
126120
triggerParam.getBroadcastIndex(),
127121
triggerParam.getBroadcastTotal());
128122

xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ private void callbackLog(List<HandleCallbackRequest> callbackParamList, String l
207207
XxlJobContext.setXxlJobContext(new XxlJobContext(
208208
-1,
209209
null,
210-
logFileName,
211210
-1,
211+
logFileName,
212212
-1,
213213
-1));
214214
XxlJobHelper.log(logContent);

0 commit comments

Comments
 (0)