feature enhancement #I49FDK 中断重试目前是全局的,希望增加针对个别组件和特定exception

This commit is contained in:
bryan31
2021-09-23 01:20:49 +08:00
parent 23f5cfb866
commit 33b4f7258d
9 changed files with 74 additions and 21 deletions

View File

@@ -1,6 +1,5 @@
package com.yomahub.liteflow.annotation;
import cn.hutool.core.annotation.Alias;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
@@ -15,6 +14,7 @@ import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Component
public @interface LiteflowComponent {

View File

@@ -14,12 +14,13 @@ import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RetryCount {
@Inherited
public @interface LiteflowRetry {
@AliasFor(value = "retry")
@AliasFor("retry")
int value() default 0;
@AliasFor(value = "value")
@AliasFor("value")
int retry() default 0;
Class<? extends Exception>[] forExceptions() default {Exception.class};

View File

@@ -3,10 +3,11 @@ package com.yomahub.liteflow.core;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.annotation.RetryCount;
import com.yomahub.liteflow.annotation.LiteflowRetry;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.property.LiteflowConfig;
import com.yomahub.liteflow.property.LiteflowConfigGetter;
import org.springframework.core.annotation.AnnotationUtils;
/**
* 组件初始化器
@@ -45,10 +46,10 @@ public class ComponentInitializer {
//先从组件上取@RetryCount标注如果没有则看全局配置全局配置如果不配置的话默认是0
//默认retryForExceptions为Exception.class
RetryCount retryCountAnnotation = nodeComponent.getClass().getAnnotation(RetryCount.class);
if (ObjectUtil.isNotNull(retryCountAnnotation)) {
nodeComponent.setRetryCount(retryCountAnnotation.retry());
nodeComponent.setRetryForExceptions(retryCountAnnotation.forExceptions());
LiteflowRetry liteflowRetryAnnotation = AnnotationUtils.getAnnotation(nodeComponent.getClass(), LiteflowRetry.class);
if (ObjectUtil.isNotNull(liteflowRetryAnnotation)) {
nodeComponent.setRetryCount(liteflowRetryAnnotation.retry());
nodeComponent.setRetryForExceptions(liteflowRetryAnnotation.forExceptions());
} else {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
nodeComponent.setRetryCount(liteflowConfig.getRetryCount());

View File

@@ -8,16 +8,8 @@
*/
package com.yomahub.liteflow.spring;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.annotation.RetryCount;
import com.yomahub.liteflow.aop.ICmpAroundAspect;
import com.yomahub.liteflow.core.ComponentInitializer;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.property.LiteflowConfig;
import com.yomahub.liteflow.property.LiteflowConfigGetter;
import com.yomahub.liteflow.util.LOGOPrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@@ -33,9 +33,29 @@ public class LiteflowRetrySpringbootTest extends BaseTest {
private FlowExecutor flowExecutor;
@Test
public void testRetry() {
public void testRetry1() {
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("a==>b==>b==>b==>c==>a==>d", response.getSlot().printStep());
Assert.assertEquals("a==>b==>b==>b", response.getSlot().printStep());
}
@Test
public void testRetry2() {
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain2", "arg");
Assert.assertFalse(response.isSuccess());
Assert.assertEquals("c==>c==>c==>c==>c==>c", response.getSlot().printStep());
}
@Test
public void testRetry3() {
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain3", "arg");
Assert.assertFalse(response.isSuccess());
}
@Test
public void testRetry4() {
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain4", "arg");
Assert.assertFalse(response.isSuccess());
Assert.assertEquals("e==>e==>e==>e==>e==>e", response.getSlot().printStep());
}
}

View File

@@ -8,14 +8,18 @@
package com.yomahub.liteflow.test.cmpRetry.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.annotation.LiteflowRetry;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("c")
@LiteflowRetry(5)
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
throw new RuntimeException("demo exception");
}
}

View File

@@ -8,14 +8,17 @@
package com.yomahub.liteflow.test.cmpRetry.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.annotation.LiteflowRetry;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("d")
@LiteflowRetry(retry = 5, forExceptions = {NullPointerException.class})
public class DCmp extends NodeComponent {
@Override
public void process() {
System.out.println("DCmp executed!");
throw new RuntimeException("demo exception");
}
}

View File

@@ -0,0 +1,24 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.cmpRetry.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.annotation.LiteflowRetry;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("e")
@LiteflowRetry(retry = 5, forExceptions = {NullPointerException.class})
public class ECmp extends NodeComponent {
@Override
public void process() {
System.out.println("ECmp executed!");
throw new NullPointerException("demo null exception");
}
}

View File

@@ -1,10 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
<then value="a,b,chain2"/>
<then value="a,b"/>
</chain>
<chain name="chain2">
<then value="c,a,d"/>
<then value="c"/>
</chain>
<chain name="chain3">
<then value="d"/>
</chain>
<chain name="chain4">
<then value="e"/>
</chain>
</flow>