From f92ad3f3a6f18c3f52f7a1c5c9b7782727b07efd Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 23 Sep 2024 09:55:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E7=9B=91=E5=90=AC=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fit2-zhao --- .../api/jmeter/ApiLocalRunner.java | 30 ++++--------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/jmeter/ApiLocalRunner.java b/api-test/backend/src/main/java/io/metersphere/api/jmeter/ApiLocalRunner.java index e0919b9e40..cc74dcfc83 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/jmeter/ApiLocalRunner.java +++ b/api-test/backend/src/main/java/io/metersphere/api/jmeter/ApiLocalRunner.java @@ -2,10 +2,8 @@ package io.metersphere.api.jmeter; import io.metersphere.jmeter.LocalRunner; import io.metersphere.utils.LoggerUtil; -import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import lombok.NoArgsConstructor; -import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.engine.StandardJMeterEngine; import org.apache.jorphan.collections.HashTree; @@ -17,15 +15,13 @@ import java.util.concurrent.ConcurrentHashMap; @Component @NoArgsConstructor public class ApiLocalRunner extends LocalRunner { - private final static Map runningTasks = new ConcurrentHashMap<>(); - private boolean isRunning = true; + private static final Map runningTasks = new ConcurrentHashMap<>(); private HashTree jmxTree; public ApiLocalRunner(HashTree jmxTree) { this.jmxTree = jmxTree; } - public void run(String report) { StandardJMeterEngine engine = new StandardJMeterEngine(); engine.configure(this.jmxTree); @@ -34,12 +30,15 @@ public class ApiLocalRunner extends LocalRunner { LoggerUtil.info("LocalRunner 开始执行报告", report); engine.runTest(); } catch (Exception e) { + LoggerUtil.error("运行报告时出错", e); engine.stopTest(true); + } finally { + runningTasks.remove(report); // Ensure removal on completion } } public static void stop(String report) { - if (MapUtils.isNotEmpty(runningTasks)) { + if (StringUtils.isNotEmpty(report)) { StandardJMeterEngine engine = runningTasks.get(report); if (engine != null) { engine.stopTest(true); @@ -54,26 +53,7 @@ public class ApiLocalRunner extends LocalRunner { } } - @PostConstruct - public void checkRunningTasks() { - new Thread(() -> { - while (isRunning) { - try { - Thread.sleep(1000 * 60); - if (MapUtils.isNotEmpty(runningTasks)) { - runningTasks.keySet().stream() - .filter(reportId -> !runningTasks.get(reportId).isActive()) - .forEach(runningTasks::remove); - } - } catch (Exception e) { - LoggerUtil.error("检查运行中的任务异常:", e); - } - } - }).start(); - } - @PreDestroy public void destroy() { - isRunning = false; } }