123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package cn.cslg.pas.service.upLoadPatent;
- import cn.cslg.pas.common.model.vo.UploadParamsVO;
- import cn.cslg.pas.common.model.vo.UploadSettingVO;
- import cn.cslg.pas.common.utils.ReadExcelUtils;
- import cn.cslg.pas.domain.PatentData;
- import cn.cslg.pas.domain.Task;
- import cn.cslg.pas.service.TaskService;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import lombok.RequiredArgsConstructor;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Service;
- import javax.annotation.PostConstruct;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- import java.util.concurrent.CountDownLatch;
- /**
- * 将专利信息存入队列或从队列取出
- *
- * @author 李仁杰
- */
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class PantentQueueService {
- private final ExcuteDataToVOService excuteDataToVOService;
- private final ExcuteUploadSettingService excuteUploadSettingService;
- private final MessageService messageService;
- private final TaskService taskService;
- private Queue<UploadParamsVO> queue = new LinkedList<>();
- private List<Integer> taskQueueList = new ArrayList<>();
- private Boolean flag = false;
- private CountDownLatch patentLatch = new CountDownLatch(1);
- private CountDownLatch taskLatch = new CountDownLatch(1);
- //将专利信息存入队列
- public void addPatnetToQueue() {
- try {
- while (true){
- //检查任务队列
- if (taskQueueList.size() == 0) {
- taskLatch.await();
- }
- //查找 taskQueueList 中有没有进行中的任务
- long count = taskService.count(new LambdaQueryWrapper<Task>().in(Task::getId, taskQueueList).eq(Task::getStatus, 1));
- //若没有,则取出第一个队列中的任务开始执行
- Task task = null;
- if (count == 0) {
- task = taskService.getById(taskQueueList.get(0));
- //任务队列去除该任务
- taskQueueList.remove(0);
- //修改该任务状态,改为进行中
- Task currentTask = new Task();
- currentTask.setId(task.getId());
- currentTask.setStatus(1);
- taskService.updateById(currentTask);
- }
- //获得文件路径
- String filePath = task.getUrl();
- //检查文件合法性
- Integer totalRow = ReadExcelUtils.textExcel(filePath);
- //调用解析数据类,根据数据来源id(如1:智慧芽)解析数据源配置文件信息
- List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting(1 + "");
- //遍历专利总数量,在循环中保存专利
- for (int i = 1; i <= totalRow; i++) {
- //解析读取一行专利
- PatentData patentData = ReadExcelUtils.readExcelOneRow(filePath, i);
- //调用装载数据类,专利数据转换为VO对象
- UploadParamsVO uploadParamsVO = excuteDataToVOService.fileToPatentVO(patentData, jsonData);
- //一个专利加入队列
- queue.add(uploadParamsVO);
- //通知消费者线程
- patentLatch.countDown();
- //Websocket发送message:通过WebSocket 在每一次循环结束后 向前端发送完成进度
- Long percentage = totalRow == 0 ? 0 : Math.round((totalRow.equals(i) ? (i * 1D) : (i + 1D)) / totalRow * 100D);
- messageService.sendWebsocketMessage(task, totalRow, i, percentage);
- }
- //全部循环结束后,发送进度
- Long percentage = 100L;
- messageService.sendWebsocketMessage(task, totalRow, totalRow, percentage);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- }
- flag = true;
- }
- //将专利信息从队列取出
- public void pushPantentToDb() throws InterruptedException {
- try {
- while (true) {
- if (queue.isEmpty()) {
- if (flag) {
- System.out.println("退出循环");
- return;
- } else {
- patentLatch.await();
- }
- } else {
- UploadParamsVO uploadParamsVO = queue.remove();
- System.out.println("出队列" + uploadParamsVO);
- }
- }
- } finally {
- }
- }
- public void queueAddTask(List<Integer> taskQueueList) {
- this.taskQueueList.addAll(taskQueueList);
- }
- public void awakeTasktch(){
- this.taskLatch.countDown();
- }
- }
|