表结构修改时,使用线程池批量更新数据。ThreadPoolExecutor CountDownLatch batchUpdate

1. 查数据、分段更新数据代码。

    public String initSaleContract() {
        long millis = System.currentTimeMillis();
        Query<McProjectSale> query = new Query<>();
        query.setStart(1);
        query.setLimit(1);
        Result<McProjectSale> mcCompanyResult = corporationService.getMcProjectSaleResult(query);
        int pageSize = 500;
        long page1 = mcCompanyResult.getTotal() / pageSize;
        if (mcCompanyResult.getTotal() % pageSize > 0) {
            page1 += 1;
        }
        CountDownLatch downLatch = new CountDownLatch(Math.toIntExact(page1));
        AtomicInteger atomicInteger = new AtomicInteger();
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(availableProcessors / 2, availableProcessors, 10, TimeUnit.SECONDS
                , new ArrayBlockingQueue<>(pageSize), r -> {
            Thread thread = new Thread(r);
            thread.setName("pool-initSaleContract-" + atomicInteger.incrementAndGet());
            return thread;
        }, new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < page1; i++) {
            int finalI = i;
            executor.submit(() -> {
                try {
                    Query<McProjectSale> query1 = new Query<>();
                    query1.setStart(finalI * pageSize);
                    query1.setLimit(pageSize);
                    List<McCompanyContact> companies = corporationService.getMcProjectSaleResult(query1).getDataList().stream()
                            .filter((Predicate<McProjectSale>) input -> Objects.nonNull(input.getContactId()) && Objects.nonNull(input.getOwnerSalesUid()))
                            .map(mcProjectSale -> {
                                McCompanyContact companyContact = new McCompanyContact();
                                companyContact.setId(mcProjectSale.getContactId());
                                Integer ownerSalesUid = mcProjectSale.getOwnerSalesUid();
                                companyContact.setOwnerSaleUid(ownerSalesUid);
                                OrgVO currentOrgVO = orgService.getOrgData(ownerSalesUid, null);
                                log.info("{}-{}", ownerSalesUid, currentOrgVO);
                                companyContact.setSaleGroupDepartmentId(currentOrgVO.getGroupDepartmentId());
                                companyContact.setSaleSubsidiaryId(currentOrgVO.getSubsidiaryId());
                                companyContact.setSaleFirstDepartmentId(currentOrgVO.getFirstDepartmentId());
                                companyContact.setSaleSecondDepartmentId(currentOrgVO.getSecondDepartmentId());
                                companyContact.setSaleThirdDepartmentId(currentOrgVO.getThirdDepartmentId());
                                return companyContact;
                            }).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(companies)) {
                        corporationService.batchUpdateMcCompanyContactOwnerSaleUid(companies);
                    }
                } catch (Exception e) {
                    log.error("McCompanyContact - {} 页 错误,{}", finalI, Throwables.getStackTraceAsString(e));
                } finally {
                    downLatch.countDown();
                }
            });
        }
        try {
            downLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        executor.shutdown();
        return callbackSuccess(System.currentTimeMillis() - millis);
    }
2. 批量更新Mapper代码。
<update id="batchUpdateMcCompanyContactOwnerSaleUid">
    UPDATE mc_company_contact
    SET owner_sale_uid = CASE
    <foreach collection="list" item="item" index="index" separator=" ">
        WHEN `id` = #{item.id} THEN #{item.ownerSaleUid}
    </foreach>
    END,
    sale_group_department_id = CASE
    <foreach collection="list" item="item" index="index" separator=" ">
        WHEN `id` = #{item.id} THEN #{item.saleGroupDepartmentId}
    </foreach>
    END,
    sale_subsidiary_id = CASE
    <foreach collection="list" item="item" index="index" separator=" ">
        WHEN `id` = #{item.id} THEN #{item.saleSubsidiaryId}
    </foreach>
    END,
    sale_first_department_id = CASE
    <foreach collection="list" item="item" index="index" separator=" ">
        WHEN `id` = #{item.id} THEN #{item.saleFirstDepartmentId}
    </foreach>
    END,
    sale_second_department_id = CASE
    <foreach collection="list" item="item" index="index" separator=" ">
        WHEN `id` = #{item.id} THEN #{item.saleSecondDepartmentId}
    </foreach>
    END,
    sale_third_department_id = CASE
    <foreach collection="list" item="item" index="index" separator=" ">
        WHEN `id` = #{item.id} THEN #{item.saleThirdDepartmentId}
    </foreach>
    END
    WHERE id IN
    <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
        #{item.id}
    </foreach>
</update>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值