多线程开启组装数据,对所有非事务操作前置

2023-06-24 15:58:06 浏览数 (2)

场景:主流程在历史业务下,越来越繁琐,臃肿,不得不加一堆校验和组装数据,主流程下对多个主表数据操作,记录完本地数据后,调用rpc。

高峰调用

TP95

改造思路:

多线程开启组装数据,对所有非事务操作前置,对大事务拆分,减少重复查询数据,异步更新数据。

before

代码语言:javascript复制
        checkCharterPrice(cacheDOList,warehouseCode,Code,OrderDO);
        response.setCode(Code);
        OrderDO.setCode(Code);
        OrderDO.setWarehouseCode(warehouseCode);
        OrderDO.setWarehouseName(warehouseName);
        OrderDO.setSignCode(request.getSignCode());
        OrderDO.setSuperClass(OrderSuperClassEnum.CLASS_1.value());
        List<OrderDetailCacheDO> detailCacheDOS = cacheDOList.stream().filter(DistinctByKeyUtil.distinctByKey(vo -> vo.getShippedOrderCode())).collect(Collectors.toList());
        OrderDO.setShippedOrderNum(detailCacheDOS.size());*/
        OrderDO.setOrderType(cacheDOList.get(0).getOrderType());
        OrderDO.setDriverName(driverSignDO.getDriverName());
        OrderDO.setDriverPhone(driverSignDO.getDriverPhone());
        OrderDO.setLicensePlate(driverSignDO.getLicensePlate());
        OrderDO.setVehicleCategory(driverSignDO.getVehicleCategory());
        OrderDO.setVehicleTypeCode(driverSignDO.getVehicleTypeCode());
        OrderDO.setVehicleTypeName(driverSignDO.getVehicleTypeName());
        OrderDO.setStatus(OrderStatusEnum.STATUS_ENTRUCKED.value());
        OrderDO.setSignStatus(SignStatusEnum.SIGN_NO.value());
        OrderDO.setStatus(StatusEnum.UNCOMPLETE.value());
        OrderDO.setSendDate(driverSignDO.getSendDate());
        OrderDO.setCreateBy(request.getUpdateBy());
        OrderDO.setCreateTime(new Date());
        OrderDO.setSupplierCode(transportVehicleDto.getSupplierCode());
        OrderDO.setSupplierName(transportVehicleDto.getSupplierName());
        OrderDO.setEntruckSource(request.getEntruckSource());

after

代码语言:javascript复制
public class BindOrderThreadPool {
    public final static ExecutorService BindOrderThreadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2   1);
}

Future<OrderDO> orderDOFuture = BindOrderThreadPool.BindOrderThreadPool.submit(() -> this.buildOrderBaseInfo(cacheDOList, warehouseCode, finalEntruckingOrderDO));
Future<SignShipped> SignShippedFuture = BindOrderThreadPool.BindOrderThreadPool.submit(() -> this.containerOrderCompleteBind(request, list, productDetailCacheDOList, entruckingOrderDO, entruckingCode, cacheIds, cacheDetailIds, eoddList, signEoddList, warehouseName, transportType));
OrderDO OrderDOByFuture = orderDOFuture.get();
SignShippedDo SignShippedByFuture = SignShippedFuture.get();
executorService.execute(()->{
            try {
                   service.query            
                } catch (Exception e) {
                    log.info("任务{}异常",requestParam,e);
                }

});
return response.builder().BaseInfo(OrderDOByFuture).SignShippedInfo(SignShippedByFuture ).build;

异步操作改造

before

代码语言:javascript复制
 TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                    @Override
                    public void afterCommit() {             
                        if (YesOrNoEnum.YES.value().equals(ConfigDO.getIsSyncCall())) {                    
                            mqOrderProducer.isSyncCallOrder(OrderDetailDo);
                        } else {                       
                            handleSync(entruckingOrderDetailDo);
                        }
                    }
                });

after

代码语言:javascript复制
          final CountDownLatch downLatch = new CountDownLatch(1);

                try {
                    handleSync(entruckingOrderDetailDo);
                } catch (Exception e) {
                    log.info("同步handleSyncy异常",e);
                } finally {
                    downLatch.countDown();
                }

原子标识判断是否异常

before

代码语言:javascript复制
boolean flag = false;
Repository.insertBatch(xxx).var
Repository.insertBatch(xxx).var
Repository.insertBatch(xxx).var
if(flag){
   handlerSyncData();
}

after

代码语言:javascript复制
 AtomicBoolean  entruckFlag = new AtomicBoolean(false);
            if(!entruckFlag.get()){
                try {
                Repository.insertBatch(xxx).var
                Repository.insertBatch(xxx).var
                Repository.insertBatch(xxx).var
                    log.warn("持久化明细表:");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(!entruckFlag.get()){
                handlerSyncData();
            }

对于重复调用数据查询前置

before

代码语言:javascript复制
for (Response.LogItem logItem : logItemList) { OrderDOentruckingOrder = OrderRepository.getOrder(logItem.getCode(), logItem.getEntruckingCode());    for (PackageConvertResponse packageConvertResponse : packageConvertResponseList) {          OrderDOentruckingOrder = OrderRepository.getOrder(logItem.getCode(), logItem.getEntruckingCode());          EntruckingOrderDO entruckingOrderDO1 = entruckingOrderMapper.selectOne(entruckingOrderDO);       }}

after

代码语言:javascript复制
for (Response.LogItem logItem : logItemList) {

 OrderDO  Order = OrderRepository.getOrder(logItem.getCode(), logItem.getEntruckingCode());
    for (PackageConvertResponse packageConvertResponse : packageConvertResponseList) {
      Order.getxxx
    }
}

完结

0 人点赞