mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 04:02:09 +08:00
解决组件重试会多次rollback的问题
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
package com.yomahub.liteflow.core;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.StopWatch;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -32,6 +33,7 @@ import com.yomahub.liteflow.monitor.CompStatistics;
|
||||
import com.yomahub.liteflow.monitor.MonitorBus;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Deque;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -156,10 +158,15 @@ public abstract class NodeComponent {
|
||||
|
||||
public void doRollback() throws Exception {
|
||||
Slot slot = this.getSlot();
|
||||
|
||||
Deque<CmpStep> rollbackSteps = slot.getRollbackSteps();
|
||||
if(!CollUtil.isEmpty(rollbackSteps)) {
|
||||
Node refNode = rollbackSteps.peekLast().getRefNode();
|
||||
if(refNode == this.getRefNode()) return;
|
||||
}
|
||||
CmpStep cmpStep = new CmpStep(nodeId, name, CmpStepTypeEnum.SINGLE);
|
||||
cmpStep.setTag(this.getTag());
|
||||
cmpStep.setInstance(this);
|
||||
cmpStep.setRefNode(this.getRefNode());
|
||||
slot.addRollbackStep(cmpStep);
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
|
||||
@@ -100,4 +100,12 @@ public class RollbackELDeclMultiSpringbootTest extends BaseTest {
|
||||
Assertions.assertEquals("321", context.getData("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
// 对重试的测试
|
||||
public void testRetry() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain10", "arg");
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
Assertions.assertEquals("n==>m", response.getRollbackStepStr());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.yomahub.liteflow.test.rollback.cmp;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.annotation.LiteflowMethod;
|
||||
import com.yomahub.liteflow.annotation.LiteflowRetry;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
|
||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||
@@ -145,4 +146,32 @@ public class CmpConfig {
|
||||
System.out.println("XCmp rollback!");
|
||||
}
|
||||
|
||||
private int flag = 0;
|
||||
@LiteflowRetry(5)
|
||||
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "m")
|
||||
public void processM(NodeComponent bindCmp) {
|
||||
if(flag < 2) {
|
||||
flag ++;
|
||||
throw new RuntimeException();
|
||||
}
|
||||
System.out.println("MCmp executed!");
|
||||
}
|
||||
|
||||
@LiteflowMethod(value = LiteFlowMethodEnum.ROLLBACK, nodeId = "m")
|
||||
public void rollbackM() throws Exception {
|
||||
System.out.println("MCmp rollback!");
|
||||
}
|
||||
|
||||
@LiteflowRetry(3)
|
||||
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "n")
|
||||
public void processN(NodeComponent bindCmp) {
|
||||
System.out.println("NCmp executed!");
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@LiteflowMethod(value = LiteFlowMethodEnum.ROLLBACK, nodeId = "n")
|
||||
public void rollbackN() throws Exception {
|
||||
System.out.println("NCmp rollback!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,4 +35,8 @@
|
||||
<chain name="chain9">
|
||||
THEN(a.tag("1"), a.tag("2"), a.tag("3"), d);
|
||||
</chain>
|
||||
|
||||
<chain name="chain10">
|
||||
THEN( m, n );
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -102,4 +102,12 @@ public class RollbackELDeclSpringbootTest extends BaseTest {
|
||||
Assertions.assertEquals("321", context.getData("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
// 对重试的测试
|
||||
public void testRetry() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain10", "arg");
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
Assertions.assertEquals("n==>m", response.getRollbackStepStr());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,4 +35,8 @@
|
||||
<chain name="chain9">
|
||||
THEN(a.tag("1"), a.tag("2"), a.tag("3"), d);
|
||||
</chain>
|
||||
|
||||
<chain name="chain10">
|
||||
THEN( m, n );
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -99,4 +99,12 @@ public class RollbackTest extends BaseTest {
|
||||
Assertions.assertEquals("321", context.getData("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
// 对重试的测试
|
||||
public void testRetry() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain10", "arg");
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
Assertions.assertEquals("n==>m", response.getRollbackStepStr());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
<node id="i" class="com.yomahub.liteflow.test.rollback.cmp.ICmp"/>
|
||||
<node id="w" class="com.yomahub.liteflow.test.rollback.cmp.WCmp"/>
|
||||
<node id="x" class="com.yomahub.liteflow.test.rollback.cmp.XCmp"/>
|
||||
<node id="m" class="com.yomahub.liteflow.test.rollback.cmp.MCmp"/>
|
||||
<node id="n" class="com.yomahub.liteflow.test.rollback.cmp.NCmp"/>
|
||||
</nodes>
|
||||
<chain name="chain1">
|
||||
THEN( a, b, WHEN(c, d).ignoreError(true), CATCH(e) );
|
||||
@@ -48,4 +50,8 @@
|
||||
<chain name="chain9">
|
||||
THEN(a.tag("1"), a.tag("2"), a.tag("3"), d);
|
||||
</chain>
|
||||
|
||||
<chain name="chain10">
|
||||
THEN( m, n );
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -94,4 +94,12 @@ public class RollbackSpringbootTest extends BaseTest {
|
||||
Assertions.assertEquals("321", context.getData("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
// 对重试的测试
|
||||
public void testRetry() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain10", "arg");
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
Assertions.assertEquals("n==>m", response.getRollbackStepStr());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,4 +35,8 @@
|
||||
<chain name="chain9">
|
||||
THEN(a.tag("1"), a.tag("2"), a.tag("3"), d);
|
||||
</chain>
|
||||
|
||||
<chain name="chain10">
|
||||
THEN( m, n );
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -2,6 +2,8 @@ package com.yomahub.liteflow.test.rollback;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.property.LiteflowConfig;
|
||||
import com.yomahub.liteflow.property.LiteflowConfigGetter;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
@@ -97,4 +99,12 @@ public class RollbackSpringbootTest extends BaseTest {
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
Assertions.assertEquals("321", context.getData("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
// 对重试的测试
|
||||
public void testRetry() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain10", "arg");
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
Assertions.assertEquals("n==>m", response.getRollbackStepStr());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.yomahub.liteflow.test.rollback.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowRetry;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("m")
|
||||
@LiteflowRetry(5)
|
||||
public class MCmp extends NodeComponent {
|
||||
|
||||
private int flag = 0;
|
||||
@Override
|
||||
public void process() {
|
||||
if(flag < 2) {
|
||||
flag ++;
|
||||
throw new RuntimeException();
|
||||
}
|
||||
System.out.println("MCmp executed!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback() throws Exception {
|
||||
System.out.println("MCmp rollback!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.yomahub.liteflow.test.rollback.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowRetry;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("n")
|
||||
@LiteflowRetry(3)
|
||||
public class NCmp extends NodeComponent {
|
||||
@Override
|
||||
public void process() {
|
||||
System.out.println("NCmp executed!");
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback() throws Exception {
|
||||
System.out.println("NCmp rollback!");
|
||||
}
|
||||
}
|
||||
@@ -35,4 +35,8 @@
|
||||
<chain name="chain9">
|
||||
THEN(a.tag("1"), a.tag("2"), a.tag("3"), d);
|
||||
</chain>
|
||||
|
||||
<chain name="chain10">
|
||||
THEN( m, n );
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -95,5 +95,13 @@ public class RollbackSpringTest extends BaseTest {
|
||||
Assertions.assertEquals("321", context.getData("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
// 对重试的测试
|
||||
public void testRetry() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain10", "arg");
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
Assertions.assertEquals("n==>m", response.getRollbackStepStr());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.yomahub.liteflow.test.rollback.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowRetry;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("m")
|
||||
@LiteflowRetry(5)
|
||||
public class MCmp extends NodeComponent {
|
||||
|
||||
private int flag = 0;
|
||||
@Override
|
||||
public void process() {
|
||||
if(flag < 2) {
|
||||
flag ++;
|
||||
throw new RuntimeException();
|
||||
}
|
||||
System.out.println("MCmp executed!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback() throws Exception {
|
||||
System.out.println("MCmp rollback!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.yomahub.liteflow.test.rollback.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowRetry;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("n")
|
||||
@LiteflowRetry(3)
|
||||
public class NCmp extends NodeComponent {
|
||||
@Override
|
||||
public void process() {
|
||||
System.out.println("NCmp executed!");
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback() throws Exception {
|
||||
System.out.println("NCmp rollback!");
|
||||
}
|
||||
}
|
||||
@@ -35,4 +35,8 @@
|
||||
<chain name="chain9">
|
||||
THEN(a.tag("1"), a.tag("2"), a.tag("3"), d);
|
||||
</chain>
|
||||
|
||||
<chain name="chain10">
|
||||
THEN( m, n );
|
||||
</chain>
|
||||
</flow>
|
||||
Reference in New Issue
Block a user