From 80cbd8366d7c8dda1a65f8b967eb1d8b6d2f235e Mon Sep 17 00:00:00 2001 From: "everywhere.z" Date: Mon, 19 Jun 2023 16:00:37 +0800 Subject: [PATCH] =?UTF-8?q?bug=20#I7EKS8=20=E5=9C=A8isAccess=E4=B8=AD?= =?UTF-8?q?=E8=BF=9B=E8=A1=8CsetIsEnd(true)=E6=B5=81=E7=A8=8B=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E7=BB=93=E6=9D=9F=E7=9A=84=E9=97=AE=E9=A2=98=20bug=20?= =?UTF-8?q?#I7EKP3=20=E5=90=8C=E4=B8=80=E4=B8=AA=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=87=8C=E5=88=86=E5=88=AB=E8=B0=83=E7=94=A82=E4=B8=AAChain?= =?UTF-8?q?=EF=BC=8CcurrObj=E6=B2=A1=E6=9C=89=E9=9A=94=E7=A6=BB=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/flow/element/Node.java | 24 ++++++---- .../flow/element/condition/ForCondition.java | 32 +++++++------ .../element/condition/IteratorCondition.java | 45 ++++++++++--------- .../flow/element/condition/LoopCondition.java | 26 +++++++++++ 4 files changed, 84 insertions(+), 43 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java index cb6148ce3..1f1d958d5 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java @@ -56,9 +56,9 @@ public class Node implements Executable, Cloneable{ private String currChainId; - private final TransmittableThreadLocal loopIndexTL = new TransmittableThreadLocal<>(); + private TransmittableThreadLocal loopIndexTL = new TransmittableThreadLocal<>(); - private final TransmittableThreadLocal currLoopObject = new TransmittableThreadLocal<>(); + private TransmittableThreadLocal currLoopObject = new TransmittableThreadLocal<>(); public Node() { @@ -77,14 +77,17 @@ public class Node implements Executable, Cloneable{ return id; } + @Override public void setId(String id) { this.id = id; } + @Override public String getTag() { return tag; } + @Override public void setTag(String tag) { this.tag = tag; } @@ -141,18 +144,18 @@ public class Node implements Executable, Cloneable{ .buildNodeExecutor(instance.getNodeExecutorClass()); // 调用节点执行器进行执行 nodeExecutor.execute(instance); - // 如果组件覆盖了isEnd方法,或者在在逻辑中主要调用了setEnd(true)的话,流程就会立马结束 - if (instance.isEnd()) { - String errorInfo = StrUtil.format("[{}]:[{}] lead the chain to end", slot.getRequestId(), - instance.getDisplayName()); - throw new ChainEndException(errorInfo); - } } else { if (BooleanUtil.isTrue(liteflowConfig.getPrintExecutionLog())) { LOG.info("[{}]:[X]skip component[{}] execution", slot.getRequestId(), instance.getDisplayName()); } } + // 如果组件覆盖了isEnd方法,或者在在逻辑中主要调用了setEnd(true)的话,流程就会立马结束 + if (instance.isEnd()) { + String errorInfo = StrUtil.format("[{}]:[{}] lead the chain to end", slot.getRequestId(), + instance.getDisplayName()); + throw new ChainEndException(errorInfo); + } } catch (ChainEndException e) { throw e; @@ -273,6 +276,9 @@ public class Node implements Executable, Cloneable{ } public Node copy() throws Exception { - return (Node)this.clone(); + Node node = (Node)this.clone(); + node.loopIndexTL = new TransmittableThreadLocal<>(); + node.currLoopObject = new TransmittableThreadLocal<>(); + return node; } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/ForCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/ForCondition.java index 5bfd827ba..496987c83 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/ForCondition.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/ForCondition.java @@ -45,22 +45,26 @@ public class ForCondition extends LoopCondition { // 获取Break节点 Executable breakItem = this.getBreakItem(); - // 循环执行 - for (int i = 0; i < forCount; i++) { - executableItem.setCurrChainId(this.getCurrChainId()); - // 设置循环index - setLoopIndex(executableItem, i); - executableItem.execute(slotIndex); - // 如果break组件不为空,则去执行 - if (ObjectUtil.isNotNull(breakItem)) { - breakItem.setCurrChainId(this.getCurrChainId()); - setLoopIndex(breakItem, i); - breakItem.execute(slotIndex); - boolean isBreak = breakItem.getItemResultMetaValue(slotIndex); - if (isBreak) { - break; + try{ + // 循环执行 + for (int i = 0; i < forCount; i++) { + executableItem.setCurrChainId(this.getCurrChainId()); + // 设置循环index + setLoopIndex(executableItem, i); + executableItem.execute(slotIndex); + // 如果break组件不为空,则去执行 + if (ObjectUtil.isNotNull(breakItem)) { + breakItem.setCurrChainId(this.getCurrChainId()); + setLoopIndex(breakItem, i); + breakItem.execute(slotIndex); + boolean isBreak = breakItem.getItemResultMetaValue(slotIndex); + if (isBreak) { + break; + } } } + }finally { + removeLoopIndex(executableItem); } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/IteratorCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/IteratorCondition.java index 1d1e15d1f..3781d7e26 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/IteratorCondition.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/IteratorCondition.java @@ -41,29 +41,34 @@ public class IteratorCondition extends LoopCondition { // 获取Break节点 Executable breakItem = this.getBreakItem(); - int index = 0; - while (it.hasNext()) { - Object itObj = it.next(); + try{ + int index = 0; + while (it.hasNext()) { + Object itObj = it.next(); - executableItem.setCurrChainId(this.getCurrChainId()); - // 设置循环index - setLoopIndex(executableItem, index); - // 设置循环迭代器对象 - setCurrLoopObject(executableItem, itObj); - // 执行可执行对象 - executableItem.execute(slotIndex); - // 如果break组件不为空,则去执行 - if (ObjectUtil.isNotNull(breakItem)) { - breakItem.setCurrChainId(this.getCurrChainId()); - setLoopIndex(breakItem, index); - setCurrLoopObject(breakItem, itObj); - breakItem.execute(slotIndex); - boolean isBreak = breakItem.getItemResultMetaValue(slotIndex); - if (isBreak) { - break; + executableItem.setCurrChainId(this.getCurrChainId()); + // 设置循环index + setLoopIndex(executableItem, index); + // 设置循环迭代器对象 + setCurrLoopObject(executableItem, itObj); + // 执行可执行对象 + executableItem.execute(slotIndex); + // 如果break组件不为空,则去执行 + if (ObjectUtil.isNotNull(breakItem)) { + breakItem.setCurrChainId(this.getCurrChainId()); + setLoopIndex(breakItem, index); + setCurrLoopObject(breakItem, itObj); + breakItem.execute(slotIndex); + boolean isBreak = breakItem.getItemResultMetaValue(slotIndex); + if (isBreak) { + break; + } } + index++; } - index++; + }finally{ + removeLoopIndex(executableItem); + removeCurrLoopObject(executableItem); } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/LoopCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/LoopCondition.java index 7ad2609c4..4f89b3ea8 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/LoopCondition.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/LoopCondition.java @@ -55,4 +55,30 @@ public abstract class LoopCondition extends Condition { } } + protected void removeLoopIndex(Executable executableItem){ + if (executableItem instanceof Chain) { + ((Chain) executableItem).getConditionList().forEach(this::removeLoopIndex); + } + else if (executableItem instanceof Condition) { + ((Condition) executableItem).getExecutableGroup() + .forEach((key, value) -> value.forEach(this::removeLoopIndex)); + } + else if (executableItem instanceof Node) { + ((Node) executableItem).removeLoopIndex(); + } + } + + protected void removeCurrLoopObject(Executable executableItem){ + if (executableItem instanceof Chain) { + ((Chain) executableItem).getConditionList().forEach(this::removeCurrLoopObject); + } + else if (executableItem instanceof Condition) { + ((Condition) executableItem).getExecutableGroup() + .forEach((key, value) -> value.forEach(this::removeCurrLoopObject)); + } + else if (executableItem instanceof Node) { + ((Node) executableItem).removeCurrLoopObject(); + } + } + }