add springboot test

This commit is contained in:
jason
2024-10-22 15:31:47 +08:00
parent a41a4d9a5b
commit 8e4634b0b8
23 changed files with 500 additions and 23 deletions

View File

@@ -140,7 +140,7 @@ public abstract class ParallelStrategyExecutor {
String chainId = DataBus.getSlot(slotIndex).getChainId();
Chain chain = FlowBus.getChain(chainId);
parallelExecutor =
ExecutorHelper.loadInstance().buildWhenExecutorWithHash(whenCondition.getThreadExecutorClass(),
ExecutorHelper.loadInstance().buildChainExecutorWithHash(whenCondition.getThreadExecutorClass(),
String.valueOf(chain.hashCode()));
} else {
parallelExecutor = ExecutorHelper.loadInstance().buildWhenExecutor(whenCondition.getThreadExecutorClass());

View File

@@ -76,6 +76,12 @@ public class LiteflowConfig {
// 异步线程池最大队列数量
private Integer whenQueueLimit;
// chain线程池最大线程数
private Integer chainMaxWorkers;
// chain线程池最大队列数量
private Integer chainQueueLimit;
// 解析模式,一共有三种,具体看其定义
private ParseModeEnum parseMode;
@@ -250,7 +256,6 @@ public class LiteflowConfig {
public void setWhenMaxWorkers(Integer whenMaxWorkers) {
this.whenMaxWorkers = whenMaxWorkers;
}
public Integer getWhenQueueLimit() {
if (ObjectUtil.isNull(whenQueueLimit)) {
return 512;
@@ -326,8 +331,8 @@ public class LiteflowConfig {
}
}
public void setChainThreadExecutorClass(String threadExecutorClass) {
this.threadExecutorClass = threadExecutorClass;
public void setChainThreadExecutorClass(String chainThreadExecutorClass) {
this.chainThreadExecutorClass = chainThreadExecutorClass;
}
public String getNodeExecutorClass() {
@@ -540,4 +545,29 @@ public class LiteflowConfig {
public void setChainThreadPoolIsolate(Boolean chainThreadPoolIsolate) {
this.chainThreadPoolIsolate = chainThreadPoolIsolate;
}
public Integer getChainMaxWorkers() {
if (ObjectUtil.isNull(chainMaxWorkers)) {
return 16;
} else {
return chainMaxWorkers;
}
}
public void setChainMaxWorkers(Integer chainMaxWorkers) {
this.chainMaxWorkers = chainMaxWorkers;
}
public Integer getChainQueueLimit() {
if (ObjectUtil.isNull(chainMaxWorkers)) {
return 512;
} else {
return chainQueueLimit;
}
}
public void setChainQueueLimit(Integer chainQueueLimit) {
this.chainQueueLimit = chainQueueLimit;
}
}

View File

@@ -134,11 +134,11 @@ public class ExecutorHelper {
public ExecutorService buildLoopParallelExecutor(Integer slotIndex) {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
//chain线程池
if (BooleanUtil.isTrue(liteflowConfig.getWhenThreadPoolIsolate())) {
if (BooleanUtil.isTrue(liteflowConfig.getChainThreadPoolIsolate())) {
//获取chain的hash
String chainId = DataBus.getSlot(slotIndex).getChainId();
Chain chain = FlowBus.getChain(chainId);
return getExecutorService(liteflowConfig.getThreadExecutorClass(), String.valueOf(chain.hashCode()));
return getExecutorService(liteflowConfig.getChainThreadExecutorClass(), String.valueOf(chain.hashCode()));
}
return getExecutorService(liteflowConfig.getParallelLoopExecutorClass());
}
@@ -183,26 +183,19 @@ public class ExecutorHelper {
}
}
// 构建when线程池 - clazz和condition的hash值共同作为缓存key
// 构建chain线程池 - clazz和condition的hash值共同作为缓存key
public ExecutorService buildChainExecutorWithHash(String conditionHash) {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
return buildChainExecutorWithHash(liteflowConfig.getThreadExecutorClass(), conditionHash);
return buildChainExecutorWithHash(liteflowConfig.getChainThreadExecutorClass(), conditionHash);
}
// 构建when线程池 - clazz和condition的hash值共同作为缓存key
// 构建chain线程池 - clazz和condition的hash值共同作为缓存key
public ExecutorService buildChainExecutorWithHash(String clazz, String conditionHash) {
if (StrUtil.isBlank(clazz)) {
return buildWhenExecutorWithHash(conditionHash);
return buildChainExecutorWithHash(conditionHash);
}
return getExecutorService(clazz, conditionHash);
}
// 构建when线程池 - 支持多个when公用一个线程池
public ExecutorService buildChainExecutor(String clazz) {
if (StrUtil.isBlank(clazz)) {
return buildWhenExecutor();
}
return getExecutorService(clazz);
}
}

View File

@@ -7,10 +7,7 @@ import com.yomahub.liteflow.property.LiteflowConfigGetter;
import java.util.concurrent.ExecutorService;
/**
* LiteFlow默认的并行多线程执行器实现
*
* @author Bryan.Zhang
* @since 2.6.6
* LiteFlow默认的chain多线程执行器实现
*/
public class LiteFlowDefaultChainExecutorBuilder implements ExecutorBuilder {
@@ -21,8 +18,8 @@ public class LiteFlowDefaultChainExecutorBuilder implements ExecutorBuilder {
if (ObjectUtil.isNull(liteflowConfig)) {
liteflowConfig = new LiteflowConfig();
}
return buildDefaultExecutor(liteflowConfig.getWhenMaxWorkers(), liteflowConfig.getWhenMaxWorkers(),
liteflowConfig.getWhenQueueLimit(), "chain-thread-");
return buildDefaultExecutor(liteflowConfig.getChainMaxWorkers(), liteflowConfig.getChainMaxWorkers(),
liteflowConfig.getChainQueueLimit(), "chain-thread-");
}
}

View File

@@ -105,6 +105,15 @@ public class LiteflowProperty {
// 每一个chain里的when和异步循环合并起来都用单独的线程池。也就是说定义了多少个chain就有多少个线程池
private Boolean chainThreadPoolIsolate;
// chain线程池最大线程数
private int chainMaxWorkers;
// chain线程池最大队列数量
private int chainQueueLimit;
// chain线程执行器class路径
private String chainThreadExecutorClass;
public boolean isEnableMonitorFile() {
return enableMonitorFile;
}
@@ -348,4 +357,29 @@ public class LiteflowProperty {
public boolean isChainThreadPoolIsolate() {
return chainThreadPoolIsolate;
}
public int getChainMaxWorkers() {
return chainMaxWorkers;
}
public void setChainMaxWorkers(int chainMaxWorkers) {
this.chainMaxWorkers = chainMaxWorkers;
}
public int getChainQueueLimit() {
return chainQueueLimit;
}
public void setChainQueueLimit(int chainQueueLimit) {
this.chainQueueLimit = chainQueueLimit;
}
public String getChainThreadExecutorClass() {
return chainThreadExecutorClass;
}
public void setChainThreadExecutorClass(String chainThreadExecutorClass) {
this.chainThreadExecutorClass = chainThreadExecutorClass;
}
}

View File

@@ -55,6 +55,9 @@ public class LiteflowPropertyAutoConfiguration {
liteflowConfig.setPeriod(liteflowMonitorProperty.getPeriod());
liteflowConfig.setScriptSetting(property.getScriptSetting());
liteflowConfig.setChainThreadPoolIsolate(property.isChainThreadPoolIsolate());
liteflowConfig.setChainThreadExecutorClass(property.getChainThreadExecutorClass());
liteflowConfig.setChainMaxWorkers(property.getChainMaxWorkers());
liteflowConfig.setChainQueueLimit(property.getChainQueueLimit());
return liteflowConfig;
}

View File

@@ -9,6 +9,8 @@ liteflow.when-max-wait-time=15000
liteflow.when-max-wait-time-unit=MILLISECONDS
liteflow.when-max-workers=16
liteflow.when-queue-limit=512
liteflow.chain-max-workers=16
liteflow.chain-queue-limit=512
liteflow.when-thread-pool-isolate=false
liteflow.chain-thread-pool-isolate=false
liteflow.parse-mode=PARSE_ALL_ON_START

View File

@@ -0,0 +1,92 @@
package com.yomahub.liteflow.test.chainThreadPool;
import cn.hutool.core.collection.ListUtil;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.slot.DefaultContext;
import com.yomahub.liteflow.test.BaseTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.annotation.Resource;
import java.util.List;
/**
* springboot环境下chain线程池隔离测试
*/
@TestPropertySource(value = "classpath:/chainThreadPool/application2.properties")
@SpringBootTest(classes = CustomChainThreadPoolELSpringbootTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.chainThreadPool.cmp"})
public class CustomChainThreadPoolELSpringbootTest extends BaseTest {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Resource
private FlowExecutor flowExecutor;
/**
* 测试chain自定义线程池隔离
*/
@Test
public void testCustomChainThreadPool1() {
LiteflowResponse response = flowExecutor.execute2Resp("chain", "arg");
DefaultContext context = response.getFirstContextBean();
Assertions.assertTrue(response.isSuccess());
Assertions.assertTrue(context.getData("threadNameFor").toString().startsWith("customer-chain-thead-1"));
Assertions.assertTrue(context.getData("threadName").toString().startsWith("customer-chain-thead-1"));
}
/**
* 测试when上自定义线程池和chain线程池隔离-优先以when上为准
*/
@Test
public void testCustomChainThreadPool2() {
LiteflowResponse response1 = flowExecutor.execute2Resp("chain2", "arg");
DefaultContext context = response1.getFirstContextBean();
Assertions.assertTrue(response1.isSuccess());
Assertions.assertTrue(context.getData("threadName").toString().startsWith("customer-chain-thead-2"));
}
/**
* 测试并行FOR循环全局线程池和chain线程池隔离-优先以chain线程池上为准
*/
@Test
public void testCustomChainThreadPool3() {
LiteflowResponse response1 = flowExecutor.execute2Resp("chain3", "arg");
DefaultContext context = response1.getFirstContextBean();
Assertions.assertTrue(response1.isSuccess());
Assertions.assertTrue(context.getData("threadNameFor").toString().startsWith("customer-chain-thead-1"));
}
/**
* 测试并行条件循环全局线程池和chain线程池隔离-优先以chain线程池上为准
*/
@Test
public void testCustomChainThreadPool4() {
LiteflowResponse response1 = flowExecutor.execute2Resp("chain4", "arg");
DefaultContext context = response1.getFirstContextBean();
Assertions.assertTrue(response1.isSuccess());
Assertions.assertTrue(context.getData("threadNameWhile").toString().startsWith("customer-chain-thead-1"));
}
/**
* 测试并行迭代循环全局线程池和chain线程池隔离-优先以chain线程池上为准
*/
@Test
public void testCustomChainThreadPool5() {
List<String> list = ListUtil.toList("1", "2", "3", "4", "5");
LiteflowResponse response1 = flowExecutor.execute2Resp("chain5", list);
DefaultContext context = response1.getFirstContextBean();
Assertions.assertTrue(response1.isSuccess());
Assertions.assertTrue(context.getData("threadNameIterator").toString().startsWith("customer-chain-thead-1"));
}
}

View File

@@ -0,0 +1,23 @@
package com.yomahub.liteflow.test.chainThreadPool;
import cn.hutool.core.util.ObjectUtil;
import com.yomahub.liteflow.property.LiteflowConfig;
import com.yomahub.liteflow.property.LiteflowConfigGetter;
import com.yomahub.liteflow.thread.ExecutorBuilder;
import java.util.concurrent.ExecutorService;
public class CustomThreadExecutor1 implements ExecutorBuilder {
@Override
public ExecutorService buildExecutor() {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
// 只有在非spring的场景下liteflowConfig才会为null
if (ObjectUtil.isNull(liteflowConfig)) {
liteflowConfig = new LiteflowConfig();
}
return buildDefaultExecutor(liteflowConfig.getChainMaxWorkers(), liteflowConfig.getChainMaxWorkers(),
liteflowConfig.getChainQueueLimit(), "customer-chain-thead-1");
}
}

View File

@@ -0,0 +1,23 @@
package com.yomahub.liteflow.test.chainThreadPool;
import cn.hutool.core.util.ObjectUtil;
import com.yomahub.liteflow.property.LiteflowConfig;
import com.yomahub.liteflow.property.LiteflowConfigGetter;
import com.yomahub.liteflow.thread.ExecutorBuilder;
import java.util.concurrent.ExecutorService;
public class CustomThreadExecutor2 implements ExecutorBuilder {
@Override
public ExecutorService buildExecutor() {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
// 只有在非spring的场景下liteflowConfig才会为null
if (ObjectUtil.isNull(liteflowConfig)) {
liteflowConfig = new LiteflowConfig();
}
return buildDefaultExecutor(liteflowConfig.getChainMaxWorkers(), liteflowConfig.getChainMaxWorkers(),
liteflowConfig.getChainQueueLimit(), "customer-chain-thead-2");
}
}

View File

@@ -0,0 +1,45 @@
package com.yomahub.liteflow.test.chainThreadPool;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.slot.DefaultContext;
import com.yomahub.liteflow.test.BaseTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.annotation.Resource;
/**
* springboot环境下chain线程池隔离测试
*/
@TestPropertySource(value = "classpath:/chainThreadPool/application.properties")
@SpringBootTest(classes = DefaultChainThreadPoolELSpringbootTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.chainThreadPool.cmp"})
public class DefaultChainThreadPoolELSpringbootTest extends BaseTest {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Resource
private FlowExecutor flowExecutor;
/**
* 测试chain默认线程池隔离
*/
@Test
public void testDefaultChainThreadPool() {
LiteflowResponse response = flowExecutor.execute2Resp("chain", "arg");
DefaultContext context = response.getFirstContextBean();
Assertions.assertTrue(response.isSuccess());
Assertions.assertTrue(context.getData("threadNameFor").toString().startsWith("chain-thread-"));
Assertions.assertTrue(context.getData("threadName").toString().startsWith("chain-thread-"));
}
}

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.chainThreadPool.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
System.out.println("ACmp executed!");
}
}

View File

@@ -0,0 +1,25 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
*
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.chainThreadPool.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
DefaultContext context = this.getFirstContextBean();
context.setData("threadName", Thread.currentThread().getName());
System.out.println("BCmp executed!");
}
}

View File

@@ -0,0 +1,30 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
*
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.chainThreadPool.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("d")
public class DCmp extends NodeComponent {
@Override
public void process() {
DefaultContext context = this.getFirstContextBean();
String key = "test";
if (context.hasData(key)) {
int count = context.getData(key);
context.setData(key, ++count);
} else {
context.setData(key, 1);
}
}
}

View File

@@ -0,0 +1,25 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
*
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.chainThreadPool.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("f")
public class FCmp extends NodeComponent {
@Override
public void process() {
DefaultContext context = this.getFirstContextBean();
context.setData("threadNameFor", Thread.currentThread().getName());
System.out.println("FCmp executed!");
}
}

View File

@@ -0,0 +1,25 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
*
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.chainThreadPool.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("i")
public class ICmp extends NodeComponent {
@Override
public void process() {
DefaultContext context = this.getFirstContextBean();
context.setData("threadNameIterator", Thread.currentThread().getName());
System.out.println("ICmp executed!");
}
}

View File

@@ -0,0 +1,17 @@
package com.yomahub.liteflow.test.chainThreadPool.cmp;
import com.yomahub.liteflow.core.NodeIteratorComponent;
import org.springframework.stereotype.Component;
import java.util.Iterator;
import java.util.List;
@Component("it")
public class ITCmp extends NodeIteratorComponent {
@Override
public Iterator<?> processIterator() throws Exception {
List<String> list = this.getRequestData();
return list.iterator();
}
}

View File

@@ -0,0 +1,25 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
*
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.chainThreadPool.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("w")
public class WCmp extends NodeComponent {
@Override
public void process() {
DefaultContext context = this.getFirstContextBean();
context.setData("threadNameWhile", Thread.currentThread().getName());
System.out.println("WCmp executed!");
}
}

View File

@@ -0,0 +1,21 @@
package com.yomahub.liteflow.test.chainThreadPool.cmp;
import com.yomahub.liteflow.core.NodeBooleanComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("z")
public class ZCmp extends NodeBooleanComponent {
@Override
public boolean processBoolean() throws Exception {
DefaultContext context = this.getFirstContextBean();
String key = "test";
if (context.hasData(key)) {
int count = context.getData("test");
return count < 5;
} else {
return true;
}
}
}

View File

@@ -0,0 +1,4 @@
liteflow.rule-source=chainThreadPool/flow.el.xml
liteflow.chain-thread-pool-isolate=true
liteflow.chain-max-workers=10
liteflow.chain-queue-limit=1024

View File

@@ -0,0 +1,5 @@
liteflow.rule-source=chainThreadPool/flow2.el.xml
liteflow.chain-thread-pool-isolate=true
liteflow.chain-max-workers=10
liteflow.chain-queue-limit=1024
liteflow.chain-thread-executor-class=com.yomahub.liteflow.test.chainThreadPool.CustomThreadExecutor1

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain">
FOR(5).parallel(true).DO(THEN(f,WHEN(
THEN(a,b)
))
);
</chain>
</flow>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain">
FOR(5).parallel(true).DO(THEN(f,WHEN(
THEN(a,b)
))
);
</chain>
<chain name="chain2">
WHEN(a, b).threadPool("com.yomahub.liteflow.test.chainThreadPool.CustomThreadExecutor2");
</chain>
<chain name="chain3">
FOR(5).parallel(true).DO(THEN(a,f
)
);
</chain>
<chain name="chain4">
WHILE(z).parallel(true).DO(THEN(w,d));
</chain>
<chain name="chain5">
ITERATOR(it).parallel(true).DO(THEN(a,i));
</chain>
</flow>