From b980e1ce09000b8040ef2f91eb0dbd6494578442 Mon Sep 17 00:00:00 2001 From: zendwang Date: Mon, 29 Mar 2021 12:54:41 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#I3DMX4=20FlowExecutor=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E4=B8=9A=E5=8A=A1=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/core/FlowExecutor.java | 46 +++++++-------- .../yomahub/liteflow/entity/data/AbsSlot.java | 44 ++++---------- .../entity/data/LiteflowResponse.java | 58 +++++++++++++++++++ .../yomahub/liteflow/entity/data/Slot.java | 8 --- .../java/com/yomahub/flowtest/Runner.java | 6 +- .../java/com/yomahub/flowtest/TestFlow.java | 8 +-- 6 files changed, 99 insertions(+), 71 deletions(-) create mode 100644 liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/LiteflowResponse.java 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 2a88da0b5..84b20153b 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 @@ -7,20 +7,15 @@ */ package com.yomahub.liteflow.core; -import java.text.MessageFormat; -import java.util.List; - 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 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.entity.flow.Chain; import com.yomahub.liteflow.exception.ChainNotFoundException; import com.yomahub.liteflow.exception.ConfigErrorException; import com.yomahub.liteflow.exception.FlowExecutorNotInitException; @@ -30,8 +25,11 @@ import com.yomahub.liteflow.parser.LocalXmlFlowParser; import com.yomahub.liteflow.parser.XmlFlowParser; import com.yomahub.liteflow.parser.ZookeeperXmlFlowParser; import com.yomahub.liteflow.property.LiteflowConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.util.concurrent.ExecutorService; +import java.text.MessageFormat; +import java.util.List; /** * 流程规则主要执行器类 @@ -98,23 +96,24 @@ public class FlowExecutor { public void reloadRule(){ init(); } - - public T execute(String chainId,Object param) throws Exception{ - return execute(chainId, param, DefaultSlot.class,null,false); + + public void invoke(String chainId, Object param, Class slotClazz, Integer slotIndex) throws Exception { + execute(chainId, param, slotClazz, slotIndex,true); + } + + public LiteflowResponse execute(String chainId, Object param) throws Exception { + return execute(chainId, param, DefaultSlot.class, null, false); } - public T execute(String chainId,Object param,Class slotClazz) throws Exception{ + public LiteflowResponse execute(String chainId, Object param, Class slotClazz) throws Exception { return execute(chainId, param, slotClazz,null,false); } - - public void invoke(String chainId,Object param,Class slotClazz,Integer slotIndex) throws Exception{ - execute(chainId, param, slotClazz,slotIndex,true); - } - - public T execute(String chainId,Object param,Class slotClazz,Integer slotIndex,boolean isInnerChain) throws Exception{ + + public LiteflowResponse execute(String chainId, Object param, Class slotClazz, Integer slotIndex, + boolean isInnerChain) throws Exception { Slot slot = null; - if(FlowBus.needInit()) { + if (FlowBus.needInit()) { init(); } @@ -150,21 +149,22 @@ public class FlowExecutor { } else { slot.setChainReqData(chainId, param); } - + LiteflowResponse response = new LiteflowResponse<>(slot); try { // 执行chain chain.execute(slotIndex); } catch (Exception e) { + response.setSuccess(false); + response.setMessage(e.getMessage()); + response.setCause(e.getCause()); LOG.error("[{}]:chain[{}] execute error on slot[{}]", slot.getRequestId(), chain.getChainName(), slotIndex); - slot.setSuccess(false); - slot.setErrorMsg(e.getMessage()); } finally { if (!isInnerChain) { slot.printStep(); DataBus.releaseSlot(slotIndex); } } - return (T)slot; + return response; } public String getZkNode() { diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java index 52bff7d0b..9d07a6e0a 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java @@ -7,42 +7,38 @@ */ package com.yomahub.liteflow.entity.data; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayDeque; import java.util.Deque; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Slot的抽象类实现 * @author Bryan.Zhang */ @SuppressWarnings("unchecked") -public abstract class AbsSlot implements Slot{ +public abstract class AbsSlot implements Slot { private static final Logger LOG = LoggerFactory.getLogger(Slot.class); - private final String REQUEST = "request"; + private static final String REQUEST = "request"; - private final String RESPONSE = "response"; + private static final String RESPONSE = "response"; - private final String CHAINNAME = "chain_name"; + private static final String CHAINNAME = "chain_name"; - private final String COND_NODE_PREFIX = "cond_"; + private static final String COND_NODE_PREFIX = "cond_"; - private final String NODE_INPUT_PREFIX = "input_"; + private static final String NODE_INPUT_PREFIX = "input_"; - private final String NODE_OUTPUT_PREFIX = "output_"; + private static final String NODE_OUTPUT_PREFIX = "output_"; - private final String CHAIN_REQ_PREFIX = "chain_req_"; + private static final String CHAIN_REQ_PREFIX = "chain_req_"; - private final String REQUEST_ID = "req_id"; - - private boolean isSuccess = true; - - private String errorMsg; + private static final String REQUEST_ID = "req_id"; private Deque executeSteps = new ArrayDeque(); @@ -142,20 +138,4 @@ public abstract class AbsSlot implements Slot{ public Deque getExecuteSteps() { return executeSteps; } - - public boolean isSuccess() { - return isSuccess; - } - - public void setSuccess(boolean success) { - isSuccess = success; - } - - public String getErrorMsg() { - return errorMsg; - } - - public void setErrorMsg(String errorMsg) { - this.errorMsg = errorMsg; - } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/LiteflowResponse.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/LiteflowResponse.java new file mode 100644 index 000000000..4d46a0d40 --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/LiteflowResponse.java @@ -0,0 +1,58 @@ +package com.yomahub.liteflow.entity.data; + +import java.io.Serializable; + +/** + * 执行结果封装类 + * @author zend.wang + */ +public class LiteflowResponse implements Serializable { + + private static final long serialVersionUID = -2792556188993845048L; + + private boolean success; + + private String message; + + private Throwable cause; + + private T data; + + public LiteflowResponse(T data) { + this.success = true; + this.message = ""; + this.data = data; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(final boolean success) { + this.success = success; + } + + public String getMessage() { + return message; + } + + public void setMessage(final String message) { + this.message = message; + } + + public Throwable getCause() { + return cause; + } + + public void setCause(final Throwable cause) { + this.cause = cause; + } + + public T getData() { + return data; + } + + public void setData(final T data) { + this.data = data; + } +} diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/Slot.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/Slot.java index 4ba30a028..ab917ecf7 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/Slot.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/Slot.java @@ -51,12 +51,4 @@ public interface Slot { public void setChainName(String chainName); public String getChainName(); - - public boolean isSuccess(); - - public void setSuccess(boolean success); - - public String getErrorMsg(); - - public void setErrorMsg(String errorMsg); } diff --git a/liteflow-test-spring/src/main/java/com/yomahub/flowtest/Runner.java b/liteflow-test-spring/src/main/java/com/yomahub/flowtest/Runner.java index 31becd387..c465152d9 100644 --- a/liteflow-test-spring/src/main/java/com/yomahub/flowtest/Runner.java +++ b/liteflow-test-spring/src/main/java/com/yomahub/flowtest/Runner.java @@ -1,7 +1,7 @@ package com.yomahub.flowtest; import com.yomahub.liteflow.core.FlowExecutor; -import com.yomahub.liteflow.entity.data.Slot; +import com.yomahub.liteflow.entity.data.LiteflowResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -16,8 +16,8 @@ public class Runner { context.start(); log.info("启动成功"); FlowExecutor flowExecutor = context.getBean(FlowExecutor.class); - Slot slot = flowExecutor.execute("chain3", "it's a request"); - System.out.println(slot); + LiteflowResponse response = flowExecutor.execute("chain3", "it's a request"); + System.out.println(response); while (true){ Thread.sleep(60000); diff --git a/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/TestFlow.java b/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/TestFlow.java index ba6286bf4..5e072cd98 100644 --- a/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/TestFlow.java +++ b/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/TestFlow.java @@ -1,9 +1,7 @@ package com.yomahub.flowtest; import com.yomahub.liteflow.core.FlowExecutor; -import com.yomahub.liteflow.entity.data.Slot; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.yomahub.liteflow.entity.data.LiteflowResponse; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @@ -17,7 +15,7 @@ public class TestFlow implements CommandLineRunner { @Override public void run(String... args) throws Exception { - Slot slot = flowExecutor.execute("chain2", "it's a request"); - System.out.println(slot); + LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); + System.out.println(response); } }