From b248bbd63c580772ce2d821679cf63a82aa088ce Mon Sep 17 00:00:00 2001 From: "everywhere.z" Date: Sun, 12 Jun 2022 17:57:47 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#I4TGGV=20=E5=AD=90=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E4=B8=AD=E7=9A=84finally=E8=8A=82=E7=82=B9=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/core/FlowExecutor.java | 11 -------- .../yomahub/liteflow/flow/element/Chain.java | 27 ++++++++++++++++--- .../PreAndFinallySpringbootTest.java | 7 +++++ .../src/test/resources/preAndFinally/flow.xml | 6 +++++ .../test/preAndFinally/PreAndFinallyTest.java | 7 +++++ .../src/test/resources/preAndFinally/flow.xml | 6 +++++ .../PreAndFinallySpringbootTest.java | 8 ++++++ .../src/test/resources/preAndFinally/flow.xml | 6 +++++ .../PreAndFinallySpringTest.java | 8 ++++++ .../src/test/resources/preAndFinally/flow.xml | 6 +++++ 10 files changed, 77 insertions(+), 15 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 74c5b1554..d716bc8e3 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 @@ -385,8 +385,6 @@ 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) { @@ -401,15 +399,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/flow/element/Chain.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java index 95a66f4ea..eac1474c9 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java @@ -10,6 +10,8 @@ package com.yomahub.liteflow.flow.element; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.exception.ChainEndException; +import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.slot.DataBus; import com.yomahub.liteflow.slot.Slot; import com.yomahub.liteflow.flow.parallel.CompletableFutureTimeout; @@ -73,19 +75,36 @@ public class Chain implements Executable { if (CollUtil.isEmpty(conditionList)) { throw new FlowSystemException("no conditionList in this chain[" + chainName + "]"); } - for (Condition condition : conditionList) { - condition.execute(slotIndex); + try { + //执行前置 + this.executePre(slotIndex); + //执行主体Condition + for (Condition condition : conditionList) { + condition.execute(slotIndex); + } + }catch (ChainEndException e){ + //这里单独catch ChainEndException是因为ChainEndException是用户自己setIsEnd抛出的异常 + //是属于正常逻辑,所以会在FlowExecutor中判断。这里不作为异常处理 + throw e; + }catch (Exception e){ + //这里事先取到exception set到slot里,为了方便finally取到exception + Slot slot = DataBus.getSlot(slotIndex); + slot.setException(e); + throw e; + }finally { + //执行后置 + this.executeFinally(slotIndex); } } // 执行pre节点 - public void executePre(Integer slotIndex) throws Exception { + private void executePre(Integer slotIndex) throws Exception { for (Condition condition : this.preConditionList){ condition.execute(slotIndex); } } - public void executeFinally(Integer slotIndex) throws Exception { + private void executeFinally(Integer slotIndex) throws Exception { for (Condition condition : this.finallyConditionList){ condition.execute(slotIndex); } diff --git a/liteflow-testcase-declare-component/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringbootTest.java b/liteflow-testcase-declare-component/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringbootTest.java index 2fec1619b..35b109f2a 100644 --- a/liteflow-testcase-declare-component/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringbootTest.java +++ b/liteflow-testcase-declare-component/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringbootTest.java @@ -61,4 +61,11 @@ public class PreAndFinallySpringbootTest extends BaseTest { Assert.assertFalse(response.isSuccess()); Assert.assertTrue(response.getContextBean().getData("hasEx")); } + + @Test + public void testPreAndFinally5() throws Exception{ + LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("p1==>p2==>p1==>p2==>a==>b==>c==>f1==>f2==>f1", response.getExecuteStepStrWithoutTime()); + } } diff --git a/liteflow-testcase-declare-component/src/test/resources/preAndFinally/flow.xml b/liteflow-testcase-declare-component/src/test/resources/preAndFinally/flow.xml index 5dac9fb0a..78e263d19 100644 --- a/liteflow-testcase-declare-component/src/test/resources/preAndFinally/flow.xml +++ b/liteflow-testcase-declare-component/src/test/resources/preAndFinally/flow.xml @@ -23,4 +23,10 @@ + + +
+        
+        
+    
 
\ No newline at end of file
diff --git a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallyTest.java b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallyTest.java
index b9db2d65c..8f3794068 100644
--- a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallyTest.java
+++ b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallyTest.java
@@ -57,4 +57,11 @@ public class PreAndFinallyTest extends BaseTest {
         Assert.assertFalse(response.isSuccess());
         Assert.assertTrue(response.getContextBean().getData("hasEx"));
     }
+
+    @Test
+    public void testPreAndFinally5() throws Exception{
+        LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
+        Assert.assertTrue(response.isSuccess());
+        Assert.assertEquals("p1==>p2==>p1==>p2==>a==>b==>c==>f1==>f2==>f1", response.getExecuteStepStrWithoutTime());
+    }
 }
diff --git a/liteflow-testcase-nospring/src/test/resources/preAndFinally/flow.xml b/liteflow-testcase-nospring/src/test/resources/preAndFinally/flow.xml
index f3ba3ba4a..e4ba3fa89 100644
--- a/liteflow-testcase-nospring/src/test/resources/preAndFinally/flow.xml
+++ b/liteflow-testcase-nospring/src/test/resources/preAndFinally/flow.xml
@@ -35,4 +35,10 @@
         
         
     
+
+    
+        
+        
+        
+    
 
\ No newline at end of file
diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringbootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringbootTest.java
index 2fec1619b..579b41dfc 100644
--- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringbootTest.java
+++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringbootTest.java
@@ -61,4 +61,12 @@ public class PreAndFinallySpringbootTest extends BaseTest {
         Assert.assertFalse(response.isSuccess());
         Assert.assertTrue(response.getContextBean().getData("hasEx"));
     }
+
+    //测试嵌套结构pre和finally是否在各自的chain里打出
+    @Test
+    public void testPreAndFinally5() throws Exception{
+        LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
+        Assert.assertTrue(response.isSuccess());
+        Assert.assertEquals("p1==>p2==>p1==>p2==>a==>b==>c==>f1==>f2==>f1", response.getExecuteStepStrWithoutTime());
+    }
 }
diff --git a/liteflow-testcase-springboot/src/test/resources/preAndFinally/flow.xml b/liteflow-testcase-springboot/src/test/resources/preAndFinally/flow.xml
index 5dac9fb0a..78e263d19 100644
--- a/liteflow-testcase-springboot/src/test/resources/preAndFinally/flow.xml
+++ b/liteflow-testcase-springboot/src/test/resources/preAndFinally/flow.xml
@@ -23,4 +23,10 @@
         
         
     
+
+    
+        
+        
+        
+    
 
\ No newline at end of file
diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringTest.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringTest.java
index 662fd7607..a60abf765 100644
--- a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringTest.java
+++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/preAndFinally/PreAndFinallySpringTest.java
@@ -55,4 +55,12 @@ public class PreAndFinallySpringTest extends BaseTest {
         Assert.assertFalse(response.isSuccess());
         Assert.assertTrue(response.getContextBean().getData("hasEx"));
     }
+
+    //测试嵌套结构pre和finally是否在各自的chain里打出
+    @Test
+    public void testPreAndFinally5() throws Exception{
+        LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
+        Assert.assertTrue(response.isSuccess());
+        Assert.assertEquals("p1==>p2==>p1==>p2==>a==>b==>c==>f1==>f2==>f1", response.getExecuteStepStrWithoutTime());
+    }
 }
diff --git a/liteflow-testcase-springnative/src/test/resources/preAndFinally/flow.xml b/liteflow-testcase-springnative/src/test/resources/preAndFinally/flow.xml
index 5dac9fb0a..78e263d19 100644
--- a/liteflow-testcase-springnative/src/test/resources/preAndFinally/flow.xml
+++ b/liteflow-testcase-springnative/src/test/resources/preAndFinally/flow.xml
@@ -23,4 +23,10 @@
         
         
     
+
+    
+        
+        
+        
+    
 
\ No newline at end of file