diff --git a/backend/src/main/java/io/metersphere/api/jmeter/MessageCache.java b/backend/src/main/java/io/metersphere/api/jmeter/MessageCache.java index a2be85cbf9..bb0ae78be0 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/MessageCache.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/MessageCache.java @@ -2,10 +2,11 @@ package io.metersphere.api.jmeter; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class MessageCache { public static Map cache = new HashMap<>(); // 串行执行队列 KEY=报告ID VALUE=开始时间 - public static Map executionQueue = new HashMap<>(); + public static Map executionQueue = new ConcurrentHashMap<>(); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 89772c416f..0c6c0f6221 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -1059,13 +1059,14 @@ public class ApiAutomationService { public void run() { List reportIds = new LinkedList<>(); for (APIScenarioReportResult key : map.keySet()) { - apiScenarioReportMapper.insert(key); - reportIds.add(key.getId()); try { + apiScenarioReportMapper.insert(key); + reportIds.add(key.getId()); // 进入执行队列 MessageCache.executionQueue.put(key.getId(), System.currentTimeMillis()); - Future future = executorService.submit(new SerialScenarioExecTask(jMeterService, apiScenarioReportMapper, key.getId(), map.get(key), request)); - ApiScenarioReport report = future.get(); + Future future = executorService.submit(new SerialScenarioExecTask(jMeterService, key.getId(), map.get(key), request)); + future.get(); + ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(key.getId()); // 如果开启失败结束执行,则判断返回结果状态 if (request.getConfig().isOnSampleError()) { if (report == null || !report.getStatus().equals("Success")) { diff --git a/backend/src/main/java/io/metersphere/api/service/task/SerialScenarioExecTask.java b/backend/src/main/java/io/metersphere/api/service/task/SerialScenarioExecTask.java index c8f6fdd9ea..c23cbd2a8b 100644 --- a/backend/src/main/java/io/metersphere/api/service/task/SerialScenarioExecTask.java +++ b/backend/src/main/java/io/metersphere/api/service/task/SerialScenarioExecTask.java @@ -6,10 +6,6 @@ package io.metersphere.api.service.task; import io.metersphere.api.dto.automation.RunScenarioRequest; import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.MessageCache; -import io.metersphere.base.domain.ApiScenarioReport; -import io.metersphere.base.mapper.ApiScenarioReportMapper; -import io.metersphere.commons.constants.APITestStatus; -import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.LogUtil; import org.apache.commons.lang3.StringUtils; import org.apache.jorphan.collections.HashTree; @@ -19,14 +15,11 @@ import java.util.concurrent.Callable; public class SerialScenarioExecTask implements Callable { private RunScenarioRequest request; private JMeterService jMeterService; - private ApiScenarioReportMapper apiScenarioReportMapper; private HashTree hashTree; - ApiScenarioReport report = null; private String id; - public SerialScenarioExecTask(JMeterService jMeterService, ApiScenarioReportMapper apiScenarioReportMapper, String id, HashTree hashTree, RunScenarioRequest request) { + public SerialScenarioExecTask(JMeterService jMeterService, String id, HashTree hashTree, RunScenarioRequest request) { this.jMeterService = jMeterService; - this.apiScenarioReportMapper = apiScenarioReportMapper; this.request = request; this.hashTree = hashTree; this.id = id; @@ -41,19 +34,19 @@ public class SerialScenarioExecTask implements Callable { jMeterService.runSerial(id, hashTree, request.getReportId(), request.getRunMode(), request.getConfig()); } while (MessageCache.executionQueue.containsKey(id)) { - long currentSecond = (System.currentTimeMillis() - MessageCache.executionQueue.get(id)) / 1000 / 60; + Thread.sleep(1000); + long time = MessageCache.executionQueue.get(id); + long currentSecond = (System.currentTimeMillis() - time) / 1000 / 60; // 设置五分钟超时 if (currentSecond > 5) { // 执行失败了,恢复报告状态 - report.setStatus(APITestStatus.Error.name()); - apiScenarioReportMapper.updateByPrimaryKey(report); break; } } - return (T) report; + return null; } catch (Exception ex) { LogUtil.error(ex.getMessage()); - MSException.throwException(ex.getMessage()); + ex.printStackTrace(); return null; } }