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

This commit is contained in:
bryan31
2021-10-15 17:52:07 +08:00
parent dbcf07f21a
commit 0de5508187
11 changed files with 212 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

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.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!");
}
}

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.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!");
}
}

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.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!");
}
}

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.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!");
}
}

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.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!");
}
}

View File

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

View File

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