From f64b97c7a2cfc930846ec605cca6ed386da13e16 Mon Sep 17 00:00:00 2001 From: luoyi <972849752@qq.com> Date: Wed, 27 Sep 2023 20:40:29 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#I821F1=20=E4=BD=BF=E7=94=A8=20Ja?= =?UTF-8?q?ckson=20=E5=BA=8F=E5=88=97=E5=8C=96=E5=AF=B9=E8=B1=A1=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=20chain=20=E6=98=AF=E5=90=A6=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builder/el/LiteFlowChainELBuilder.java | 32 +++++--- .../yomahub/liteflow/core/FlowExecutor.java | 81 +------------------ .../yomahub/liteflow/flow/element/Node.java | 15 ++-- 3 files changed, 32 insertions(+), 96 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java index 9473efb8..616988d7 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java @@ -4,15 +4,15 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.StrUtil; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.ql.util.express.DefaultContext; import com.ql.util.express.ExpressRunner; import com.ql.util.express.InstructionSet; import com.ql.util.express.exception.QLException; import com.yomahub.liteflow.builder.el.operator.*; import com.yomahub.liteflow.common.ChainConstant; -import com.yomahub.liteflow.exception.DataNotFoundException; -import com.yomahub.liteflow.exception.ELParseException; -import com.yomahub.liteflow.exception.FlowSystemException; +import com.yomahub.liteflow.exception.*; import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.flow.element.Chain; import com.yomahub.liteflow.flow.element.Condition; @@ -34,6 +34,8 @@ public class LiteFlowChainELBuilder { private static final LFLog LOG = LFLoggerManager.getLogger(LiteFlowChainELBuilder.class); + private static ObjectMapper objectMapper =new ObjectMapper(); + private Chain chain; /** @@ -109,15 +111,12 @@ public class LiteFlowChainELBuilder { return this; } - /** - *
原来逻辑从 FlowBus 中获取相应的 chain,如果 EL 表达式中出现嵌套引用 chain,那么在构建 Condition 的时候可能会出现 chain 死循环引用情况
- *故删掉从 FlowBus 中获取的逻辑,直接使用新的 {@link LiteFlowChainELBuilder} 对象。
- * - * @param chainId - * @return LiteFlowChainELBuilder - */ public LiteFlowChainELBuilder setChainId(String chainId) { - this.chain.setChainId(chainId); + if (FlowBus.containChain(chainId)) { + this.chain = FlowBus.getChain(chainId); + } else { + this.chain.setChainId(chainId); + } return this; } @@ -199,6 +198,17 @@ public class LiteFlowChainELBuilder { if (CollUtil.isNotEmpty(errorList)) { throw new RuntimeException(CollUtil.join(errorList, ",", "[", "]")); } + // 对每一个 chain 进行循环引用检测 + try { + objectMapper.writeValueAsString(this.chain); + } catch (Exception e) { + e.printStackTrace(); + if (e instanceof JsonMappingException) { + throw new CyclicDependencyException(StrUtil.format("There is a circular dependency in the chain[{}], please check carefully.", chain.getChainId())); + } else { + throw new ParseException(e.getMessage()); + } + } } /** 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 fea85b47..830ad102 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 @@ -15,7 +15,9 @@ import com.yomahub.liteflow.enums.InnerChainTypeEnum; import com.yomahub.liteflow.exception.*; import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.flow.LiteflowResponse; -import com.yomahub.liteflow.flow.element.*; +import com.yomahub.liteflow.flow.element.Chain; +import com.yomahub.liteflow.flow.element.Node; +import com.yomahub.liteflow.flow.element.Rollbackable; import com.yomahub.liteflow.flow.entity.CmpStep; import com.yomahub.liteflow.flow.id.IdGeneratorHolder; import com.yomahub.liteflow.log.LFLog; @@ -193,9 +195,6 @@ public class FlowExecutor { } } - // 检查构建生成的 chain 的有效性 - checkValidOfChain(); - // 执行钩子 if (isStart) { FlowInitHook.executeHook(); @@ -215,80 +214,6 @@ public class FlowExecutor { } } - /** - * 检查 chain 的有效性,同时重新构建 FlowBus 的 chain,将其子 chain 引用连起来 - * @throws CyclicDependencyException - */ - private void checkValidOfChain() { - - // 存储已经构建完的有效的 chain 对应 Id - Set