Просмотр исходного кода

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

bryan31 3 лет назад
Родитель
Сommit
33b4f7258d

+ 1 - 1
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 {
 

+ 4 - 3
liteflow-core/src/main/java/com/yomahub/liteflow/annotation/RetryCount.java → 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<? extends Exception>[] forExceptions() default {Exception.class};

+ 6 - 5
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());

+ 0 - 8
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;

+ 22 - 2
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<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());
     }
 }

+ 4 - 0
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");
 	}
 
 }

+ 3 - 0
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");
 	}
 
 }

+ 24 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/cmpRetry/cmp/ECmp.java

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

+ 10 - 2
liteflow-testcase-springboot/src/test/resources/cmpRetry/flow.xml

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