From 83dfb5640c1e11634dd6c4fae0d078494f0c0190 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 05 六月 2023 17:08:48 +0800
Subject: [PATCH] 开发Voc文件异步入库定时程序

---
 src/main/java/com/yssh/service/VocValsService.java |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/yssh/service/VocValsService.java b/src/main/java/com/yssh/service/VocValsService.java
index a977c63..685bbc1 100644
--- a/src/main/java/com/yssh/service/VocValsService.java
+++ b/src/main/java/com/yssh/service/VocValsService.java
@@ -1,16 +1,23 @@
 package com.yssh.service;
 
+import com.google.common.collect.Lists;
 import com.yssh.dao.VocValsMapper;
 import com.yssh.entity.VocVals;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
 
 @Service
 public class VocValsService implements VocValsMapper {
     @Resource
     VocValsMapper vocValsMapper;
+
+    protected final Log logger = LogFactory.getLog(this.getClass());
 
     @Override
     public List<VocVals> selectByTime(String time, Double startVal) {
@@ -36,4 +43,29 @@
     public int deleteLastYear() {
         return vocValsMapper.deleteLastYear();
     }
+
+    @Async("threadPoolTaskExecutor")
+    public int insertVocVals(List<VocVals> list) throws InterruptedException {
+        List<List<VocVals>> lists = Lists.partition(list, IAsyncService.BATCH_INSERT_500);
+
+        CountDownLatch countDownLatch = new CountDownLatch(list.size());
+        for (List<VocVals> corpList : lists) {
+            executeAsync(corpList, countDownLatch);
+        }
+
+        countDownLatch.await();
+        logger.info("------ VOC.csv锛�" + lists.size() + " 鏉℃暟鎹凡鍏ュ簱 ------");
+
+        return list.size();
+    }
+
+    private void executeAsync(List<VocVals> corpList, CountDownLatch countDownLatch) {
+        try {
+            // 寮傛绾跨▼瑕佸仛鐨勪簨鎯�
+            vocValsMapper.inserts(corpList);
+        } finally {
+            // 寰堝叧閿�, 鏃犺涓婇潰绋嬪簭鏄惁寮傚父蹇呴』鎵цcountDown,鍚﹀垯await鏃犳硶閲婃斁
+            countDownLatch.countDown();
+        }
+    }
 }

--
Gitblit v1.9.3