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