From 0f35cedf0fd4dc7aa8ecb9b997847b0e0f4f1453 Mon Sep 17 00:00:00 2001 From: Kugaaa Date: Wed, 12 Jul 2023 14:59:30 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#I7GMTS=20=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9B=91=E5=90=AC=20catch=20=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=EF=BC=8C=E4=BF=9D=E8=AF=81=E7=9B=91=E5=90=AC=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E4=B8=8D=E8=A2=AB=E5=81=9C=E6=AD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/monitor/MonitorFile.java | 12 +++++++-- .../MonitorFileELSpringbootTest.java | 27 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/monitor/MonitorFile.java b/liteflow-core/src/main/java/com/yomahub/liteflow/monitor/MonitorFile.java index 756d54273..fb3226b77 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/monitor/MonitorFile.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/monitor/MonitorFile.java @@ -63,13 +63,21 @@ public class MonitorFile { @Override public void onFileChange(File file) { LOG.info("file modify,filePath={}", file.getAbsolutePath()); - FlowExecutorHolder.loadInstance().reloadRule(); + this.reloadRule(); } @Override public void onFileDelete(File file) { LOG.info("file delete,filePath={}", file.getAbsolutePath()); - FlowExecutorHolder.loadInstance().reloadRule(); + this.reloadRule(); + } + + private void reloadRule() { + try { + FlowExecutorHolder.loadInstance().reloadRule(); + } catch (Exception e) { + LOG.error("reload rule error", e); + } } }); // 创建文件变化监听器 diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/monitorFile/MonitorFileELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/monitorFile/MonitorFileELSpringbootTest.java index 2ffdd06e2..32c895be8 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/monitorFile/MonitorFileELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/monitorFile/MonitorFileELSpringbootTest.java @@ -7,8 +7,10 @@ import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.flow.LiteflowResponse; import com.yomahub.liteflow.test.BaseTest; import org.junit.Assert; +import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ComponentScan; @@ -23,6 +25,7 @@ import java.io.File; @SpringBootTest(classes = MonitorFileELSpringbootTest.class) @EnableAutoConfiguration @ComponentScan({ "com.yomahub.liteflow.test.monitorFile.cmp" }) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MonitorFileELSpringbootTest extends BaseTest { @Resource @@ -39,4 +42,28 @@ public class MonitorFileELSpringbootTest extends BaseTest { Assert.assertEquals("a==>c==>b", response.getExecuteStepStr()); } + /** + * 测试文件变更,但是 EL 规则错误情况 + * 输出 ERROR 日志异常信息,但是不会停止监听线程,当下一次变更正确后替换为新规则 + */ + @Test + public void testMonitorError() throws Exception { + String absolutePath = new ClassPathResource("classpath:/monitorFile/flow.el.xml").getAbsolutePath(); + String content = FileUtil.readUtf8String(absolutePath); + + // 错误规则配置 + String newContent = content.replace("THEN(a, c, b);", "THEN(c, b, ;"); + FileUtil.writeString(newContent, new File(absolutePath), CharsetUtil.CHARSET_UTF_8); + Thread.sleep(3000); + LiteflowResponse reloadFailedResponse = flowExecutor.execute2Resp("chain1", "arg"); + Assert.assertEquals("a==>c==>b", reloadFailedResponse.getExecuteStepStr()); + + // 再次变更正确 + newContent = content.replace("THEN(a, c, b);", "THEN(c, b, a);"); + FileUtil.writeString(newContent, new File(absolutePath), CharsetUtil.CHARSET_UTF_8); + Thread.sleep(3000); + LiteflowResponse reloadSuccessResponse = flowExecutor.execute2Resp("chain1", "arg"); + Assert.assertEquals("c==>b==>a", reloadSuccessResponse.getExecuteStepStr()); + } + }