java的异步多线程调用百万级接口数据+本地缓存的使用、手动分页

1.分页方法

public TableDataInfo getCarrierExceptionDetailVoList(Date startTime, Date endTime, String deptCode, String carrierCode, Integer pageSize, Integer pageNum) {

        StringBuilder redisKey = new StringBuilder("datacockpit:carrierExceptionDataDetail")
                .append(":").append(startTime)
                .append(":").append(endTime)
                .append(":").append(deptCode)
                .append(":").append(carrierCode);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(null);
        tableDataInfo.setTotal(0);

        List<CarrierExceptionDetailVo> carrierExceptionDetailVoList = new ArrayList<>();

        if (carrierKanBanExceptionDetailCache.getIfPresent(redisKey.toString()) != null) {
            carrierExceptionDetailVoList = (List<CarrierExceptionDetailVo>) carrierKanBanExceptionDetailCache.getIfPresent(redisKey.toString());
            if (CollectionUtils.isEmpty(carrierExceptionDetailVoList)) {
                return tableDataInfo;
            }
            // 页码
            int count = carrierExceptionDetailVoList.size(); // 总记录数
            // 计算总页数
            int pages = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            // 起始位置
            int start = 0;
            if (pageNum > 0) {
                start = pageNum > pages ? (pages - 1) * pageSize : (pageNum - 1) * pageSize;
            }
            tableDataInfo.setTotal(carrierExceptionDetailVoList.size());
            tableDataInfo.setRows(carrierExceptionDetailVoList.stream().skip(start).limit(pageSize).collect(Collectors.toList()));
            return tableDataInfo;
        }

        List<String> deptCodeList = warHouseCodeUtilsService.getDeptCodeOneList(deptCode);
        if (CollectionUtils.isEmpty(deptCodeList)) {
            throw new CustomException("查询到的机构编码为空,请联系管理员!");
        }

        //异步查询指标
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        HttpServletRequest request = ServletUtils.getRequest();

        // data_cockpit_scheduling 536514

        List<CarrierExceptionDetailVo> finalCarrierExceptionDetailVoList = carrierExceptionDetailVoList;
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
            SpecialCarLineAppNotUsed specialCarLineAppNotUsed = new SpecialCarLineAppNotUsed();
            specialCarLineAppNotUsed.setStartTime(startTime);
            specialCarLineAppNotUsed.setEndTime(endTime);
            specialCarLineAppNotUsed.setDeptCodeList(deptCodeList);
            specialCarLineAppNotUsed.setCarrierCode(carrierCode);
            querySpecialCarLineAppNotUsedDetails(specialCarLineAppNotUsed, finalCarrierExceptionDetailVoList);
        });
        futures.add(future1);


        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
            SpecialCarLineAppUnqualifiedUseRate specialCarLineAppUnqualifiedUseRate = new SpecialCarLineAppUnqualifiedUseRate();
            specialCarLineAppUnqualifiedUseRate.setStartTime(startTime);
            specialCarLineAppUnqualifiedUseRate.setEndTime(endTime);
            specialCarLineAppUnqualifiedUseRate.setDeptCodeList(deptCodeList);
            specialCarLineAppUnqualifiedUseRate.setCarrierCode(carrierCode);
            querySpecialCarLineAppUnqualifiedUseRateDetails(specialCarLineAppUnqualifiedUseRate, finalCarrierExceptionDetailVoList);
        });
        futures.add(future2);
        // 大表

        CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
            SpecialCarLineOrderNotSignedRate orderNotSignedRate = new SpecialCarLineOrderNotSignedRate();
            orderNotSignedRate.setStartTime(startTime);
            orderNotSignedRate.setEndTime(endTime);
            orderNotSignedRate.setDeptCodeList(deptCodeList);
            orderNotSignedRate.setCarrierCode(carrierCode);
            NoSpecialCarLineNotSignedRate noSpecialCarLineNotSignedRate = new NoSpecialCarLineNotSignedRate();
            noSpecialCarLineNotSignedRate.setStartTime(startTime);
            noSpecialCarLineNotSignedRate.setEndTime(endTime);
            noSpecialCarLineNotSignedRate.setDeptCodeList(deptCodeList);
            noSpecialCarLineNotSignedRate.setCarrierCode(carrierCode);
            queryOrderNotSignedRateDetails(finalCarrierExceptionDetailVoList, orderNotSignedRate, noSpecialCarLineNotSignedRate);
        });
        futures.add(future3);

        // 大表数据

        CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
            TrackStoreCoverageNoRateDetails trackStoreCoverageNoRateDetails = new TrackStoreCoverageNoRateDetails();
            trackStoreCoverageNoRateDetails.setStartTime(startTime);
            trackStoreCoverageNoRateDetails.setEndTime(endTime);
            trackStoreCoverageNoRateDetails.setDeptCodeList(Collections.singletonList(deptCodeList));
            trackStoreCoverageNoRateDetails.setCarrierCode(carrierCode);
            selectTrackStoreNoCoverageDetailsList(finalCarrierExceptionDetailVoList, trackStoreCoverageNoRateDetails);
        });
        futures.add(future4);
        // 

        CompletableFuture<Void> future5 = CompletableFuture.runAsync(() -> {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
            PrecoolingTemperatureNoCoverageDetails precoolingTemperatureNoCoverageDetails = new PrecoolingTemperatureNoCoverageDetails();
            precoolingTemperatureNoCoverageDetails.setStartTime(startTime);
            precoolingTemperatureNoCoverageDetails.setEndTime(endTime);
            precoolingTemperatureNoCoverageDetails.setDeptList(deptCodeList);
            precoolingTemperatureNoCoverageDetails.setCarrierCode(carrierCode);
            selectPreCoolingTempNoCoverageDetailsList(finalCarrierExceptionDetailVoList, precoolingTemperatureNoCoverageDetails);
        });
        futures.add(future5);
        // 专车专线

        CompletableFuture<Void> future6 = CompletableFuture.runAsync(() -> {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
            TempNoStandardDetails tempNoStandardDetails = new TempNoStandardDetails();
            tempNoStandardDetails.setStartTime(startTime);
            tempNoStandardDetails.setEndTime(endTime);
            tempNoStandardDetails.setDeptList(deptCodeList);
            tempNoStandardDetails.setCarrierCode(carrierCode);
            selectTempNoStandardDetailsList(finalCarrierExceptionDetailVoList, tempNoStandardDetails);
        });
        futures.add(future6);

        //等待所有异步查询任务完成
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

        if (!CollectionUtils.isEmpty(finalCarrierExceptionDetailVoList)) {
            carrierExceptionDetailVoList = new ArrayList<>(finalCarrierExceptionDetailVoList.stream()
                    .collect(Collectors.toMap(CarrierExceptionDetailVo::getSchedulingBillCode, vo -> vo, (v1, v2) -> v1))
                    .values());
            // 页码
            int count = carrierExceptionDetailVoList.size(); // 总记录数
            // 计算总页数
            int pages = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            // 起始位置
            int start = 0;
            if (pageNum > 0) {
                start = pageNum > pages ? (pages - 1) * pageSize : (pageNum - 1) * pageSize;
            }
            tableDataInfo.setTotal(carrierExceptionDetailVoList.size());
            tableDataInfo.setRows(carrierExceptionDetailVoList.stream().skip(start).limit(pageSize).collect(Collectors.toList()));
        }
        if (carrierKanBanExceptionDetailCache.getIfPresent(redisKey.toString()) == null) {
            carrierKanBanExceptionDetailCache.put(redisKey.toString(), carrierExceptionDetailVoList);
        }
        return tableDataInfo;
    }

2.设置缓存

Cache<String, Object> carrierKanBanExceptionDetailCache = CacheBuilder.newBuilder()
            .expireAfterAccess(2, TimeUnit.HOURS)       // 过期时间
            .maximumSize(256)                                  // 设置最大容量
            .concurrencyLevel(4)
            .initialCapacity(128)
            .build();

3.同时调用6个方法,方法里面嵌套多线程异步查询mysql

/**
     * 专车专线
     *
     * @param specialCarLineAppUnqualifiedUseRate 参数
     * @param carrierExceptionDetailVoList        参数
     */
    public void querySpecialCarLineAppUnqualifiedUseRateDetails(SpecialCarLineAppUnqualifiedUseRate specialCarLineAppUnqualifiedUseRate,
                                                                List<CarrierExceptionDetailVo> carrierExceptionDetailVoList) {
        List<SpecialCarLineAppUnqualifiedUseRate> specialCarLineAppUnqualifiedUseRates = schedulingAppRateMapper.querySpecialCarLineAppUnqualifiedUseRateDetails(specialCarLineAppUnqualifiedUseRate);
        if (!CollectionUtils.isEmpty(specialCarLineAppUnqualifiedUseRates)) {
            specialCarLineAppUnqualifiedUseRates.forEach(bean -> {
                CarrierExceptionDetailVo carrierExceptionDetailVo = new CarrierExceptionDetailVo();
                carrierExceptionDetailVo.setSchedulingBillCode(bean.getSchedulingBillCode());
                carrierExceptionDetailVo.setMainDriverName(bean.getMainDriverName());
                carrierExceptionDetailVo.setViceDriverName(bean.getViceDriverName());
                carrierExceptionDetailVo.setLicensePlateNumber(bean.getLicensePlateNumber());
                carrierExceptionDetailVoList.add(carrierExceptionDetailVo);
            });
        }
    }

    public void querySpecialCarLineAppNotUsedDetails(SpecialCarLineAppNotUsed specialCarLineAppNotUsed,
                                                     List<CarrierExceptionDetailVo> carrierExceptionDetailVoList) {
        List<SpecialCarLineAppNotUsed> specialCarLineAppNotUses = schedulingAppRateMapper.querySpecialCarLineAppNotUsedDetails(specialCarLineAppNotUsed);
        if (!CollectionUtils.isEmpty(specialCarLineAppNotUses)) {
            specialCarLineAppNotUses.forEach(bean -> {
                CarrierExceptionDetailVo carrierExceptionDetailVo = CarrierExceptionDetailVo.builder()
                        .schedulingBillCode(bean.getSchedulingBillCode())
                        .mainDriverName(bean.getMainDriverName())
                        .viceDriverName(bean.getViceDriverName())
                        .licensePlateNumber(bean.getLicensePlateNumber())
                        .build();
                carrierExceptionDetailVoList.add(carrierExceptionDetailVo);
            });
        }
    }

    public void queryOrderNotSignedRateDetails(List<CarrierExceptionDetailVo> carrierExceptionDetailVoList,
                                               SpecialCarLineOrderNotSignedRate orderNotSignedRate,
                                               NoSpecialCarLineNotSignedRate noSpecialCarLineNotSignedRate) {

        //异步查询指标
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        HttpServletRequest request = ServletUtils.getRequest();
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
            List<SpecialCarLineOrderNotSignedRate> orderNotSignedRates = tmsWorkSignedMapper.querySpecialCarLineOrderNotSignedRateDetails2(orderNotSignedRate);
            if (!CollectionUtils.isEmpty(orderNotSignedRates)) {
                orderNotSignedRates.forEach(bean -> {
                    CarrierExceptionDetailVo carrierExceptionDetailVo = CarrierExceptionDetailVo.builder()
                            .schedulingBillCode(bean.getSchedulingBillCode())
                            .mainDriverName(bean.getDeliveryUserName())
                            .build();
                    carrierExceptionDetailVoList.add(carrierExceptionDetailVo);
                });
            }
        });
        futures.add(future1);

        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
            List<NoSpecialCarLineNotSignedRate> noSpecialCarLineNotSignedRates = tmsWorkSignedMapper.queryNoSpecialCarLineNotSignedRateDetails2(noSpecialCarLineNotSignedRate);
            if (!CollectionUtils.isEmpty(noSpecialCarLineNotSignedRates)) {
                noSpecialCarLineNotSignedRates.forEach(bean -> {
                    CarrierExceptionDetailVo carrierExceptionDetailVo = CarrierExceptionDetailVo.builder()
                            .schedulingBillCode(bean.getSchedulingBillCode())
                            .mainDriverName(bean.getDeliveryUserName())
                            .build();
                    carrierExceptionDetailVoList.add(carrierExceptionDetailVo);
                });
            }
        });
        futures.add(future2);
        //等待所有异步查询任务完成
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
    }

    public void selectTrackStoreNoCoverageDetailsList(List<CarrierExceptionDetailVo> carrierExceptionDetailVoList,
                                                      TrackStoreCoverageNoRateDetails trackStoreCoverageNoRateDetails) {
        List<TrackStoreCoverageNoRateDetails> trackStoreCoverageNoRateDetailsList = dispatchSheetMonitoringMapper.selectTrackStoreNoCoverageDetailsList2(trackStoreCoverageNoRateDetails);
        if (!CollectionUtils.isEmpty(trackStoreCoverageNoRateDetailsList)) {
            trackStoreCoverageNoRateDetailsList.forEach(bean -> {
                CarrierExceptionDetailVo carrierExceptionDetailVo = CarrierExceptionDetailVo.builder()
                        .schedulingBillCode(bean.getSchedulingBillCode())
                        .mainDriverName(bean.getMainDriverName())
                        .licensePlateNumber(bean.getLicensePlateNumber())
                        .build();
                carrierExceptionDetailVoList.add(carrierExceptionDetailVo);
            });
        }
    }

    public void selectPreCoolingTempNoCoverageDetailsList(List<CarrierExceptionDetailVo> carrierExceptionDetailVoList,
                                                          PrecoolingTemperatureNoCoverageDetails precoolingTemperatureNoCoverageDetails) {
        List<PrecoolingTemperatureNoCoverageDetails> precoolingTemperatureNoCoverageDetailsList = dispatchSheetMonitoringMapper.selectPreCoolingTempNoCoverageDetailsList2(precoolingTemperatureNoCoverageDetails);
        if (!CollectionUtils.isEmpty(precoolingTemperatureNoCoverageDetailsList)) {
            precoolingTemperatureNoCoverageDetailsList.forEach(bean -> {
                CarrierExceptionDetailVo carrierExceptionDetailVo = CarrierExceptionDetailVo.builder()
                        .mainDriverName(bean.getMainDriverName())
                        .schedulingBillCode(bean.getSchedulingBillCode())
                        .licensePlateNumber(bean.getLicensePlateNumber())
                        .build();
                carrierExceptionDetailVoList.add(carrierExceptionDetailVo);
            });
        }
    }

    public void selectTempNoStandardDetailsList(List<CarrierExceptionDetailVo> carrierExceptionDetailVoList,
                                                TempNoStandardDetails tempNoStandardDetails) {
        List<TempNoStandardDetails> tempNoStandardDetailsList = dispatchSheetMonitoringMapper.selectTempNoStandardDetailsList2(tempNoStandardDetails);
        if (!CollectionUtils.isEmpty(tempNoStandardDetailsList)) {
            tempNoStandardDetailsList.forEach(bean -> {
                CarrierExceptionDetailVo carrierExceptionDetailVo = CarrierExceptionDetailVo.builder()
                        .mainDriverName(bean.getMainDriverName())
                        .schedulingBillCode(bean.getSchedulingBillCode())
                        .licensePlateNumber(bean.getLicensePlateNumber())
                        .build();
                carrierExceptionDetailVoList.add(carrierExceptionDetailVo);
            });
        }

    }

4.缓存依赖

<dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>

你可能感兴趣的:(java,缓存,redis)