parent
f5f98bc042
commit
0c03f6348c
@ -0,0 +1,46 @@
|
||||
package com.xxl.job.core.util;
|
||||
|
||||
/**
|
||||
* sharding vo
|
||||
* @author xuxueli 2017-07-25 21:26:38
|
||||
*/
|
||||
public class ShardingUtil {
|
||||
|
||||
private static InheritableThreadLocal<ShardingVO> contextHolder = new InheritableThreadLocal<ShardingVO>();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.xxl.job.executor.service.jobhandler;
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.handler.annotation.JobHander;
|
||||
import com.xxl.job.core.log.XxlJobLogger;
|
||||
import com.xxl.job.core.util.ShardingUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* 广播分片任务
|
||||
*
|
||||
* @author xuxueli 2017-07-25 20:56:50
|
||||
*/
|
||||
@JobHander(value="shardingJobHandler")
|
||||
@Service
|
||||
public class ShardingJobHandler extends IJobHandler {
|
||||
|
||||
@Override
|
||||
public ReturnT<String> execute(String... params) throws Exception {
|
||||
|
||||
// 分片参数
|
||||
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
|
||||
XxlJobLogger.log("分片参数:当前分片序号 = {0}, 总分片数 = {1}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
|
||||
// 业务逻辑
|
||||
for (int i = 0; i < shardingVO.getTotal(); i++) {
|
||||
if (i == shardingVO.getIndex()) {
|
||||
XxlJobLogger.log("第 {0} 片, 命中分片开始处理", i);
|
||||
} else {
|
||||
XxlJobLogger.log("第 {0} 片, 忽略", i);
|
||||
}
|
||||
}
|
||||
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.xxl.job.executor.service.jobhandler;
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.handler.annotation.JobHander;
|
||||
import com.xxl.job.core.log.XxlJobLogger;
|
||||
import com.xxl.job.core.util.ShardingUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* 广播分片任务
|
||||
*
|
||||
* @author xuxueli 2017-07-25 20:56:50
|
||||
*/
|
||||
@JobHander(value="shardingJobHandler")
|
||||
@Service
|
||||
public class ShardingJobHandler extends IJobHandler {
|
||||
|
||||
@Override
|
||||
public ReturnT<String> execute(String... params) throws Exception {
|
||||
|
||||
// 分片参数
|
||||
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
|
||||
XxlJobLogger.log("分片参数:当前分片序号 = {0}, 总分片数 = {1}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
|
||||
// 业务逻辑
|
||||
for (int i = 0; i < shardingVO.getTotal(); i++) {
|
||||
if (i == shardingVO.getIndex()) {
|
||||
XxlJobLogger.log("第 {0} 片, 命中分片开始处理", i);
|
||||
} else {
|
||||
XxlJobLogger.log("第 {0} 片, 忽略", i);
|
||||
}
|
||||
}
|
||||
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue