From 3b7daf6759582c46afabf88b3b7f5f8e3dea01c2 Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Sat, 29 Nov 2025 00:49:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E9=87=8D=E6=9E=84=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E4=B8=8A=E4=B8=8B=E6=96=87=E6=97=A5=E5=BF=97=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=AD=97=E6=AE=B5=E5=8F=8A=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 XxlJobContext 中的日志时间戳和文件名字段重命名为 logDateTime 和 logFileName - 更新 XxlJobContext 构造函数参数名称以匹配新的字段命名 - 修改 XxlJobHelper 中获取日志信息的方法名称和实现逻辑 - 调整 ScriptJobHandler 和 JobThread 中对日志文件名的调用方式 - 优化 JobThread 中触发队列空值判断条件 - 在 TriggerCallbackThread 初始化 XxlJobContext 时增加缺失的 logId 参数传递 --- .../xxl/job/core/context/XxlJobContext.java | 39 ++++++++++++------- .../xxl/job/core/context/XxlJobHelper.java | 30 ++++++++++---- .../core/handler/impl/ScriptJobHandler.java | 2 +- .../com/xxl/job/core/thread/JobThread.java | 5 ++- .../core/thread/TriggerCallbackThread.java | 1 + 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobContext.java b/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobContext.java index 7d3d13c6..3f2dfc81 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobContext.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobContext.java @@ -27,14 +27,19 @@ public class XxlJobContext { // ---------------------- for log ---------------------- /** - * job log timestamp + * log id */ - private final long jobLogTime; - + private final long logId; + + /** + * log timestamp + */ + private final long logDateTime; + /** - * job log filename + * log filename */ - private final String jobLogFileName; + private final String logFileName; // ---------------------- for shard ---------------------- @@ -66,16 +71,18 @@ public class XxlJobContext { private String handleMsg; - public XxlJobContext(long jobId, + public XxlJobContext(long jobId, String jobParam, - long jobLogTime, - String jobLogFileName, + long logId, + long logDateTime, + String logFileName, int shardIndex, int shardTotal) { this.jobId = jobId; this.jobParam = jobParam; - this.jobLogTime = jobLogTime; - this.jobLogFileName = jobLogFileName; + this.logId = logId; + this.logDateTime = logDateTime; + this.logFileName = logFileName; this.shardIndex = shardIndex; this.shardTotal = shardTotal; @@ -90,12 +97,16 @@ public class XxlJobContext { return jobParam; } - public String getJobLogFileName() { - return jobLogFileName; + public long getLogId() { + return logId; + } + + public long getLogDateTime() { + return logDateTime; } - public long getJobLogTime() { - return jobLogTime; + public String getLogFileName() { + return logFileName; } public int getShardIndex() { diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobHelper.java b/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobHelper.java index 838b0fd6..ae9e62b6 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobHelper.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobHelper.java @@ -18,7 +18,7 @@ import java.util.Date; */ public class XxlJobHelper { - // ---------------------- base info ---------------------- + // ---------------------- job info ---------------------- /** * current JobId @@ -48,20 +48,34 @@ public class XxlJobHelper { return xxlJobContext.getJobParam(); } - // ---------------------- for log ---------------------- + // ---------------------- log info ---------------------- /** * current job log time * * @return logDateTime */ - public static long getJobLogTime() { + public static long getLogId() { XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext(); if (xxlJobContext == null) { return -1; } - return xxlJobContext.getJobLogTime(); + return xxlJobContext.getLogId(); + } + + /** + * current job log time + * + * @return logDateTime + */ + public static long getLogDateTime() { + XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext(); + if (xxlJobContext == null) { + return -1; + } + + return xxlJobContext.getLogDateTime(); } /** @@ -69,16 +83,16 @@ public class XxlJobHelper { * * @return logFileName */ - public static String getJobLogFileName() { + public static String getLogFileName() { XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext(); if (xxlJobContext == null) { return null; } - return xxlJobContext.getJobLogFileName(); + return xxlJobContext.getLogFileName(); } - // ---------------------- for shard ---------------------- + // ---------------------- shard info ---------------------- /** * current ShardIndex @@ -171,7 +185,7 @@ public class XxlJobHelper { (appendLog != null ? appendLog : ""); // appendlog - String logFileName = xxlJobContext.getJobLogFileName(); + String logFileName = xxlJobContext.getLogFileName(); if (logFileName!=null && !logFileName.trim().isEmpty()) { XxlJobFileAppender.appendLog(logFileName, formatAppendLog); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java b/xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java index 725dea94..54ea0fa3 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java @@ -69,7 +69,7 @@ public class ScriptJobHandler extends IJobHandler { } // log file - String logFileName = XxlJobContext.getXxlJobContext().getJobLogFileName(); + String logFileName = XxlJobContext.getXxlJobContext().getLogFileName(); // script params:0=param、1=分片序号、2=分片总数 String jobParam = XxlJobHelper.getJobParam(); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java b/xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java index 9000f40c..1082f01d 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java @@ -115,6 +115,7 @@ public class JobThread extends Thread{ XxlJobContext xxlJobContext = new XxlJobContext( triggerParam.getJobId(), triggerParam.getExecutorParams(), + triggerParam.getLogId(), triggerParam.getLogDateTime(), logFileName, triggerParam.getBroadcastIndex(), @@ -178,7 +179,7 @@ public class JobThread extends Thread{ } else { if (idleTimes > 30) { - if(triggerQueue.size() == 0) { // avoid concurrent trigger causes jobId-lost + if(triggerQueue.isEmpty()) { // avoid concurrent trigger causes jobId-lost XxlJobExecutor.removeJobThread(jobId, "excutor idle times over limit."); } } @@ -221,7 +222,7 @@ public class JobThread extends Thread{ } // callback trigger request in queue - while(triggerQueue !=null && triggerQueue.size()>0){ + while(triggerQueue !=null && !triggerQueue.isEmpty()){ TriggerRequest triggerParam = triggerQueue.poll(); if (triggerParam!=null) { // is killed diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java b/xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java index 4252dbed..2417a36d 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java @@ -208,6 +208,7 @@ public class TriggerCallbackThread { -1, null, -1, + -1, logFileName, -1, -1));