李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.chinaztt.mes.quality.excel;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.chinaztt.mes.quality.service.TestStandardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
 
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
 
/**
 * @Auther: shihongzhi
 * @Date: 2021/12/22/17:52
 * @Description:
 */
@Slf4j
public class TestStandardUploadListener extends AnalysisEventListener<TestStandardData> {
 
    /**
     * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 10000;
 
    /**
     * 缓存的数据
     */
    private List<TestStandardData> cachedDataList = new LinkedList<>();
 
    private TestStandardService testStandardService;
 
    public TestStandardUploadListener(TestStandardService testStandardService) {
        this.testStandardService = testStandardService;
    }
 
    @Override
    public void invoke(TestStandardData testStandardData, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", JSON.toJSONString(testStandardData));
        cachedDataList.add(testStandardData);
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList.clear();
        }
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 这里也要保存数据,确保最后遗留的数据也存储到数据库
        saveData();
        log.info("所有数据解析完成!");
    }
 
    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据,开始存储数据库!", cachedDataList.size());
        testStandardService.importExcel(cachedDataList);
        log.info("存储数据库成功!");
    }
}