燕山石化溯源三维电子沙盘-【后端】-服务
13693261870
2023-06-05 83dfb5640c1e11634dd6c4fae0d078494f0c0190
开发Voc文件异步入库定时程序
已添加1个文件
已修改4个文件
151 ■■■■ 文件已修改
src/main/java/com/yssh/entity/VocVals.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/scheduled/ReadCsvTask.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/service/IAsyncService.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/service/VocValsService.java 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/utils/VocParser.java 65 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/yssh/entity/VocVals.java
@@ -1,68 +1,72 @@
package com.yssh.entity;
import cn.hutool.core.date.DateTime;
import com.github.biyanwen.annotation.CsvProperty;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.Date;
public class VocVals implements Serializable {
    private static final long serialVersionUID = -20230605145412000L;
    @ApiModelProperty(value = "主键")
    private int id;
    private Integer id;
    @CsvProperty(index = 0)
    @ApiModelProperty(value = "X")
    private int x;
    private Integer x;
    @CsvProperty(index = 1)
    @ApiModelProperty(value = "Y")
    private int y;
    private Integer y;
    @CsvProperty(index = 2)
    @ApiModelProperty(value = "值")
    private double val;
    private Double val;
    @ApiModelProperty(value = "创建时间")
    private DateTime createTime;
    private Date createTime;
    public VocVals() {
    }
    public int getId() {
    public Integer getId() {
        return id;
    }
    public void setId(int id) {
    public void setId(Integer id) {
        this.id = id;
    }
    public int getX() {
    public Integer getX() {
        return x;
    }
    public void setX(int x) {
    public void setX(Integer x) {
        this.x = x;
    }
    public int getY() {
    public Integer getY() {
        return y;
    }
    public void setY(int y) {
    public void setY(Integer y) {
        this.y = y;
    }
    public double getVal() {
    public Double getVal() {
        return val;
    }
    public void setVal(double val) {
    public void setVal(Double val) {
        this.val = val;
    }
    public DateTime getCreateTime() {
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(DateTime createTime) {
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}
src/main/java/com/yssh/scheduled/ReadCsvTask.java
@@ -1,11 +1,13 @@
package com.yssh.scheduled;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.yssh.entity.VocVals;
import com.yssh.service.VocValsService;
import com.yssh.utils.VocParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,6 +25,7 @@
import com.yssh.utils.DateUtils;
import com.yssh.utils.TableStrategy;
import javax.annotation.Resource;
@Component
public class ReadCsvTask {
@@ -34,6 +37,9 @@
    @Autowired
    private SuYuanServiceImpl suYuanService;
    @Resource
    private VocValsService vocValsService;
    @Autowired
    private IDictRecordService dictRecordService;
@@ -62,7 +68,12 @@
                continue;
            }
            int count = vocValsService.countByTime(time);
            if (count > 0) {
                continue;
            }
            EasyCsv.read(filePath, VocVals.class, new VocParser(vocValsService, calendar.getTime())).doRead();
        }
    }
src/main/java/com/yssh/service/IAsyncService.java
@@ -5,11 +5,10 @@
import com.yssh.dao.BaseMapper;
public interface IAsyncService {
    final Integer BATCH_INSERT_NUMBER = 1000;
    final Integer BATCH_INSERT_500 = 500;
    <T> void executeAsync(String tableName, List<T> lists, BaseMapper mapper, CountDownLatch countDownLatch);
}
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();
        }
    }
}
src/main/java/com/yssh/utils/VocParser.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,65 @@
package com.yssh.utils;
import com.github.biyanwen.impl.AbstractCsvFileParser;
import com.yssh.entity.VocVals;
import com.yssh.service.VocValsService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * VOC转换类
 * @author WWW
 * @date 2023-06-05
 */
public class VocParser extends AbstractCsvFileParser<VocVals> {
    protected final Log logger = LogFactory.getLog(this.getClass());
    /**
     * æ¯éš”5000条入库一次
     */
    public static final int BATCH_COUNT = 5000;
    private VocValsService vocValsService;
    private Date date;
    private List<VocVals> list = new ArrayList<>(BATCH_COUNT);
    public VocParser(VocValsService vocValsService, Date date) {
        this.vocValsService = vocValsService;
        this.date = date;
    }
    @Override
    protected void doAfterAllAnalysed() {
        inserts();
    }
    @Override
    protected void invoke(VocVals vv) {
        if (null == vv.getX() || vv.getX() < 0 || null == vv.getY() || vv.getY() < 0 || null == vv.getVal() || vv.getVal() <= 0) {
            return;
        }
        vv.setCreateTime(this.date);
        list.add(vv);
        if (list.size() >= BATCH_COUNT) {
            inserts();
            list = new ArrayList<>(BATCH_COUNT);
        }
    }
    private void inserts() {
        try {
            this.vocValsService.insertVocVals(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }
}