From 213e0c9f2917e3362424cd17785a94e9a3377b87 Mon Sep 17 00:00:00 2001 From: dengwc Date: Thu, 11 Apr 2024 15:06:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E7=AE=97=E6=98=8E=E7=BB=86=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=98=A0=E5=B0=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order_record/LyStatementOrder.java | 44 ++-- .../LYOrderRecordDomainService.java | 227 ++++++++++-------- .../config/webconfig/WebConfig.java | 4 +- 3 files changed, 150 insertions(+), 125 deletions(-) diff --git a/src/main/java/com/chint/domain/factoriy/order_record/LyStatementOrder.java b/src/main/java/com/chint/domain/factoriy/order_record/LyStatementOrder.java index 9a6acf64..84bdd309 100644 --- a/src/main/java/com/chint/domain/factoriy/order_record/LyStatementOrder.java +++ b/src/main/java/com/chint/domain/factoriy/order_record/LyStatementOrder.java @@ -29,9 +29,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; @Slf4j @Service @@ -95,11 +93,32 @@ public class LyStatementOrder { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String startTime = startDate.atStartOfDay().format(formatter); String endTime = LocalDateTime.now().format(formatter); + //创建一个线程池并行执行 + ThreadPoolExecutor executor = executeTask(onlineStatementList, startTime, endTime); + try { + // 关闭线程池 + executor.shutdown(); + // 等待所有任务执行完成或者超时时间到达(这里设置为一天) + boolean flag = executor.awaitTermination(1, TimeUnit.HOURS); + } catch (InterruptedException e) { + // 处理中断异常 + log.error(e.getMessage()); + // 强制关闭线程池 + executor.shutdownNow(); + } + log.info("保存成功!"); + } + + private ThreadPoolExecutor executeTask(ArrayList onlineStatementList, String startTime, String endTime) { + ThreadPoolExecutor executor = new ThreadPoolExecutor( + 4, // 核心线程数 + 4, // 最大线程数 + 0L, // 空闲线程存活时间 + TimeUnit.MILLISECONDS, // 存活时间单位 + new LinkedBlockingQueue<>(100) // 任务队列,设置一个有限大小的队列 + ); for (ResStatementList.OnlineStatementList onlineStatement : onlineStatementList) { String statementCode = onlineStatement.getStatementCode();//对账单编码 - // 提交任务给线程池处理 - // 定义线程池,可以根据实际情况调整线程数量 - ExecutorService executor = Executors.newFixedThreadPool(4); // 创建一个包含4个线程的线程池 //1.查询机票账单详情 executor.submit(() -> flightStatement(statementCode, startTime, endTime)); //2.查询酒店账单详情 @@ -108,19 +127,8 @@ public class LyStatementOrder { executor.submit(() -> trainStatement(statementCode, startTime, endTime)); //4.查询用车账单详情 executor.submit(() -> carStatement(statementCode, startTime, endTime)); - // 关闭线程池 - executor.shutdown(); - try { - // 等待所有任务执行完成或者超时时间到达(这里设置为一天) - boolean flag = executor.awaitTermination(1, TimeUnit.HOURS); - } catch (InterruptedException e) { - // 处理中断异常 - log.error(e.getMessage()); - // 强制关闭线程池 - executor.shutdownNow(); - } } - log.info("保存成功!"); + return executor; } /** diff --git a/src/main/java/com/chint/domain/service/order_record/LYOrderRecordDomainService.java b/src/main/java/com/chint/domain/service/order_record/LYOrderRecordDomainService.java index 34e595f1..1c9b88fe 100644 --- a/src/main/java/com/chint/domain/service/order_record/LYOrderRecordDomainService.java +++ b/src/main/java/com/chint/domain/service/order_record/LYOrderRecordDomainService.java @@ -2,6 +2,7 @@ package com.chint.domain.service.order_record; import com.chint.application.commands.LYOrderRecordGenerateCommand; import com.chint.application.commands.OrderRecordGenerateCommand; +import com.chint.domain.aggregates.order.OrderDetail; import com.chint.domain.aggregates.order.order_record.OrderCarRecord; import com.chint.domain.aggregates.order.order_record.OrderFlightRecord; import com.chint.domain.aggregates.order.order_record.OrderHotelRecord; @@ -19,14 +20,19 @@ import com.chint.infrastructure.util.PageResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Component; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.support.DefaultTransactionDefinition; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; @Component public class LYOrderRecordDomainService { @@ -50,124 +56,137 @@ public class LYOrderRecordDomainService { @Autowired private OrderDetailDomainService orderDetailDomainService; + @Autowired + private DataSourceTransactionManager transactionManager; + - @Transactional @ListenTo(command = "LYOrderRecordGenerateCommand", order = 0) - public List generateFlightRecord(LYOrderRecordGenerateCommand command) { - ArrayList orderFlightRecordList = new ArrayList<>(); - int count = 0;//初始化变量 - int pageSize = 100; // 假设每页大小为10 - int LoopNum;//需要的循环数 - do { - PageRequest pageResult = PageRequest.of(count, pageSize); - Page lyOrderFlightRecordPage = jdbcLyOrderFlightRecord.findAllByStatusIsNull(pageResult); - long totalElements = lyOrderFlightRecordPage.getTotalElements();//总数 - LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整 - //数据 - List lyOrderFlightRecordPageList = lyOrderFlightRecordPage.toList(); - ArrayList orderFlightRecords = new ArrayList<>(100); - for (LyOrderFlightRecord lyOrderFlightRecord : lyOrderFlightRecordPageList) { - OrderFlightRecord orderFlightRecord = lyOrderRecordExtensionFactory.createFlightOrderRecord(lyOrderFlightRecord);//转换 - orderFlightRecords.add(orderFlightRecord); - } - //保存数据 - orderDetailDomainService.saveFlightOrderRecordBatch(orderFlightRecords); - orderFlightRecordList.addAll(orderFlightRecords); - count++; - } while (count < LoopNum); - //修改数据,将状态为null的设置为1 - jdbcLyOrderFlightRecord.updateStatusByNullStatus(); - return orderFlightRecordList; + public void generateFlightRecord(LYOrderRecordGenerateCommand command) { + useThread(() -> { + int count = 0;//初始化变量 + int pageSize = 100; // 假设每页大小为10 + int LoopNum;//需要的循环数 + do { + PageRequest pageResult = PageRequest.of(count, pageSize); + Page lyOrderFlightRecordPage = jdbcLyOrderFlightRecord.findAllByStatusIsNull(pageResult); + long totalElements = lyOrderFlightRecordPage.getTotalElements();//总数 + LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整 + //数据 + List lyOrderFlightRecordPageList = lyOrderFlightRecordPage.toList(); + ArrayList orderFlightRecords = new ArrayList<>(100); + for (LyOrderFlightRecord lyOrderFlightRecord : lyOrderFlightRecordPageList) { + OrderFlightRecord orderFlightRecord = lyOrderRecordExtensionFactory.createFlightOrderRecord(lyOrderFlightRecord);//转换 + orderFlightRecords.add(orderFlightRecord); + } + //保存数据 + orderDetailDomainService.saveFlightOrderRecordBatch(orderFlightRecords); + count++; + } while (count < LoopNum); + //修改数据,将状态为null的设置为1 + jdbcLyOrderFlightRecord.updateStatusByNullStatus(); + }); } - @Transactional @ListenTo(command = "LYOrderRecordGenerateCommand", order = 1) - public List generateHotelRecord(LYOrderRecordGenerateCommand command) { - ArrayList orderHotelRecordList = new ArrayList<>(); - int count = 0;//初始化变量 - int pageSize = 100; // 假设每页大小为10 - int LoopNum;//需要的循环数 - do { - PageRequest pageResult = PageRequest.of(count, pageSize); - Page lyOrderHotelRecordPage = jdbcLyOrderHotelRecord.findAllByStatusIsNull(pageResult); - long totalElements = lyOrderHotelRecordPage.getTotalElements();//总数 - LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整 - //数据 - List lyOrderHotelRecordPageList = lyOrderHotelRecordPage.toList(); - ArrayList orderHotelRecords = new ArrayList<>(100); - for (LyOrderHotelRecord lyOrderHotelRecord : lyOrderHotelRecordPageList) { - OrderHotelRecord orderHotelRecord = lyOrderRecordExtensionFactory.createHotelOrderRecord(lyOrderHotelRecord);//转换 - orderHotelRecords.add(orderHotelRecord); - } - //保存数据 - orderDetailDomainService.saveHotelOrderRecordBatch(orderHotelRecords); - orderHotelRecordList.addAll(orderHotelRecords); - count++; - } while (count < LoopNum); - //修改数据,将状态为null的设置为1 - jdbcLyOrderHotelRecord.updateStatusByNullStatus(); - return orderHotelRecordList; + public void generateHotelRecord(LYOrderRecordGenerateCommand command) { + useThread(() -> { + int count = 0;//初始化变量 + int pageSize = 100; // 假设每页大小为10 + int LoopNum;//需要的循环数 + do { + PageRequest pageResult = PageRequest.of(count, pageSize); + Page lyOrderHotelRecordPage = jdbcLyOrderHotelRecord.findAllByStatusIsNull(pageResult); + long totalElements = lyOrderHotelRecordPage.getTotalElements();//总数 + LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整 + //数据 + List lyOrderHotelRecordPageList = lyOrderHotelRecordPage.toList(); + ArrayList orderHotelRecords = new ArrayList<>(100); + for (LyOrderHotelRecord lyOrderHotelRecord : lyOrderHotelRecordPageList) { + OrderHotelRecord orderHotelRecord = lyOrderRecordExtensionFactory.createHotelOrderRecord(lyOrderHotelRecord);//转换 + orderHotelRecords.add(orderHotelRecord); + } + //保存数据 + orderDetailDomainService.saveHotelOrderRecordBatch(orderHotelRecords); + count++; + } while (count < LoopNum); + //修改数据,将状态为null的设置为1 + jdbcLyOrderHotelRecord.updateStatusByNullStatus(); + }); } - @Transactional @ListenTo(command = "LYOrderRecordGenerateCommand", order = 2) - public List generateTrainRecord(LYOrderRecordGenerateCommand command) { - ArrayList orderTrainRecordsList = new ArrayList<>(); - int count = 0;//初始化变量 - int pageSize = 100; // 假设每页大小为10 - int LoopNum;//需要的循环数 - do { - PageRequest pageResult = PageRequest.of(count, pageSize); - Page lyOrderTrainRecordPage = jdbcLyOrderTrainRecord.findAllByStatusIsNull(pageResult); - long totalElements = lyOrderTrainRecordPage.getTotalElements();//总数 - LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整 - //数据 - List lyOrderTrainRecordPageList = lyOrderTrainRecordPage.toList(); - ArrayList orderTrainRecords = new ArrayList<>(100); - for (LyOrderTrainRecord lyOrderTrainRecord : lyOrderTrainRecordPageList) { - OrderTrainRecord orderTrainRecord = lyOrderRecordExtensionFactory.createTrainOrderRecord(lyOrderTrainRecord);//转换 - orderTrainRecords.add(orderTrainRecord); - } - //保存数据 - orderDetailDomainService.saveTrainOrderRecordBatch(orderTrainRecords); - orderTrainRecordsList.addAll(orderTrainRecords); - count++; - } while (count < LoopNum); - //修改数据,将状态为null的设置为1 - jdbcLyOrderTrainRecord.updateStatusByNullStatus(); - return orderTrainRecordsList; + public void generateTrainRecord(LYOrderRecordGenerateCommand command) { + useThread(() -> { + int count = 0;//初始化变量 + int pageSize = 100; // 假设每页大小为10 + int LoopNum;//需要的循环数 + do { + PageRequest pageResult = PageRequest.of(count, pageSize); + Page lyOrderTrainRecordPage = jdbcLyOrderTrainRecord.findAllByStatusIsNull(pageResult); + long totalElements = lyOrderTrainRecordPage.getTotalElements();//总数 + LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整 + //数据 + List lyOrderTrainRecordPageList = lyOrderTrainRecordPage.toList(); + ArrayList orderTrainRecords = new ArrayList<>(100); + for (LyOrderTrainRecord lyOrderTrainRecord : lyOrderTrainRecordPageList) { + OrderTrainRecord orderTrainRecord = lyOrderRecordExtensionFactory.createTrainOrderRecord(lyOrderTrainRecord);//转换 + orderTrainRecords.add(orderTrainRecord); + } + //保存数据 + orderDetailDomainService.saveTrainOrderRecordBatch(orderTrainRecords); + count++; + } while (count < LoopNum); + //修改数据,将状态为null的设置为1 + jdbcLyOrderTrainRecord.updateStatusByNullStatus(); + }); } - - @Transactional @ListenTo(command = "LYOrderRecordGenerateCommand", order = 3) - public List generateCarRecord(LYOrderRecordGenerateCommand command) { - ArrayList orderCarRecordsList = new ArrayList<>(); - int count = 0;//初始化变量 - int pageSize = 100; // 假设每页大小为10 - int LoopNum;//需要的循环数 - do { - PageRequest pageResult = PageRequest.of(count, pageSize); - Page lyOrderCarRecordPage = jdbcLyOrderCarRecord.findAllByStatusIsNull(pageResult); - long totalElements = lyOrderCarRecordPage.getTotalElements();//总数 - LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整 - //数据 - List lyOrderCarRecordPageList = lyOrderCarRecordPage.toList(); - ArrayList orderCarRecords = new ArrayList<>(100); - for (LyOrderCarRecord lyOrderCarRecord : lyOrderCarRecordPageList) { - OrderCarRecord orderCarRecord = lyOrderRecordExtensionFactory.createCarOrderRecord(lyOrderCarRecord);//转换 - orderCarRecords.add(orderCarRecord); + public void generateCarRecord(LYOrderRecordGenerateCommand command) { + useThread(() -> { + int count = 0;//初始化变量 + int pageSize = 100; // 假设每页大小为10 + int LoopNum;//需要的循环数 + do { + PageRequest pageResult = PageRequest.of(count, pageSize); + Page lyOrderCarRecordPage = jdbcLyOrderCarRecord.findAllByStatusIsNull(pageResult); + long totalElements = lyOrderCarRecordPage.getTotalElements();//总数 + LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整 + //数据 + List lyOrderCarRecordPageList = lyOrderCarRecordPage.toList(); + ArrayList orderCarRecords = new ArrayList<>(100); + for (LyOrderCarRecord lyOrderCarRecord : lyOrderCarRecordPageList) { + OrderCarRecord orderCarRecord = lyOrderRecordExtensionFactory.createCarOrderRecord(lyOrderCarRecord);//转换 + orderCarRecords.add(orderCarRecord); + } + //保存数据 + orderDetailDomainService.saveCarOrderRecordBatch(orderCarRecords); + count++; + } while (count < LoopNum); + //修改数据,将状态为null的设置为1 + jdbcLyOrderCarRecord.updateStatusByNullStatus(); + }); + } + + + //使用线程并行执行 + public void useThread(Runnable runnable) { + new Thread(() -> { + // 开启事务 + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); + def.setPropagationBehaviorName("PROPAGATION_REQUIRED"); + TransactionStatus status = transactionManager.getTransaction(def); + try { + runnable.run();//执行具体方法 + // 提交事务 + transactionManager.commit(status); + } catch (Exception e) { + // 发生异常时回滚事务 + transactionManager.rollback(status); } - //保存数据 - orderDetailDomainService.saveCarOrderRecordBatch(orderCarRecords); - orderCarRecordsList.addAll(orderCarRecords); - count++; - } while (count < LoopNum); - //修改数据,将状态为null的设置为1 - jdbcLyOrderCarRecord.updateStatusByNullStatus(); - return orderCarRecordsList; + }).start(); } } diff --git a/src/main/java/com/chint/infrastructure/config/webconfig/WebConfig.java b/src/main/java/com/chint/infrastructure/config/webconfig/WebConfig.java index 45ac2f73..7cbded34 100644 --- a/src/main/java/com/chint/infrastructure/config/webconfig/WebConfig.java +++ b/src/main/java/com/chint/infrastructure/config/webconfig/WebConfig.java @@ -34,9 +34,7 @@ public class WebConfig implements WebMvcConfigurer { //调用bean registry.addInterceptor(getMyRequestLoggingInterceptor()) .addPathPatterns("/**") - .excludePathPatterns("/public/**/pageQuery", "/order/pageQuery", "/OrderDetail/query/**", - "/order/query/**", "/location/**", "/OrderDetail/record/query"); - + .excludePathPatterns("/location/**", "/**/query/**", "/**/pageQuery/**"); } @Override