enhancement #I7GMTS 本地文件监听 catch 异常,保证监听线程不被停止

This commit is contained in:
Kugaaa
2023-07-12 14:59:30 +08:00
parent 0b54beacbe
commit 0f35cedf0f
2 changed files with 37 additions and 2 deletions

View File

@@ -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);
}
}
});
// 创建文件变化监听器

View File

@@ -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());
}
}