Browse Source

调整了入库的事务

lifuquan 2 years ago
parent
commit
1ee4339941

+ 34 - 0
doc/开发文档/任务特点分析.md

@@ -1,3 +1,37 @@
 # 任务特点分析
 
 <https://oapi.dingtalk.com/robot/send?access_token=b2f1424d6119affaacab614b184f043fcd2c73db2651bb86eff29992d66820bf>
+
+## 自动报表 定时任务特点分析
+
+1. 每天运行1次
+2. 以月为边界,每天执行的任务要求前一天的数据必须完整,中间不能间隔
+3. 数据源不是特别稳定
+
+因为第2、3点特点,需要:
+
+1. 运行时先检查前一天的数据入库是否完整,如果完整
+2. 运行时需要检查当天数据是否完备,如果不完备进入循环等待,每个一个时间间隔进行一次检查,每次检查如果缺少数据都需要发送一次提醒。
+
+先搞定第2步,第1个问题暂时未遇到
+
+```java
+/**
+     * 模拟检查等待
+     * 
+     * @throws Exception
+     */
+    @Test
+    void test() throws Exception {
+        String filePath = "D:/src/投诉清单各地市投诉率20230503.xlsx";
+        File file = new File(filePath);
+        while (!file.exists()) {
+            // 发送提醒
+            System.out.println("检查发现:文件" + filePath + "不存在");
+            // 等待5秒后再次检查
+            Thread.sleep(1000 * 5);
+        }
+        // 完成任务
+        System.out.println("done!");
+    }
+```

BIN
doc/开发文档/样本数据/输入/数据表头说明.xlsx


+ 1 - 1
tsl_data/src/main/java/com/nokia/tsl_data/controller/TslTaskConroller.java

@@ -44,7 +44,7 @@ public class TslTaskConroller {
     @PostMapping("/warahouse")
     public R warahouseManually(@RequestBody String day) {
         try {
-            taskService.dataWaraHouseTask(day);
+            tslWaraHouseService.dataWaraHouseTask(day);
             return R.ok().message("入库成功");
         } catch (FileNotFoundException e) {
             e.printStackTrace();

+ 3 - 1
tsl_data/src/main/java/com/nokia/tsl_data/service/TslCronTaskService.java

@@ -35,7 +35,9 @@ public class TslCronTaskService {
      */
     public void startCronTask() {
         if (!isStarted) {
-            future = taskScheduler.schedule(tslTaskService.cronTask(), new CronTrigger(cronExpression));
+            future = taskScheduler.schedule(() -> {
+                tslTaskService.cronTask();
+            }, new CronTrigger(cronExpression));
             isStarted = true;
         }
     }

+ 19 - 42
tsl_data/src/main/java/com/nokia/tsl_data/service/TslTaskService.java

@@ -2,7 +2,6 @@ package com.nokia.tsl_data.service;
 
 import java.awt.image.BufferedImage;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -17,8 +16,6 @@ import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.ss.usermodel.WorkbookFactory;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-
 import com.nokia.common.excel.entity.CellRect;
 import com.nokia.common.excel.poi.PoiUtil;
 import com.nokia.common.message.pushmessage.PushMessageUtil;
@@ -56,32 +53,25 @@ public class TslTaskService {
      * 
      * @return
      */
-    public Runnable cronTask() {
-        return new Runnable() {
-
-            @Override
-            public void run() {
-                // 获取入参 day
-                String day = new SimpleDateFormat("yyyyMMdd")
-                        .format(new Date(System.currentTimeMillis() - 1000L * 3600 * 24));
-
-                try {
-                    // 检查数据源
-                    checkSrcExsit(day);
-                    // 数据入库
-                    dataWaraHouseTask(day);
-                    // 生成xlsx
-                    reportGenerateTask(day);
-                    // 截图
-                    screenShotTask(day);
-                } catch (Exception e) {
-                    log.error("定时任务出错--{}", e.getMessage());
-                    e.printStackTrace();
-                    pushMessageUtil.sendTextMessage("定时任务出错--" + e.getMessage());
-                }
-            }
-
-        };
+    public void cronTask() {
+        // 获取入参 day
+        String day = new SimpleDateFormat("yyyyMMdd")
+                .format(new Date(System.currentTimeMillis() - 1000L * 3600 * 24));
+
+        try {
+            // 检查数据源
+            checkSrcExsit(day);
+            // 数据入库
+            tslWaraHouseService.dataWaraHouseTask(day);
+            // 生成xlsx
+            reportGenerateTask(day);
+            // 截图
+            screenShotTask(day);
+        } catch (Exception e) {
+            log.error("定时任务出错--{}", e.getMessage());
+            e.printStackTrace();
+            pushMessageUtil.sendTextMessage("定时任务出错--" + e.getMessage());
+        }
     }
 
     /**
@@ -177,17 +167,4 @@ public class TslTaskService {
             throw new RuntimeException(e.getMessage());
         }
     }
-
-    /**
-     * 数据入库任务
-     * 
-     * @param day
-     * @throws IOException
-     * @throws FileNotFoundException
-     */
-    @Transactional(rollbackFor = Exception.class)
-    public void dataWaraHouseTask(String day) throws FileNotFoundException, IOException {
-        tslWaraHouseService.waraHouseHighQuality(day);
-        tslWaraHouseService.waraHouseMobileComp(day);
-    }
 }

+ 17 - 0
tsl_data/src/main/java/com/nokia/tsl_data/service/TslWaraHouseService.java

@@ -13,6 +13,7 @@ import java.util.List;
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVParser;
 import org.apache.commons.csv.CSVRecord;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
@@ -49,6 +50,9 @@ public class TslWaraHouseService {
     @Value("${tslTask.source.HIGH_QUALITY_FIELD_NUM}")
     private int HIGH_QUALITY_FIELD_NUM;
 
+    @Autowired
+    private TslWaraHouseService tslWaraHouseService;
+
     public TslWaraHouseService(JdbcTemplate jdbcTemplate) {
         this.jdbcTemplate = jdbcTemplate;
     }
@@ -134,4 +138,17 @@ public class TslWaraHouseService {
             log.debug("河北_CEM高品质2日统计 {} 入库成功", day);
         }
     }
+
+    /**
+     * 数据入库任务
+     * 
+     * @param day
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void dataWaraHouseTask(String day) throws FileNotFoundException, IOException {
+        tslWaraHouseService.waraHouseHighQuality(day);
+        tslWaraHouseService.waraHouseMobileComp(day);
+    }
 }