!330 [ISSUE #IC2F4F] 增加对模糊路径下新增文件的监听

Merge pull request !330 from DaleLee/monitor_added_file
This commit is contained in:
铂赛东
2025-05-28 05:59:36 +00:00
committed by Gitee
3 changed files with 91 additions and 1 deletions

View File

@@ -72,7 +72,13 @@ public class MonitorFile {
this.reloadRule();
}
private void reloadRule() {
@Override
public void onFileCreate(File file) {
LOG.info("file create,filePath={}", file.getAbsolutePath());
this.reloadRule();
}
private void reloadRule() {
try {
FlowExecutorHolder.loadInstance().reloadRule();
} catch (Exception e) {

View File

@@ -0,0 +1,82 @@
package com.yomahub.liteflow.test.monitorFile;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.CharsetUtil;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.exception.ChainNotFoundException;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.test.BaseTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
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 javax.annotation.Resource;
import java.nio.file.Path;
import java.nio.file.Paths;
@TestPropertySource(value = "classpath:/monitorFile/application2.properties")
@SpringBootTest(classes = MonitorFileELSpringbootTest2.class)
@EnableAutoConfiguration
@ComponentScan({ "com.yomahub.liteflow.test.monitorFile.cmp" })
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MonitorFileELSpringbootTest2 extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
// 测试模糊路径下监听新增文件
@Test
public void testMonitorAddFile() throws Exception {
// 文件新增前
LiteflowResponse liteflowResponse = flowExecutor.execute2Resp("chain_add", "arg");
Assertions.assertFalse(liteflowResponse.isSuccess());
Assertions.assertTrue(liteflowResponse.getCause() instanceof ChainNotFoundException);
// 文件新增
String flowPath = new ClassPathResource("classpath:monitorFile").getAbsolutePath();
Path newFilePath = Paths.get(flowPath, "test", "flow.el.2.xml");
FileUtil.writeString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<flow><chain id=\"chain_add\">THEN(a);</chain></flow>",
newFilePath.toFile(), CharsetUtil.CHARSET_UTF_8);
Thread.sleep(1000);
liteflowResponse = flowExecutor.execute2Resp("chain_add", "arg");
Assertions.assertTrue(liteflowResponse.isSuccess());
Assertions.assertEquals("a", liteflowResponse.getExecuteStepStr());
}
// 测试新增不符合匹配规则的文件
@Test
public void testMonitorAddFileNotMatch() throws Exception {
// 文件新增前
LiteflowResponse liteflowResponse = flowExecutor.execute2Resp("chain_not_match", "arg");
Assertions.assertFalse(liteflowResponse.isSuccess());
Assertions.assertTrue(liteflowResponse.getCause() instanceof ChainNotFoundException);
// 文件新增
String flowPath = new ClassPathResource("classpath:monitorFile").getAbsolutePath();
Path newFilePath = Paths.get(flowPath, "test", "test.flow.el.2.xml");
FileUtil.writeString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<flow><chain id=\"chain_not_match\">THEN(a);</chain></flow>",
newFilePath.toFile(), CharsetUtil.CHARSET_UTF_8);
Thread.sleep(1000);
liteflowResponse = flowExecutor.execute2Resp("chain_not_match", "arg");
Assertions.assertFalse(liteflowResponse.isSuccess());
Assertions.assertTrue(liteflowResponse.getCause() instanceof ChainNotFoundException);
}
@AfterEach
public void afterEach(){
// 删除新增文件
String flowPath = new ClassPathResource("classpath:monitorFile").getAbsolutePath();
Path newFilePath = Paths.get(flowPath, "test");
FileUtil.del(newFilePath);
}
}

View File

@@ -0,0 +1,2 @@
liteflow.rule-source=monitorFile/**/flow*.xml
liteflow.enable-monitor-file=true