diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java b/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java index 9b029f868..ed830c959 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java @@ -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 { diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/RetryCount.java b/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowRetry.java similarity index 83% rename from liteflow-core/src/main/java/com/yomahub/liteflow/annotation/RetryCount.java rename to liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowRetry.java index e6c86e502..0c9c182aa 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/RetryCount.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowRetry.java @@ -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[] forExceptions() default {Exception.class}; diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/ComponentInitializer.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/ComponentInitializer.java index 7baaebe56..02cee03a5 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/ComponentInitializer.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/ComponentInitializer.java @@ -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()); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java b/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java index 8f05cf8f7..4ed751143 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java @@ -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; diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/LiteflowRetrySpringbootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/LiteflowRetrySpringbootTest.java index b4dcb00f0..7d09ac83d 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/LiteflowRetrySpringbootTest.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/LiteflowRetrySpringbootTest.java @@ -33,9 +33,29 @@ public class LiteflowRetrySpringbootTest extends BaseTest { private FlowExecutor flowExecutor; @Test - public void testRetry() { + public void testRetry1() { LiteflowResponse 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 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 response = flowExecutor.execute2Resp("chain3", "arg"); + Assert.assertFalse(response.isSuccess()); + } + + @Test + public void testRetry4() { + LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg"); + Assert.assertFalse(response.isSuccess()); + Assert.assertEquals("e==>e==>e==>e==>e==>e", response.getSlot().printStep()); } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/CCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/CCmp.java index ee1f3d304..ae75a7570 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/CCmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/CCmp.java @@ -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"); } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/DCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/DCmp.java index 7676f0456..b9232e5ae 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/DCmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/DCmp.java @@ -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"); } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/ECmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/ECmp.java new file mode 100644 index 000000000..ba3f92d65 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/ECmp.java @@ -0,0 +1,24 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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"); + } + +} diff --git a/liteflow-testcase-springboot/src/test/resources/cmpRetry/flow.xml b/liteflow-testcase-springboot/src/test/resources/cmpRetry/flow.xml index 35a4c42a8..d44e3ee05 100644 --- a/liteflow-testcase-springboot/src/test/resources/cmpRetry/flow.xml +++ b/liteflow-testcase-springboot/src/test/resources/cmpRetry/flow.xml @@ -1,10 +1,18 @@ - + - + + + + + + + + + \ No newline at end of file