From fa8ff86d0592500e4228442b8cf717ea75873b67 Mon Sep 17 00:00:00 2001 From: tangkc Date: Mon, 13 Jun 2022 16:20:25 +0800 Subject: [PATCH] =?UTF-8?q?[I5BR5M]=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/core/FlowExecutor.java | 39 +++++++++++-------- .../com/yomahub/liteflow/flow/FlowBus.java | 7 ++++ .../exception/Exception1SpringBootTest.java | 16 +++----- .../test/exception/Exception1Test.java | 16 +++----- .../exception/Exception1SpringBootTest.java | 18 +++------ .../test/exception/Exception1SpringTest.java | 16 +++----- 6 files changed, 50 insertions(+), 62 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java index d716bc8e3..b32c5b04e 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java @@ -65,13 +65,13 @@ public class FlowExecutor { DataBus.init(); } - public FlowExecutor(LiteflowConfig liteflowConfig){ + public FlowExecutor(LiteflowConfig liteflowConfig) { this.liteflowConfig = liteflowConfig; //把liteFlowConfig设到LiteFlowGetter中去 LiteflowConfigGetter.setLiteflowConfig(liteflowConfig); //设置FlowExecutor的Holder,虽然大部分地方都可以通过Spring上下文获取到,但放入Holder,还是为了某些地方能方便的取到 FlowExecutorHolder.setHolder(this); - if (liteflowConfig.isParseOnStart()){ + if (liteflowConfig.isParseOnStart()) { this.init(); } //初始化DataBus @@ -86,9 +86,11 @@ public class FlowExecutor { throw new ConfigErrorException("config error, please check liteflow config property"); } - if (StrUtil.isBlank(liteflowConfig.getRuleSource())){ + if (StrUtil.isBlank(liteflowConfig.getRuleSource())) { return; } + // 初始化前清空缓存 + FlowBus.cleanChainMap(); List sourceRulePathList = Lists.newArrayList(liteflowConfig.getRuleSource().split(",|;")); @@ -128,7 +130,7 @@ public class FlowExecutor { throw new ConfigErrorException("parse error, please check liteflow config property"); } } - } catch (CyclicDependencyException e){ + } catch (CyclicDependencyException e) { LOG.error(e.getMessage()); throw e; } catch (Exception e) { @@ -139,22 +141,25 @@ public class FlowExecutor { } //单类型的配置文件,需要一起解析 - if (!liteflowConfig.isSupportMultipleType()){ + if (!liteflowConfig.isSupportMultipleType()) { //检查Parser是否只有一个,因为多个不同的parser会造成子流程的混乱 - if (parserNameSet.size() > 1){ + if (parserNameSet.size() > 1) { String errorMsg = "cannot have multiple different parsers"; LOG.error(errorMsg); throw new MultipleParsersException(errorMsg); } //进行多个配置文件的一起解析 - try{ + try { if (ObjectUtil.isNotNull(parser)) { parser.parseMain(rulePathList); } else { throw new ConfigErrorException("parse error, please check liteflow config property"); } - } catch (CyclicDependencyException e){ + } catch (CyclicDependencyException e) { + LOG.error(e.getMessage()); + throw e; + } catch (ChainDuplicateException e) { LOG.error(e.getMessage()); throw e; } catch (Exception e) { @@ -270,7 +275,7 @@ public class FlowExecutor { this.execute(chainId, param, null, slotIndex, true); } - public LiteflowResponse invoke2Resp(String chainId, Object param, Integer slotIndex){ + public LiteflowResponse invoke2Resp(String chainId, Object param, Integer slotIndex) { return this.execute2Resp(chainId, param, null, slotIndex, true); } @@ -293,7 +298,7 @@ public class FlowExecutor { } private T execute(String chainId, Object param, Class contextBeanClazz, - Integer slotIndex, boolean isInnerChain) throws Exception { + Integer slotIndex, boolean isInnerChain) throws Exception { Slot slot = this.doExecute(chainId, param, contextBeanClazz, slotIndex, isInnerChain); if (ObjectUtil.isNotNull(slot.getException())) { throw slot.getException(); @@ -338,14 +343,14 @@ public class FlowExecutor { } private Slot doExecute(String chainId, Object param, Class contextBeanClazz, Integer slotIndex, - boolean isInnerChain) { + boolean isInnerChain) { if (FlowBus.needInit()) { init(); } if (!isInnerChain && ObjectUtil.isNull(slotIndex)) { slotIndex = DataBus.offerSlot(contextBeanClazz); - if (BooleanUtil.isTrue(liteflowConfig.getPrintExecutionLog())){ + if (BooleanUtil.isTrue(liteflowConfig.getPrintExecutionLog())) { LOG.info("slot[{}] offered", slotIndex); } } @@ -361,18 +366,18 @@ public class FlowExecutor { if (StrUtil.isBlank(slot.getRequestId())) { slot.generateRequestId(); - if (BooleanUtil.isTrue(liteflowConfig.getPrintExecutionLog())){ + if (BooleanUtil.isTrue(liteflowConfig.getPrintExecutionLog())) { LOG.info("requestId[{}] has generated", slot.getRequestId()); } } if (!isInnerChain) { - if (ObjectUtil.isNotNull(param)){ + if (ObjectUtil.isNotNull(param)) { slot.setRequestData(param); } slot.setChainName(chainId); } else { - if (ObjectUtil.isNotNull(param)){ + if (ObjectUtil.isNotNull(param)) { slot.setChainReqData(chainId, param); } } @@ -388,12 +393,12 @@ public class FlowExecutor { // 执行chain chain.execute(slotIndex); } catch (ChainEndException e) { - if (ObjectUtil.isNotNull(chain)){ + if (ObjectUtil.isNotNull(chain)) { String warnMsg = StrUtil.format("[{}]:chain[{}] execute end on slot[{}]", slot.getRequestId(), chain.getChainName(), slotIndex); LOG.warn(warnMsg); } } catch (Exception e) { - if (ObjectUtil.isNotNull(chain)){ + if (ObjectUtil.isNotNull(chain)) { String errMsg = StrUtil.format("[{}]:chain[{}] execute error on slot[{}]", slot.getRequestId(), chain.getChainName(), slotIndex); LOG.error(errMsg, e); } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java index 2a1c886d2..2cefb41f3 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java @@ -189,6 +189,10 @@ public class FlowBus { cleanScriptCache(); } + public static void cleanChainMap(){ + chainMap.clear(); + } + public static void cleanScriptCache() { //如果引入了脚本组件SPI,则还需要清理脚本的缓存 try { @@ -201,6 +205,9 @@ public class FlowBus { } public static void refreshFlowMetaData(FlowParserTypeEnum type, String content) throws Exception { + // 刷新前清空缓存 + cleanChainMap(); + if (type.equals(FlowParserTypeEnum.TYPE_XML)) { new LocalXmlFlowParser().parse(content); } else if (type.equals(FlowParserTypeEnum.TYPE_JSON)) { diff --git a/liteflow-testcase-declare-component/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringBootTest.java b/liteflow-testcase-declare-component/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringBootTest.java index bd1e2fa90..dfe350393 100644 --- a/liteflow-testcase-declare-component/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringBootTest.java +++ b/liteflow-testcase-declare-component/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringBootTest.java @@ -1,12 +1,12 @@ package com.yomahub.liteflow.test.exception; import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.exception.ChainDuplicateException; import com.yomahub.liteflow.exception.ConfigErrorException; import com.yomahub.liteflow.exception.FlowExecutorNotInitException; import com.yomahub.liteflow.property.LiteflowConfig; import com.yomahub.liteflow.property.LiteflowConfigGetter; import com.yomahub.liteflow.test.BaseTest; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -32,17 +32,11 @@ public class Exception1SpringBootTest extends BaseTest { /** * 验证 chain 节点重复的异常 */ - @Test(expected = FlowExecutorNotInitException.class) + @Test(expected = ChainDuplicateException.class) public void testChainDuplicateException() { - try { - LiteflowConfig config = LiteflowConfigGetter.get(); - config.setRuleSource("exception/flow-exception.xml"); - flowExecutor.init(); - } catch (Exception ex) { - // 这里需要 catch 是因为,异常是往上抛的,最后被包装成了 FlowExecutorNotInitException - Assert.assertTrue(ex.getMessage().contains("[chain name duplicate]")); - throw ex; - } + LiteflowConfig config = LiteflowConfigGetter.get(); + config.setRuleSource("exception/flow-exception.xml"); + flowExecutor.init(); } @Test(expected = ConfigErrorException.class) diff --git a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/Exception1Test.java b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/Exception1Test.java index b062dd7fb..5be900a90 100644 --- a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/Exception1Test.java +++ b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/Exception1Test.java @@ -2,12 +2,12 @@ package com.yomahub.liteflow.test.exception; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.core.FlowExecutorHolder; +import com.yomahub.liteflow.exception.ChainDuplicateException; import com.yomahub.liteflow.exception.ConfigErrorException; import com.yomahub.liteflow.exception.FlowExecutorNotInitException; import com.yomahub.liteflow.property.LiteflowConfig; import com.yomahub.liteflow.property.LiteflowConfigGetter; import com.yomahub.liteflow.test.BaseTest; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -32,17 +32,11 @@ public class Exception1Test extends BaseTest { /** * 验证 chain 节点重复的异常 */ - @Test(expected = FlowExecutorNotInitException.class) + @Test(expected = ChainDuplicateException.class) public void testChainDuplicateException() { - try { - LiteflowConfig config = LiteflowConfigGetter.get(); - config.setRuleSource("exception/flow-exception.xml"); - flowExecutor.init(); - } catch (Exception ex) { - // 这里需要 catch 是因为,异常是往上抛的,最后被包装成了 FlowExecutorNotInitException - Assert.assertTrue(ex.getMessage().contains("[chain name duplicate]")); - throw ex; - } + LiteflowConfig config = LiteflowConfigGetter.get(); + config.setRuleSource("exception/flow-exception.xml"); + flowExecutor.init(); } @Test(expected = ConfigErrorException.class) diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringBootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringBootTest.java index 65b7b7edc..8c53b8c85 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringBootTest.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringBootTest.java @@ -1,12 +1,12 @@ package com.yomahub.liteflow.test.exception; import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.exception.ChainDuplicateException; import com.yomahub.liteflow.exception.ConfigErrorException; import com.yomahub.liteflow.exception.FlowExecutorNotInitException; import com.yomahub.liteflow.property.LiteflowConfig; import com.yomahub.liteflow.property.LiteflowConfigGetter; import com.yomahub.liteflow.test.BaseTest; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -25,24 +25,18 @@ import javax.annotation.Resource; @SpringBootTest(classes = Exception1SpringBootTest.class) @EnableAutoConfiguration public class Exception1SpringBootTest extends BaseTest { - + @Resource private FlowExecutor flowExecutor; /** * 验证 chain 节点重复的异常 */ - @Test(expected = FlowExecutorNotInitException.class) + @Test(expected = ChainDuplicateException.class) public void testChainDuplicateException() { - try { - LiteflowConfig config = LiteflowConfigGetter.get(); - config.setRuleSource("exception/flow-exception.xml"); - flowExecutor.init(); - } catch (Exception ex) { - // 这里需要 catch 是因为,异常是往上抛的,最后被包装成了 FlowExecutorNotInitException - Assert.assertTrue(ex.getMessage().contains("[chain name duplicate]")); - throw ex; - } + LiteflowConfig config = LiteflowConfigGetter.get(); + config.setRuleSource("exception/flow-exception.xml"); + flowExecutor.init(); } @Test(expected = ConfigErrorException.class) diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringTest.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringTest.java index fbb9d3c9e..673cfc3a7 100644 --- a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringTest.java +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/exception/Exception1SpringTest.java @@ -1,12 +1,12 @@ package com.yomahub.liteflow.test.exception; import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.exception.ChainDuplicateException; import com.yomahub.liteflow.exception.ConfigErrorException; import com.yomahub.liteflow.exception.FlowExecutorNotInitException; import com.yomahub.liteflow.property.LiteflowConfig; import com.yomahub.liteflow.property.LiteflowConfigGetter; import com.yomahub.liteflow.test.BaseTest; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; @@ -30,17 +30,11 @@ public class Exception1SpringTest extends BaseTest { /** * 验证 chain 节点重复的异常 */ - @Test(expected = FlowExecutorNotInitException.class) + @Test(expected = ChainDuplicateException.class) public void testChainDuplicateException() { - try { - LiteflowConfig config = LiteflowConfigGetter.get(); - config.setRuleSource("exception/flow-exception.xml"); - flowExecutor.init(); - } catch (Exception ex) { - // 这里需要 catch 是因为,异常是往上抛的,最后被包装成了 FlowExecutorNotInitException - Assert.assertTrue(ex.getMessage().contains("[chain name duplicate]")); - throw ex; - } + LiteflowConfig config = LiteflowConfigGetter.get(); + config.setRuleSource("exception/flow-exception.xml"); + flowExecutor.init(); } @Test(expected = ConfigErrorException.class)