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 queue = new LinkedList<>(); private List 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().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 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 taskQueueList) { this.taskQueueList.addAll(taskQueueList); } public void awakeTasktch(){ this.taskLatch.countDown(); } }