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 1641688be..9d7983d9f 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 @@ -6,13 +6,14 @@ import cn.hutool.core.util.CharsetUtil; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.flow.LiteflowResponse; import com.yomahub.liteflow.test.BaseTest; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ComponentScan; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.io.File; @@ -21,6 +22,7 @@ import java.io.File; @SpringBootTest(classes = MonitorFileELSpringbootTest.class) @EnableAutoConfiguration @ComponentScan({ "com.yomahub.liteflow.test.monitorFile.cmp" }) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class MonitorFileELSpringbootTest extends BaseTest { @Resource @@ -37,4 +39,28 @@ public class MonitorFileELSpringbootTest extends BaseTest { Assertions.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"); + Assertions.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"); + Assertions.assertEquals("c==>b==>a", reloadSuccessResponse.getExecuteStepStr()); + } + }