From 273a408d158ee0f729b68e963a893d43fe46b7b2 Mon Sep 17 00:00:00 2001 From: sikadai <466608943@qq.com> Date: Mon, 14 Feb 2022 15:34:51 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=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 | 24 +++++-------------- .../yomahub/liteflow/entity/flow/Chain.java | 17 +++++++++++-- 2 files changed, 21 insertions(+), 20 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 03cbe1a08..90405b966 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 @@ -13,24 +13,21 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import com.google.common.collect.Lists; +import com.yomahub.liteflow.entity.data.DataBus; +import com.yomahub.liteflow.entity.data.DefaultSlot; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.entity.data.Slot; +import com.yomahub.liteflow.entity.flow.Chain; import com.yomahub.liteflow.entity.flow.Node; import com.yomahub.liteflow.enums.FlowParserTypeEnum; import com.yomahub.liteflow.exception.*; +import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.parser.*; import com.yomahub.liteflow.property.LiteflowConfig; import com.yomahub.liteflow.util.SpringAware; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.yomahub.liteflow.entity.flow.Chain; -import com.yomahub.liteflow.entity.data.DataBus; -import com.yomahub.liteflow.entity.data.DefaultSlot; -import com.yomahub.liteflow.entity.data.LiteflowResponse; -import com.yomahub.liteflow.entity.data.Slot; -import com.yomahub.liteflow.flow.FlowBus; -import com.yomahub.liteflow.parser.LocalXmlFlowParser; -import com.yomahub.liteflow.parser.XmlFlowParser; -import com.yomahub.liteflow.parser.ZookeeperXmlFlowParser; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -384,15 +381,6 @@ public class FlowExecutor { } slot.setException(e); } finally { - try{ - if (ObjectUtil.isNotNull(chain)){ - chain.executeFinally(slotIndex); - } - }catch (Exception e){ - String errMsg = StrUtil.format("[{}]:an exception occurred during the finally Component execution in chain[{}]", slot.getRequestId(), chain.getChainName()); - LOG.error(errMsg, e); - } - if (!isInnerChain) { slot.printStep(); DataBus.releaseSlot(slotIndex); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java index b30c5f549..c351945d4 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java @@ -24,8 +24,13 @@ import com.yomahub.liteflow.property.LiteflowConfigGetter; import com.yomahub.liteflow.thread.ExecutorHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; -import java.util.concurrent.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** @@ -74,7 +79,15 @@ public class Chain implements Executable { if (CollUtil.isEmpty(conditionList)) { throw new FlowSystemException("no conditionList in this chain[" + chainName + "]"); } + try { + executeBody(slotIndex); + } finally { + executeFinally(slotIndex); + } + } + // 执行主体的逻辑 + private void executeBody(Integer slotIndex) throws Exception { //循环chain里包含的condition,每一个condition分四种类型:pre,then,when,finally //这里conditionList其实已经是有序的,pre一定在最前面,finally一定在最后面 for (Condition condition : conditionList) { From e34416574d0dc60bdd70ba06b6379e6e418b138b Mon Sep 17 00:00:00 2001 From: daiqi <466608943@qq.com> Date: Fri, 11 Mar 2022 21:42:12 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BD=BF=E7=94=A8ContextAwareHolder.loadCo?= =?UTF-8?q?ntextAware=E6=94=B9=E9=80=A0=E8=8E=B7=E5=8F=96=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=9E=84=E5=BB=BA=E8=80=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/yomahub/liteflow/core/FlowExecutor.java | 9 +++++++++ .../java/com/yomahub/liteflow/entity/flow/Chain.java | 11 ----------- .../com/yomahub/liteflow/thread/ExecutorHelper.java | 4 +++- 3 files changed, 12 insertions(+), 12 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 6fb36bf1e..090d4080c 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 @@ -398,6 +398,15 @@ public class FlowExecutor { } slot.setException(e); } finally { + try{ + if (ObjectUtil.isNotNull(chain)){ + chain.executeFinally(slotIndex); + } + }catch (Exception e){ + String errMsg = StrUtil.format("[{}]:an exception occurred during the finally Component execution in chain[{}]", slot.getRequestId(), chain.getChainName()); + LOG.error(errMsg, e); + } + if (!isInnerChain) { slot.printStep(); DataBus.releaseSlot(slotIndex); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java index c351945d4..88b4495b5 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java @@ -79,17 +79,6 @@ public class Chain implements Executable { if (CollUtil.isEmpty(conditionList)) { throw new FlowSystemException("no conditionList in this chain[" + chainName + "]"); } - try { - executeBody(slotIndex); - } finally { - executeFinally(slotIndex); - } - } - - // 执行主体的逻辑 - private void executeBody(Integer slotIndex) throws Exception { - //循环chain里包含的condition,每一个condition分四种类型:pre,then,when,finally - //这里conditionList其实已经是有序的,pre一定在最前面,finally一定在最后面 for (Condition condition : conditionList) { if (condition instanceof PreCondition){ for (Executable executableItem : condition.getNodeList()) { diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java index 2bfca78cf..5b1163d40 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java @@ -14,6 +14,7 @@ import com.google.common.collect.Maps; import com.yomahub.liteflow.exception.ThreadExecutorServiceCreateException; import com.yomahub.liteflow.property.LiteflowConfig; import com.yomahub.liteflow.property.LiteflowConfigGetter; +import com.yomahub.liteflow.spi.holder.ContextAwareHolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -123,7 +124,8 @@ public class ExecutorHelper { */ private ExecutorBuilder getExecutorBuilder(String threadExecutorClass) { try { - return (ExecutorBuilder) Class.forName(threadExecutorClass).newInstance(); + Class executorClass = (Class) Class.forName(threadExecutorClass); + return ContextAwareHolder.loadContextAware().registerBean(executorClass); } catch (Exception e) { LOG.error(e.getMessage(), e); throw new ThreadExecutorServiceCreateException(e.getMessage()); From 3fbcb61e7e50aecea23c837fd2f1e79ab6219f98 Mon Sep 17 00:00:00 2001 From: daiqi <466608943@qq.com> Date: Fri, 11 Mar 2022 21:43:39 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/yomahub/liteflow/thread/ExecutorHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java index 5b1163d40..9bf23839b 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java @@ -117,7 +117,7 @@ public class ExecutorHelper { * 根据线程执行构建者Class类名获取ExecutorBuilder实例 *

* - * @param threadExecutorClass + * @param threadExecutorClass 线程执行class全量名 * @return com.yomahub.liteflow.thread.ExecutorBuilder * @author sikadai * @date 2022/1/21 23:04 From 072bc9c01475cc7911cc72a050cd877d70378b31 Mon Sep 17 00:00:00 2001 From: daiqi <466608943@qq.com> Date: Fri, 11 Mar 2022 21:55:52 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=B0=86chain=E4=B8=AD=E7=9A=84pre?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E5=89=A5=E7=A6=BB=E8=87=B3flowExecutor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/core/FlowExecutor.java | 3 ++- .../yomahub/liteflow/entity/flow/Chain.java | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 8 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 090d4080c..c0db15eb4 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 @@ -383,7 +383,8 @@ public class FlowExecutor { String errorMsg = StrUtil.format("[{}]:couldn't find chain with the id[{}]", slot.getRequestId(), chainId); throw new ChainNotFoundException(errorMsg); } - + // 执行前置 + chain.executePre(slotIndex); // 执行chain chain.execute(slotIndex); } catch (ChainEndException e) { diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java index 88b4495b5..79835e172 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java @@ -80,11 +80,7 @@ public class Chain implements Executable { throw new FlowSystemException("no conditionList in this chain[" + chainName + "]"); } for (Condition condition : conditionList) { - if (condition instanceof PreCondition){ - for (Executable executableItem : condition.getNodeList()) { - executableItem.execute(slotIndex); - } - } else if (condition instanceof ThenCondition) { + if (condition instanceof ThenCondition) { for (Executable executableItem : condition.getNodeList()) { executableItem.execute(slotIndex); } @@ -94,10 +90,20 @@ public class Chain implements Executable { } } + // 执行pre节点 + public void executePre(Integer slotIndex) throws Exception { + //先把pre的节点过滤出来 + List preConditionList =filterCondition(ConditionTypeEnum.TYPE_PRE); + for (Condition finallyCondition : preConditionList){ + for(Executable executableItem : finallyCondition.getNodeList()){ + executableItem.execute(slotIndex); + } + } + } + public void executeFinally(Integer slotIndex) throws Exception { //先把finally的节点过滤出来 - List finallyConditionList = conditionList.stream().filter(condition -> - condition.getConditionType().equals(ConditionTypeEnum.TYPE_FINALLY)).collect(Collectors.toList()); + List finallyConditionList =filterCondition(ConditionTypeEnum.TYPE_FINALLY); for (Condition finallyCondition : finallyConditionList){ for(Executable executableItem : finallyCondition.getNodeList()){ executableItem.execute(slotIndex); @@ -105,6 +111,13 @@ public class Chain implements Executable { } } + // 根据节点condition类型过去出节点列表 + private List filterCondition(ConditionTypeEnum conditionTypeEnum) { + assert conditionTypeEnum != null; + return conditionList.stream().filter(condition -> + condition.getConditionType().equals(conditionTypeEnum)).collect(Collectors.toList()); + } + @Override public ExecuteTypeEnum getExecuteType() { return ExecuteTypeEnum.CHAIN; From b75244b9e52e5b9f8e9c9bff5b4024629bd247f7 Mon Sep 17 00:00:00 2001 From: daiqi <466608943@qq.com> Date: Fri, 11 Mar 2022 22:05:02 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/entity/flow/Chain.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java index 79835e172..0f8267643 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java @@ -92,20 +92,19 @@ public class Chain implements Executable { // 执行pre节点 public void executePre(Integer slotIndex) throws Exception { - //先把pre的节点过滤出来 - List preConditionList =filterCondition(ConditionTypeEnum.TYPE_PRE); - for (Condition finallyCondition : preConditionList){ - for(Executable executableItem : finallyCondition.getNodeList()){ - executableItem.execute(slotIndex); - } - } + doExecuteForPointConditionType(slotIndex, ConditionTypeEnum.TYPE_PRE); } public void executeFinally(Integer slotIndex) throws Exception { - //先把finally的节点过滤出来 - List finallyConditionList =filterCondition(ConditionTypeEnum.TYPE_FINALLY); - for (Condition finallyCondition : finallyConditionList){ - for(Executable executableItem : finallyCondition.getNodeList()){ + doExecuteForPointConditionType(slotIndex, ConditionTypeEnum.TYPE_FINALLY); + } + + // 执行指定的conditionType的节点 + private void doExecuteForPointConditionType(Integer slotIndex, ConditionTypeEnum conditionTypeEnum) throws Exception { + //先把指定condition类型的节点过滤出来 + List conditions =filterCondition(conditionTypeEnum); + for (Condition condition : conditions){ + for(Executable executableItem : condition.getNodeList()){ executableItem.execute(slotIndex); } }