xxl-job如何实现分片处理

时间:2025-03-17 15:31:35
/** * 分片广播任务 */ @XxlJob("shardingJobHandler") public void shardingJobHandler() throws Exception { // 分片参数 int shardIndex = XxlJobHelper.getShardIndex(); int shardTotal = XxlJobHelper.getShardTotal(); XxlJobHelper.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); //获取需要处理的所有门店列表 List<StoreInfo> storeInfoList = storeInfoMapper.selectList(new QueryWrapper<StoreInfo>().lambda().orderByDesc(StoreInfo::getStoreCode)); //当前分片需要处理的门店列表 List<StoreInfo> shardStoreInfoList = new ArrayList<>(); // 业务逻辑 for (int i = 0; i < storeInfoList.size(); i++) { if (i % shardTotal == shardIndex) { //将当前分片需要处理的门店加入list shardStoreInfoList.add(storeInfoList.get(i)); } } //处理业务逻辑... service.doBusiness(shardStoreInfoList); }