1、webcad拆单导入优化;2、配件、板材导入计数器超时优化;

This commit is contained in:
gaoqr
2025-04-02 14:00:47 +08:00
parent adcb915085
commit dd95b1d028
3 changed files with 20 additions and 65 deletions
@@ -38,6 +38,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -301,7 +302,7 @@ public class PartsServiceImpl implements PartsService {
try { try {
// 等待所有任务查询完成 // 等待所有任务查询完成
latch.await(); latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new ServiceException(PARTS_IMPORT_INTERRUPT_ERROR); throw new ServiceException(PARTS_IMPORT_INTERRUPT_ERROR);
@@ -33,6 +33,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -244,7 +245,7 @@ public class PlateManageServiceImpl implements PlateManageService {
try { try {
// 等待所有任务查询完成 // 等待所有任务查询完成
latch.await(); latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new ServiceException(PLATE_IMPORT_INTERRUPT_ERROR); throw new ServiceException(PLATE_IMPORT_INTERRUPT_ERROR);
@@ -78,8 +78,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -1500,7 +1499,7 @@ public class WebCadOrderImportFactory {
String[] specSplit = spec.split("-"); String[] specSplit = spec.split("-");
// 例:15mm-18mm // 例:15mm-18mm
if (specSplit.length == 2) { if (specSplit.length == 2) {
return specSplit[0] + "-" + width + "mm-"; return specSplit[0] + "-" + width + "mm";
} }
// 例:9mm-18mm-经典檀木 // 例:9mm-18mm-经典檀木
if (specSplit.length == 3) { if (specSplit.length == 3) {
@@ -1617,69 +1616,27 @@ public class WebCadOrderImportFactory {
private void saveDataAsync() { private void saveDataAsync() {
TransactionStatus transactionStatus = transactionManager.getTransaction(TransactionDefinition.withDefaults()); TransactionStatus transactionStatus = transactionManager.getTransaction(TransactionDefinition.withDefaults());
AtomicBoolean hasError = new AtomicBoolean(false); AtomicBoolean hasError = new AtomicBoolean(false);
ExecutorService executorService = Executors.newFixedThreadPool(3);
CountDownLatch latch = new CountDownLatch(3);
// 当前数据源data_code // 当前数据源data_code
String peek = DynamicDataSourceContextHolder.peek(); String peek = DynamicDataSourceContextHolder.peek();
List<List<OrderBodyDO>> bodyList = batchProcess(bodyMap.values().stream().toList());
List<List<OrderGroupDO>> groupList = batchProcess(orderGroupDOS);
List<List<OrderItemDO>> itemList = batchProcess(orderItemDOS);
// 计数器
CountDownLatch latch = new CountDownLatch(bodyList.size() + groupList.size() + itemList.size());
// 保存 // 保存
// saveBodyAsync(latch, hasError, peek); saveBodyAsync(bodyList,latch, hasError, peek);
// saveGroupAsync(latch, hasError, peek); saveGroupAsync(groupList, latch, hasError, peek);
// saveItemAsync(latch, hasError, peek); saveItemAsync(itemList, latch, hasError, peek);
Runnable bodyTask = () -> {
try {
// 子线程同步主线程的多数据源
DynamicDataSourceContextHolder.push(peek);
orderInfoBatchInsertWithinThreshold(orderBodyMapper, bodyMap.values().stream().toList(), true);
} catch (Exception e) {
hasError.set(true);
log.error("Error saving order bodies", e);
} finally {
latch.countDown();
}
};
Runnable itemTask = () -> {
try {
// 子线程同步主线程的多数据源
DynamicDataSourceContextHolder.push(peek);
orderInfoBatchInsertWithinThreshold(orderItemMapper, orderItemDOS, true);
} catch (Exception e) {
hasError.set(true);
log.error("Error saving order items", e);
} finally {
latch.countDown();
}
};
Runnable groupTask = () -> {
try {
// 子线程同步主线程的多数据源
DynamicDataSourceContextHolder.push(peek);
orderInfoBatchInsertWithinThreshold(orderGroupMapper, orderGroupDOS, true);
} catch (Exception e) {
hasError.set(true);
log.error("Error saving order groups", e);
} finally {
latch.countDown();
}
};
// 执行子线程
executorService.submit(bodyTask);
executorService.submit(itemTask);
executorService.submit(groupTask);
try { try {
// 等待所有任务完成 // 等待所有任务完成30s没有完成就主动终止
latch.await(); latch.await(30, TimeUnit.SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
hasError.set(true); hasError.set(true);
log.error("WEBCAD import saveData Thread interrupted", e); log.error("WEBCAD import saveData Thread interrupted", e);
} finally {
// 中止所有任务
executorService.shutdown();
} }
if (hasError.get()) { if (hasError.get()) {
@@ -1700,9 +1657,7 @@ public class WebCadOrderImportFactory {
* @param hasError * @param hasError
* @param peek * @param peek
*/ */
private void saveBodyAsync(CountDownLatch latch, AtomicBoolean hasError, String peek) { private void saveBodyAsync(List<List<OrderBodyDO>> bodyList, CountDownLatch latch, AtomicBoolean hasError, String peek) {
List<OrderBodyDO> orderBodyDOS = bodyMap.values().stream().toList();
List<List<OrderBodyDO>> bodyList = batchProcess(orderBodyDOS);
for (List<OrderBodyDO> bodyDOS : bodyList) { for (List<OrderBodyDO> bodyDOS : bodyList) {
threadPoolTaskExecutor.submit(() -> { threadPoolTaskExecutor.submit(() -> {
try { try {
@@ -1729,8 +1684,7 @@ public class WebCadOrderImportFactory {
* @param hasError * @param hasError
* @param peek * @param peek
*/ */
private void saveGroupAsync(CountDownLatch latch, AtomicBoolean hasError, String peek) { private void saveGroupAsync(List<List<OrderGroupDO>> groupList, CountDownLatch latch, AtomicBoolean hasError, String peek) {
List<List<OrderGroupDO>> groupList = batchProcess(orderGroupDOS);
for (List<OrderGroupDO> orderGroupDOS : groupList) { for (List<OrderGroupDO> orderGroupDOS : groupList) {
threadPoolTaskExecutor.submit(() -> { threadPoolTaskExecutor.submit(() -> {
try { try {
@@ -1757,8 +1711,7 @@ public class WebCadOrderImportFactory {
* @param hasError * @param hasError
* @param peek * @param peek
*/ */
private void saveItemAsync(CountDownLatch latch, AtomicBoolean hasError, String peek) { private void saveItemAsync(List<List<OrderItemDO>> itemList, CountDownLatch latch, AtomicBoolean hasError, String peek) {
List<List<OrderItemDO>> itemList = batchProcess(orderItemDOS);
for (List<OrderItemDO> itemDOS : itemList) { for (List<OrderItemDO> itemDOS : itemList) {
threadPoolTaskExecutor.submit(() -> { threadPoolTaskExecutor.submit(() -> {
try { try {