一、异常出现的场景

  一次线上订单历史数据字段刷新操作,3张表100多万数据。由于同步更新太慢大概20分钟以上,所以采用异不的方式。代码如下:

private void batchUpdate(List<SaasOrderRecordDataForUpdate> saasOrderRecordDataForUpdateList, List<SaasServiceOrderInfoDataForUpdate> saasServiceOrderInfoDataForUpdateList, List<OrderGoodsDataForUpdate> orderGoodsDataForUpdateList, List<OrderAdditionCostInfoDataForUpdate> orderAdditionCostInfoDataForUpdateList) {
        List<Future> asyncResultList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(saasOrderRecordDataForUpdateList)) {
            int size = saasOrderRecordDataForUpdateList.size();
            int count = size / 5000;
            for (int i = 0; i <= count; i++) {
                List<SaasOrderRecordDataForUpdate> subList = null;
                if (i == count) {
                    if (5000 * i < size) {
                        subList = saasOrderRecordDataForUpdateList.subList(5000 * i, size);
                    }
                } else {
                    subList = saasOrderRecordDataForUpdateList.subList(5000 * i, 5000 * (i + 1));
                }

                if (CollectionUtils.isNotEmpty(subList)) {
                    Future future = dataRefreshJobServiceForAsync.batchUpdateSaasOrderRecordDataForUpdate(subList);
                    asyncResultList.add(future);
                }
            }
        }
        XxlJobLogger.log("批量更新订单数:{}", saasOrderRecordDataForUpdateList.size());
        if (CollectionUtils.isNotEmpty(saasServiceOrderInfoDataForUpdateList)) {
            int size = saasServiceOrderInfoDataForUpdateList.size();
            int count = size / 5000;
            for (int i = 0; i <= count; i++) {
                List<SaasServiceOrderInfoDataForUpdate> subList = null;
                if (i == count) {
                    if (5000 * i < size) {
                        subList = saasServiceOrderInfoDataForUpdateList.subList(5000 * i, size);
                    }
                } else {
                    subList = saasServiceOrderInfoDataForUpdateList.subList(5000 * i, 5000 * (i + 1));
                }

                if (CollectionUtils.isNotEmpty(subList)) {
                    Future future = dataRefreshJobServiceForAsync.batchUpdateSaasServiceOrderInfoDataForUpdate(subList);
                    asyncResultList.add(future);
                }
            }

        }
        XxlJobLogger.log("批量更新订单服务数:{}", saasServiceOrderInfoDataForUpdateList.size());
        if (CollectionUtils.isNotEmpty(orderGoodsDataForUpdateList)) {
            int size = orderGoodsDataForUpdateList.size();
            int count = size / 5000;
            for (int i = 0; i <= count; i++) {
                List<OrderGoodsDataForUpdate> subList = null;
                if (i == count) {
                    if (5000 * i < size) {
                        subList = orderGoodsDataForUpdateList.subList(5000 * i, size);
                    }
                } else {
                    subList = orderGoodsDataForUpdateList.subList(5000 * i, 5000 * (i + 1));
                }

                if (CollectionUtils.isNotEmpty(subList)) {
                    Future future = dataRefreshJobServiceForAsync.batchUpdateOrderGoodsDataForUpdate(subList);
                    asyncResultList.add(future);
                }
            }

        }
        XxlJobLogger.log("批量更新订单商品数:{}", orderGoodsDataForUpdateList.size());
        if (CollectionUtils.isNotEmpty(orderAdditionCostInfoDataForUpdateList)) {
            int size = orderAdditionCostInfoDataForUpdateList.size();
            int count = size / 5000;
            for (int i = 0; i <= count; i++) {
                List<OrderAdditionCostInfoDataForUpdate> subList = null;
                if (i == count) {
                    if (5000 * i < size) {
                        subList = orderAdditionCostInfoDataForUpdateList.subList(5000 * i, size);
                    }
                } else {
                    subList = orderAdditionCostInfoDataForUpdateList.subList(5000 * i, 5000 * (i + 1));
                }

                if (CollectionUtils.isNotEmpty(subList)) {
                    Future future = dataRefreshJobServiceForAsync.batchUpdateOrderAdditionCostInfoDataForUpdate(subList);
                    asyncResultList.add(future);
                }
            }
        }
        XxlJobLogger.log("批量更新订单附加费数:{}", orderAdditionCostInfoDataForUpdateList.size());

        if (CollectionUtils.isNotEmpty(asyncResultList)) {
            for (Future asyncResult : asyncResultList) {
                try {
                    asyncResult.get();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
View Code

相关文章: