enhancement #I4TGGV 子流程中的finally节点没有执行

This commit is contained in:
everywhere.z
2022-06-12 17:57:47 +08:00
parent aaaef76142
commit b248bbd63c
10 changed files with 77 additions and 15 deletions

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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<DefaultContext> response = flowExecutor.execute2Resp("chain5", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("p1==>p2==>p1==>p2==>a==>b==>c==>f1==>f2==>f1", response.getExecuteStepStrWithoutTime());
}
}

View File

@@ -23,4 +23,10 @@
<then value="a,d,c"/>
<finally value="f3"/>
</chain>
<chain name="chain5">
<pre value="p1,p2"/>
<then value="chain1"/>
<finally value="f1"/>
</chain>
</flow>

View File

@@ -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<DefaultContext> response = flowExecutor.execute2Resp("chain5", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("p1==>p2==>p1==>p2==>a==>b==>c==>f1==>f2==>f1", response.getExecuteStepStrWithoutTime());
}
}

View File

@@ -35,4 +35,10 @@
<then value="a,d,c"/>
<finally value="f3"/>
</chain>
<chain name="chain5">
<pre value="p1,p2"/>
<then value="chain1"/>
<finally value="f1"/>
</chain>
</flow>

View File

@@ -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<DefaultContext> response = flowExecutor.execute2Resp("chain5", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("p1==>p2==>p1==>p2==>a==>b==>c==>f1==>f2==>f1", response.getExecuteStepStrWithoutTime());
}
}

View File

@@ -23,4 +23,10 @@
<then value="a,d,c"/>
<finally value="f3"/>
</chain>
<chain name="chain5">
<pre value="p1,p2"/>
<then value="chain1"/>
<finally value="f1"/>
</chain>
</flow>

View File

@@ -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<DefaultContext> response = flowExecutor.execute2Resp("chain5", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("p1==>p2==>p1==>p2==>a==>b==>c==>f1==>f2==>f1", response.getExecuteStepStrWithoutTime());
}
}

View File

@@ -23,4 +23,10 @@
<then value="a,d,c"/>
<finally value="f3"/>
</chain>
<chain name="chain5">
<pre value="p1,p2"/>
<then value="chain1"/>
<finally value="f1"/>
</chain>
</flow>