浏览代码

bug #I4E2V4 修复异步线程池没有被TTL修饰有可能引起的无法拿到ThreadLocal值的问题

bryan31 3 年之前
父节点
当前提交
0de5508187

+ 2 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java

@@ -10,6 +10,7 @@ package com.yomahub.liteflow.entity.flow;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.StrUtil;
+import com.alibaba.ttl.TtlCallable;
 import com.yomahub.liteflow.entity.data.DataBus;
 import com.yomahub.liteflow.entity.data.Slot;
 import com.yomahub.liteflow.enums.ExecuteTypeEnum;
@@ -130,7 +131,7 @@ public class Chain implements Executable {
 
         for (int i = 0; i < condition.getNodeList().size(); i++) {
             futures.add(parallelExecutor.submit(
-                    new ParallelCallable(condition.getNodeList().get(i), slotIndex, requestId, latch, liteflowConfig.getRetryCount())
+                    TtlCallable.get(new ParallelCallable(condition.getNodeList().get(i), slotIndex, requestId, latch, liteflowConfig.getRetryCount()))
             ));
         }
 

+ 16 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/useTTLInWhen/TestTL.java

@@ -0,0 +1,16 @@
+package com.yomahub.liteflow.test.useTTLInWhen;
+
+import com.alibaba.ttl.TransmittableThreadLocal;
+
+public class TestTL {
+
+    public static ThreadLocal<String> tl = new TransmittableThreadLocal<>();
+
+    public static String get(){
+        return tl.get();
+    }
+
+    public static void set(String value){
+        tl.set(value);
+    }
+}

+ 43 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/useTTLInWhen/UseTTLInWhenSpringbootTest.java

@@ -0,0 +1,43 @@
+package com.yomahub.liteflow.test.useTTLInWhen;
+
+import com.yomahub.liteflow.core.FlowExecutor;
+import com.yomahub.liteflow.entity.data.DefaultSlot;
+import com.yomahub.liteflow.entity.data.LiteflowResponse;
+import com.yomahub.liteflow.test.BaseTest;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.annotation.Resource;
+import java.util.Set;
+
+/**
+ * 在when异步节点的情况下去拿ThreadLocal里的测试场景
+ * @author Bryan.Zhang
+ * @since 2.6.3
+ */
+@RunWith(SpringRunner.class)
+@TestPropertySource(value = "classpath:/useTTLInWhen/application.properties")
+@SpringBootTest(classes = UseTTLInWhenSpringbootTest.class)
+@EnableAutoConfiguration
+@ComponentScan({"com.yomahub.liteflow.test.useTTLInWhen.cmp"})
+public class UseTTLInWhenSpringbootTest extends BaseTest {
+
+    @Resource
+    private FlowExecutor flowExecutor;
+
+    @Test
+    public void testPrivateDelivery() throws Exception{
+        LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1", "arg");
+        Assert.assertEquals("hello,b", response.getSlot().getData("b"));
+        Assert.assertEquals("hello,c", response.getSlot().getData("c"));
+        Assert.assertEquals("hello,d", response.getSlot().getData("d"));
+        Assert.assertEquals("hello,e", response.getSlot().getData("e"));
+        Assert.assertEquals("hello,f", response.getSlot().getData("f"));
+    }
+}

+ 22 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/useTTLInWhen/cmp/ACmp.java

@@ -0,0 +1,22 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.useTTLInWhen.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.test.useTTLInWhen.TestTL;
+import org.springframework.stereotype.Component;
+
+@Component("a")
+public class ACmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		TestTL.set("hello");
+		System.out.println("ACmp executed!");
+	}
+}

+ 24 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/useTTLInWhen/cmp/BCmp.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.useTTLInWhen.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.test.useTTLInWhen.TestTL;
+import org.springframework.stereotype.Component;
+
+@Component("b")
+public class BCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		String value = TestTL.get();
+		this.getSlot().setData(this.getNodeId(),value+",b");
+		System.out.println("BCmp executed!");
+	}
+
+}

+ 24 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/useTTLInWhen/cmp/CCmp.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.useTTLInWhen.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.test.useTTLInWhen.TestTL;
+import org.springframework.stereotype.Component;
+
+@Component("c")
+public class CCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		String value = TestTL.get();
+		this.getSlot().setData(this.getNodeId(),value+",c");
+		System.out.println("CCmp executed!");
+	}
+
+}

+ 24 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/useTTLInWhen/cmp/DCmp.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.useTTLInWhen.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.test.useTTLInWhen.TestTL;
+import org.springframework.stereotype.Component;
+
+@Component("d")
+public class DCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		String value = TestTL.get();
+		this.getSlot().setData(this.getNodeId(),value+",d");
+		System.out.println("DCmp executed!");
+	}
+
+}

+ 24 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/useTTLInWhen/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.useTTLInWhen.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.test.useTTLInWhen.TestTL;
+import org.springframework.stereotype.Component;
+
+@Component("e")
+public class ECmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		String value = TestTL.get();
+		this.getSlot().setData(this.getNodeId(),value+",e");
+		System.out.println("ECmp executed!");
+	}
+
+}

+ 24 - 0
liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/useTTLInWhen/cmp/FCmp.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.useTTLInWhen.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.test.useTTLInWhen.TestTL;
+import org.springframework.stereotype.Component;
+
+@Component("f")
+public class FCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		String value = TestTL.get();
+		this.getSlot().setData(this.getNodeId(),value+",f");
+		System.out.println("FCmp executed!");
+	}
+
+}

+ 2 - 0
liteflow-testcase-springboot/src/test/resources/useTTLInWhen/application.properties

@@ -0,0 +1,2 @@
+liteflow.rule-source=useTTLInWhen/flow.xml
+liteflow.when-max-workers=2

+ 7 - 0
liteflow-testcase-springboot/src/test/resources/useTTLInWhen/flow.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<flow>
+    <chain name="chain1">
+        <then value="a"/>
+        <when value="b,c,d,e,f"/>
+    </chain>
+</flow>