From e17797888cb227000c5a4e67e7e08364b6418cdb Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Fri, 12 Jun 2020 17:46:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BB=BB=E5=8A=A1=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=20"XxlJobContent"=20=EF=BC=8C=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E4=BB=BB=E5=8A=A1=E4=B8=8A=E4=B8=8B=E6=96=87?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=8C=85=E6=8B=AC=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?ID=E3=80=81=E5=88=86=E7=89=87=E5=8F=82=E6=95=B0=E7=AD=89?= =?UTF-8?q?=EF=BC=8C=E6=96=B9=E4=BE=BF=E8=BF=90=E8=A1=8C=E6=97=B6=E5=AD=98?= =?UTF-8?q?=E5=8F=96=E4=BB=BB=E5=8A=A1=E7=9B=B8=E5=85=B3=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=EF=BC=9B=20=20=20=20=20-=20=E5=BA=9F=E5=BC=83=20"ShardingUtil"?= =?UTF-8?q?=20=E7=BB=84=E4=BB=B6=EF=BC=9A=E6=94=B9=E7=94=A8=20"XxlJobConte?= =?UTF-8?q?xt.getXxlJobContext().getShardIndex()/getShardTotal();"=20?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=88=86=E7=89=87=E5=8F=82=E6=95=B0=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/XXL-JOB-English-Documentation.md | 3 +- doc/XXL-JOB官方文档.md | 6 +- .../xxl/job/core/context/XxlJobContext.java | 68 ++++++++++++++ .../core/handler/impl/ScriptJobHandler.java | 7 +- .../com/xxl/job/core/thread/JobThread.java | 9 +- .../com/xxl/job/core/util/ShardingUtil.java | 92 +++++++++---------- .../jobhandler/ShardingJobHandler.java | 12 ++- .../jboot/jobhandler/ShardingJobHandler.java | 12 ++- .../jfinal/jobhandler/ShardingJobHandler.java | 12 ++- .../nutz/jobhandler/ShardingJobHandler.java | 12 ++- .../service/jobhandler/SampleXxlJob.java | 12 ++- .../service/jobhandler/SampleXxlJob.java | 12 ++- 12 files changed, 172 insertions(+), 85 deletions(-) create mode 100644 xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobContext.java diff --git a/doc/XXL-JOB-English-Documentation.md b/doc/XXL-JOB-English-Documentation.md index ceeb239d..0f480397 100644 --- a/doc/XXL-JOB-English-Documentation.md +++ b/doc/XXL-JOB-English-Documentation.md @@ -974,7 +974,8 @@ When “分片广播” is selected as route policy in executor cluster, one tas The develop process of "分片广播" is the same as general task, The difference is that you can get slice parameters,code as shown below(go and see ShardingJobHandler in execuotr example ): - ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); + int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex(); + int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal(); This slice parameter object has two properties: diff --git a/doc/XXL-JOB官方文档.md b/doc/XXL-JOB官方文档.md index 461fcf0b..a66a4981 100644 --- a/doc/XXL-JOB官方文档.md +++ b/doc/XXL-JOB官方文档.md @@ -1207,7 +1207,8 @@ XXL-JOB会为每次调度请求生成一个单独的日志文件,需要通过 - Java语言任务获取分片参数方式:BEAN、GLUE模式(Java) ``` // 可参考Sample示例执行器中的示例任务"ShardingJobHandler"了解试用 -ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); +int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex(); +int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal(); ``` - 脚本语言任务获取分片参数方式:GLUE模式(Shell)、GLUE模式(Python)、GLUE模式(Nodejs) ``` @@ -1990,7 +1991,8 @@ data: post-data - 3、邮箱告警配置优化:将"spring.mail.from"与"spring.mail.username"属性拆分开,更加灵活的支持一些无密码邮箱服务; - 4、多个项目依赖升级至较新稳定版本,如netty、spring、springboot等; - 5、通用HTTP任务Handler(httpJobHandler)优化:修复 "setDoOutput(true)" 导致任务请求GetMethod失效问题; -- 6、[迭代中] 新增任务属性 "XxlJobContent" ,统一维护任务上下文信息,方便运行时存取任务相关信息; +- 6、新增任务属性 "XxlJobContent" ,统一维护任务上下文信息,包括任务ID、分片参数等,方便运行时存取任务相关信息; + - 废弃 "ShardingUtil" 组件:改用 "XxlJobContext.getXxlJobContext().getShardIndex()/getShardTotal();" 获取分片参数; - 7、[规划中]任务触发参数优化:支持选择 "Cron触发"、"固定间隔时间触发"、"指定时间点触发"、"不选择" 等; ### 7.32 版本 v2.3.0 Release Notes[规划中] 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 new file mode 100644 index 00000000..a0129069 --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/context/XxlJobContext.java @@ -0,0 +1,68 @@ +package com.xxl.job.core.context; + +/** + * xxl-job context + * + * @author xuxueli 2020-05-21 + * [Dear hj] + */ +public class XxlJobContext { + + /** + * job id + */ + private final long jobId; + + /** + * job log filename + */ + private final String jobLogFileName; + + /** + * shard index + */ + private final int shardIndex; + + /** + * shard total + */ + private final int shardTotal; + + + public XxlJobContext(long jobId, String jobLogFileName, int shardIndex, int shardTotal) { + this.jobId = jobId; + this.jobLogFileName = jobLogFileName; + this.shardIndex = shardIndex; + this.shardTotal = shardTotal; + } + + public long getJobId() { + return jobId; + } + + public String getJobLogFileName() { + return jobLogFileName; + } + + public int getShardIndex() { + return shardIndex; + } + + public int getShardTotal() { + return shardTotal; + } + + + // ---------------------- tool ---------------------- + + private static InheritableThreadLocal contextHolder = new InheritableThreadLocal(); + + public static void setXxlJobContext(XxlJobContext xxlJobContext){ + contextHolder.set(xxlJobContext); + } + + public static XxlJobContext getXxlJobContext(){ + return contextHolder.get(); + } + +} \ No newline at end of file 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 452c3877..f9357e81 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 @@ -1,12 +1,12 @@ package com.xxl.job.core.handler.impl; import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.context.XxlJobContext; import com.xxl.job.core.glue.GlueTypeEnum; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.log.XxlJobFileAppender; import com.xxl.job.core.log.XxlJobLogger; import com.xxl.job.core.util.ScriptUtil; -import com.xxl.job.core.util.ShardingUtil; import java.io.File; @@ -71,11 +71,10 @@ public class ScriptJobHandler extends IJobHandler { String logFileName = XxlJobFileAppender.contextHolder.get(); // script params:0=param、1=分片序号、2=分片总数 - ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); String[] scriptParams = new String[3]; scriptParams[0] = param; - scriptParams[1] = String.valueOf(shardingVO.getIndex()); - scriptParams[2] = String.valueOf(shardingVO.getTotal()); + scriptParams[1] = String.valueOf(XxlJobContext.getXxlJobContext().getShardIndex()); + scriptParams[2] = String.valueOf(XxlJobContext.getXxlJobContext().getShardTotal()); // invoke XxlJobLogger.log("----------- script file:"+ scriptFileName +" -----------"); 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 126fd143..aa4efb94 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 @@ -3,11 +3,11 @@ package com.xxl.job.core.thread; import com.xxl.job.core.biz.model.HandleCallbackParam; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.TriggerParam; +import com.xxl.job.core.context.XxlJobContext; import com.xxl.job.core.executor.XxlJobExecutor; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.log.XxlJobFileAppender; import com.xxl.job.core.log.XxlJobLogger; -import com.xxl.job.core.util.ShardingUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -117,8 +117,13 @@ public class JobThread extends Thread{ // log filename, like "logPath/yyyy-MM-dd/9999.log" String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTime()), triggerParam.getLogId()); + XxlJobContext.setXxlJobContext(new XxlJobContext( + triggerParam.getLogId(), + logFileName, + triggerParam.getBroadcastIndex(), + triggerParam.getBroadcastTotal())); + XxlJobFileAppender.contextHolder.set(logFileName); - ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal())); // execute XxlJobLogger.log("
----------- xxl-job job execute start -----------
----------- Param:" + triggerParam.getExecutorParams()); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/util/ShardingUtil.java b/xxl-job-core/src/main/java/com/xxl/job/core/util/ShardingUtil.java index cfd35b93..76716686 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/util/ShardingUtil.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/util/ShardingUtil.java @@ -1,46 +1,46 @@ -package com.xxl.job.core.util; - -/** - * sharding vo - * @author xuxueli 2017-07-25 21:26:38 - */ -public class ShardingUtil { - - private static InheritableThreadLocal contextHolder = new InheritableThreadLocal(); - - public static class ShardingVO { - - private int index; // sharding index - private int total; // sharding total - - public ShardingVO(int index, int total) { - this.index = index; - this.total = total; - } - - public int getIndex() { - return index; - } - - public void setIndex(int index) { - this.index = index; - } - - public int getTotal() { - return total; - } - - public void setTotal(int total) { - this.total = total; - } - } - - public static void setShardingVo(ShardingVO shardingVo){ - contextHolder.set(shardingVo); - } - - public static ShardingVO getShardingVo(){ - return contextHolder.get(); - } - -} +//package com.xxl.job.core.util; +// +///** +// * sharding vo +// * @author xuxueli 2017-07-25 21:26:38 +// */ +//public class ShardingUtil { +// +// private static InheritableThreadLocal contextHolder = new InheritableThreadLocal(); +// +// public static class ShardingVO { +// +// private int index; // sharding index +// private int total; // sharding total +// +// public ShardingVO(int index, int total) { +// this.index = index; +// this.total = total; +// } +// +// public int getIndex() { +// return index; +// } +// +// public void setIndex(int index) { +// this.index = index; +// } +// +// public int getTotal() { +// return total; +// } +// +// public void setTotal(int total) { +// this.total = total; +// } +// } +// +// public static void setShardingVo(ShardingVO shardingVo){ +// contextHolder.set(shardingVo); +// } +// +// public static ShardingVO getShardingVo(){ +// return contextHolder.get(); +// } +// +//} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xuxueli/executor/sample/frameless/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xuxueli/executor/sample/frameless/jobhandler/ShardingJobHandler.java index b45a24eb..10b8fc7c 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xuxueli/executor/sample/frameless/jobhandler/ShardingJobHandler.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xuxueli/executor/sample/frameless/jobhandler/ShardingJobHandler.java @@ -1,9 +1,9 @@ package com.xuxueli.executor.sample.frameless.jobhandler; import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.context.XxlJobContext; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.log.XxlJobLogger; -import com.xxl.job.core.util.ShardingUtil; /** * 分片广播任务 @@ -16,12 +16,14 @@ public class ShardingJobHandler extends IJobHandler { public ReturnT execute(String param) throws Exception { // 分片参数 - ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); - XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal()); + int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex(); + int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal(); + + XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); // 业务逻辑 - for (int i = 0; i < shardingVO.getTotal(); i++) { - if (i == shardingVO.getIndex()) { + for (int i = 0; i < shardTotal; i++) { + if (i == shardIndex) { XxlJobLogger.log("第 {} 片, 命中分片开始处理", i); } else { XxlJobLogger.log("第 {} 片, 忽略", i); diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/ShardingJobHandler.java index 8eb70aa7..4914e68a 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/ShardingJobHandler.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/ShardingJobHandler.java @@ -1,9 +1,9 @@ package com.xuxueli.executor.sample.jboot.jobhandler; import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.context.XxlJobContext; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.log.XxlJobLogger; -import com.xxl.job.core.util.ShardingUtil; /** * 分片广播任务 @@ -16,12 +16,14 @@ public class ShardingJobHandler extends IJobHandler { public ReturnT execute(String param) throws Exception { // 分片参数 - ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); - XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal()); + int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex(); + int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal(); + + XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); // 业务逻辑 - for (int i = 0; i < shardingVO.getTotal(); i++) { - if (i == shardingVO.getIndex()) { + for (int i = 0; i < shardTotal; i++) { + if (i == shardIndex) { XxlJobLogger.log("第 {} 片, 命中分片开始处理", i); } else { XxlJobLogger.log("第 {} 片, 忽略", i); diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java index bb3fa2c5..ebc95ede 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java @@ -1,9 +1,9 @@ package com.xuxueli.executor.sample.jfinal.jobhandler; import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.context.XxlJobContext; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.log.XxlJobLogger; -import com.xxl.job.core.util.ShardingUtil; /** * 分片广播任务 @@ -16,12 +16,14 @@ public class ShardingJobHandler extends IJobHandler { public ReturnT execute(String param) throws Exception { // 分片参数 - ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); - XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal()); + int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex(); + int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal(); + + XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); // 业务逻辑 - for (int i = 0; i < shardingVO.getTotal(); i++) { - if (i == shardingVO.getIndex()) { + for (int i = 0; i < shardTotal; i++) { + if (i == shardIndex) { XxlJobLogger.log("第 {} 片, 命中分片开始处理", i); } else { XxlJobLogger.log("第 {} 片, 忽略", i); diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/ShardingJobHandler.java index d01ab918..5a03b211 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/ShardingJobHandler.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/ShardingJobHandler.java @@ -1,9 +1,9 @@ package com.xuxueli.executor.sample.nutz.jobhandler; import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.context.XxlJobContext; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.log.XxlJobLogger; -import com.xxl.job.core.util.ShardingUtil; /** * 分片广播任务 @@ -16,12 +16,14 @@ public class ShardingJobHandler extends IJobHandler { public ReturnT execute(String param) throws Exception { // 分片参数 - ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); - XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal()); + int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex(); + int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal(); + + XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); // 业务逻辑 - for (int i = 0; i < shardingVO.getTotal(); i++) { - if (i == shardingVO.getIndex()) { + for (int i = 0; i < shardTotal; i++) { + if (i == shardIndex) { XxlJobLogger.log("第 {} 片, 命中分片开始处理", i); } else { XxlJobLogger.log("第 {} 片, 忽略", i); diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java index 666d2d3e..fac84edf 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java @@ -1,10 +1,10 @@ package com.xxl.job.executor.service.jobhandler; import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.context.XxlJobContext; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; -import com.xxl.job.core.util.ShardingUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -55,12 +55,14 @@ public class SampleXxlJob { public ReturnT shardingJobHandler(String param) throws Exception { // 分片参数 - ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); - XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal()); + int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex(); + int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal(); + + XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); // 业务逻辑 - for (int i = 0; i < shardingVO.getTotal(); i++) { - if (i == shardingVO.getIndex()) { + for (int i = 0; i < shardTotal; i++) { + if (i == shardIndex) { XxlJobLogger.log("第 {} 片, 命中分片开始处理", i); } else { XxlJobLogger.log("第 {} 片, 忽略", i); diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java index 666d2d3e..fac84edf 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java @@ -1,10 +1,10 @@ package com.xxl.job.executor.service.jobhandler; import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.context.XxlJobContext; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; -import com.xxl.job.core.util.ShardingUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -55,12 +55,14 @@ public class SampleXxlJob { public ReturnT shardingJobHandler(String param) throws Exception { // 分片参数 - ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); - XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal()); + int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex(); + int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal(); + + XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); // 业务逻辑 - for (int i = 0; i < shardingVO.getTotal(); i++) { - if (i == shardingVO.getIndex()) { + for (int i = 0; i < shardTotal; i++) { + if (i == shardIndex) { XxlJobLogger.log("第 {} 片, 命中分片开始处理", i); } else { XxlJobLogger.log("第 {} 片, 忽略", i);