mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 12:12:08 +08:00
Merge branch 'v2.6.8-when-threadpool' of https://gitee.com/sikadai/liteFlow into v2.6.9-when-threadpool
This commit is contained in:
@@ -48,4 +48,13 @@ public class LiteFlowWhenConditionBuilder extends LiteFlowConditionBuilder{
|
|||||||
}
|
}
|
||||||
return setAny(Boolean.parseBoolean(any));
|
return setAny(Boolean.parseBoolean(any));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public LiteFlowWhenConditionBuilder setThreadExecutorClass(String executorServiceName){
|
||||||
|
if (StrUtil.isBlank(executorServiceName)) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
this.condition.setThreadExecutorClass(executorServiceName);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ package com.yomahub.liteflow.entity.flow;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.alibaba.ttl.threadpool.TtlExecutors;
|
|
||||||
import com.yomahub.liteflow.entity.data.DataBus;
|
import com.yomahub.liteflow.entity.data.DataBus;
|
||||||
import com.yomahub.liteflow.entity.data.Slot;
|
import com.yomahub.liteflow.entity.data.Slot;
|
||||||
import com.yomahub.liteflow.entity.flow.parallel.CompletableFutureTimeout;
|
import com.yomahub.liteflow.entity.flow.parallel.CompletableFutureTimeout;
|
||||||
@@ -120,7 +119,7 @@ public class Chain implements Executable {
|
|||||||
Slot slot = DataBus.getSlot(slotIndex);
|
Slot slot = DataBus.getSlot(slotIndex);
|
||||||
|
|
||||||
//此方法其实只会初始化一次Executor,不会每次都会初始化。Executor是唯一的
|
//此方法其实只会初始化一次Executor,不会每次都会初始化。Executor是唯一的
|
||||||
ExecutorService parallelExecutor = ExecutorHelper.loadInstance().buildExecutor();
|
ExecutorService parallelExecutor = ExecutorHelper.loadInstance().buildExecutor(condition.getThreadExecutorClass());
|
||||||
|
|
||||||
//获得liteflow的参数
|
//获得liteflow的参数
|
||||||
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ public class Condition {
|
|||||||
|
|
||||||
//只在when类型下有效,为true的话说明在多个并行节点下,任意一个成功,整个when就成功
|
//只在when类型下有效,为true的话说明在多个并行节点下,任意一个成功,整个when就成功
|
||||||
private boolean any = false;
|
private boolean any = false;
|
||||||
|
// when单独的线程池名称
|
||||||
|
private String threadExecutorClass;
|
||||||
|
|
||||||
public Condition(List<Executable> nodeList) {
|
public Condition(List<Executable> nodeList) {
|
||||||
this.nodeList = nodeList;
|
this.nodeList = nodeList;
|
||||||
@@ -77,4 +79,12 @@ public class Condition {
|
|||||||
public void setAny(boolean any) {
|
public void setAny(boolean any) {
|
||||||
this.any = any;
|
this.any = any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getThreadExecutorClass() {
|
||||||
|
return threadExecutorClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setThreadExecutorClass(String threadExecutorClass) {
|
||||||
|
this.threadExecutorClass = threadExecutorClass;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public class WhenCondition extends Condition{
|
|||||||
super.setGroup(condition.getGroup());
|
super.setGroup(condition.getGroup());
|
||||||
super.setErrorResume(condition.isErrorResume());
|
super.setErrorResume(condition.isErrorResume());
|
||||||
super.setAny(condition.isAny());
|
super.setAny(condition.isAny());
|
||||||
|
super.setThreadExecutorClass(condition.getThreadExecutorClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package com.yomahub.liteflow.parser;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
@@ -11,23 +10,18 @@ import com.alibaba.fastjson.parser.Feature;
|
|||||||
import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
|
import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
|
||||||
import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
|
import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
|
||||||
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
|
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
|
||||||
import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
|
|
||||||
import com.yomahub.liteflow.core.NodeComponent;
|
import com.yomahub.liteflow.core.NodeComponent;
|
||||||
import com.yomahub.liteflow.entity.flow.*;
|
|
||||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||||
import com.yomahub.liteflow.exception.EmptyConditionValueException;
|
import com.yomahub.liteflow.exception.EmptyConditionValueException;
|
||||||
import com.yomahub.liteflow.exception.ExecutableItemNotFoundException;
|
|
||||||
import com.yomahub.liteflow.exception.NodeTypeNotSupportException;
|
import com.yomahub.liteflow.exception.NodeTypeNotSupportException;
|
||||||
import com.yomahub.liteflow.exception.NotSupportConditionException;
|
import com.yomahub.liteflow.exception.NotSupportConditionException;
|
||||||
import com.yomahub.liteflow.flow.FlowBus;
|
import com.yomahub.liteflow.flow.FlowBus;
|
||||||
import com.yomahub.liteflow.spring.ComponentScanner;
|
import com.yomahub.liteflow.spring.ComponentScanner;
|
||||||
import org.dom4j.Element;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Json格式解析器
|
* Json格式解析器
|
||||||
@@ -136,6 +130,7 @@ public abstract class JsonFlowParser extends FlowParser {
|
|||||||
String group;
|
String group;
|
||||||
String errorResume;
|
String errorResume;
|
||||||
String any;
|
String any;
|
||||||
|
String threadExecutorClass;
|
||||||
|
|
||||||
//构建chainBuilder
|
//构建chainBuilder
|
||||||
String chainName = chainObject.getString("name");
|
String chainName = chainObject.getString("name");
|
||||||
@@ -148,6 +143,7 @@ public abstract class JsonFlowParser extends FlowParser {
|
|||||||
errorResume = condObject.getString("errorResume");
|
errorResume = condObject.getString("errorResume");
|
||||||
group = condObject.getString("group");
|
group = condObject.getString("group");
|
||||||
any = condObject.getString("any");
|
any = condObject.getString("any");
|
||||||
|
threadExecutorClass = condObject.getString("threadExecutorClass");
|
||||||
|
|
||||||
if (ObjectUtil.isNull(conditionType)){
|
if (ObjectUtil.isNull(conditionType)){
|
||||||
throw new NotSupportConditionException("ConditionType is not supported");
|
throw new NotSupportConditionException("ConditionType is not supported");
|
||||||
@@ -165,6 +161,7 @@ public abstract class JsonFlowParser extends FlowParser {
|
|||||||
.setErrorResume(errorResume)
|
.setErrorResume(errorResume)
|
||||||
.setGroup(group)
|
.setGroup(group)
|
||||||
.setAny(any)
|
.setAny(any)
|
||||||
|
.setThreadExecutorClass(threadExecutorClass)
|
||||||
.setValue(condValueStr)
|
.setValue(condValueStr)
|
||||||
.build()
|
.build()
|
||||||
).build();
|
).build();
|
||||||
|
|||||||
@@ -2,18 +2,12 @@ package com.yomahub.liteflow.parser;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
|
import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
|
||||||
import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
|
import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
|
||||||
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
|
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
|
||||||
import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
|
|
||||||
import com.yomahub.liteflow.core.NodeComponent;
|
import com.yomahub.liteflow.core.NodeComponent;
|
||||||
import com.yomahub.liteflow.entity.flow.Chain;
|
|
||||||
import com.yomahub.liteflow.entity.flow.Condition;
|
|
||||||
import com.yomahub.liteflow.entity.flow.Executable;
|
|
||||||
import com.yomahub.liteflow.entity.flow.Node;
|
|
||||||
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
||||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||||
import com.yomahub.liteflow.exception.*;
|
import com.yomahub.liteflow.exception.*;
|
||||||
@@ -25,11 +19,9 @@ import org.dom4j.Element;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* xml形式的解析器
|
* xml形式的解析器
|
||||||
@@ -127,6 +119,7 @@ public abstract class XmlFlowParser extends FlowParser {
|
|||||||
String group;
|
String group;
|
||||||
String errorResume;
|
String errorResume;
|
||||||
String any;
|
String any;
|
||||||
|
String threadExecutorClass;
|
||||||
ConditionTypeEnum conditionType;
|
ConditionTypeEnum conditionType;
|
||||||
|
|
||||||
//构建chainBuilder
|
//构建chainBuilder
|
||||||
@@ -140,6 +133,7 @@ public abstract class XmlFlowParser extends FlowParser {
|
|||||||
errorResume = condE.attributeValue("errorResume");
|
errorResume = condE.attributeValue("errorResume");
|
||||||
group = condE.attributeValue("group");
|
group = condE.attributeValue("group");
|
||||||
any = condE.attributeValue("any");
|
any = condE.attributeValue("any");
|
||||||
|
threadExecutorClass = condE.attributeValue("threadExecutorClass");
|
||||||
|
|
||||||
if (ObjectUtil.isNull(conditionType)){
|
if (ObjectUtil.isNull(conditionType)){
|
||||||
throw new NotSupportConditionException("ConditionType is not supported");
|
throw new NotSupportConditionException("ConditionType is not supported");
|
||||||
@@ -156,6 +150,7 @@ public abstract class XmlFlowParser extends FlowParser {
|
|||||||
.setErrorResume(errorResume)
|
.setErrorResume(errorResume)
|
||||||
.setGroup(group)
|
.setGroup(group)
|
||||||
.setAny(any)
|
.setAny(any)
|
||||||
|
.setThreadExecutorClass(threadExecutorClass)
|
||||||
.setValue(condValueStr)
|
.setValue(condValueStr)
|
||||||
.build()
|
.build()
|
||||||
).build();
|
).build();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* <p>Title: liteflow</p>
|
* <p>Title: liteflow</p>
|
||||||
* <p>Description: 轻量级的组件式流程框架</p>
|
* <p>Description: 轻量级的组件式流程框架</p>
|
||||||
|
*
|
||||||
* @author Bryan.Zhang
|
* @author Bryan.Zhang
|
||||||
* @email weenyc31@163.com
|
* @email weenyc31@163.com
|
||||||
* @Date 2020/4/1
|
* @Date 2020/4/1
|
||||||
@@ -8,31 +9,39 @@
|
|||||||
package com.yomahub.liteflow.thread;
|
package com.yomahub.liteflow.thread;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import com.yomahub.liteflow.exception.ThreadExecutorServiceCreateException;
|
import com.yomahub.liteflow.exception.ThreadExecutorServiceCreateException;
|
||||||
import com.yomahub.liteflow.property.LiteflowConfig;
|
import com.yomahub.liteflow.property.LiteflowConfig;
|
||||||
import com.yomahub.liteflow.util.SpringAware;
|
import com.yomahub.liteflow.util.SpringAware;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 线程池工具类
|
* 线程池工具类
|
||||||
|
*
|
||||||
* @author Bryan.Zhang
|
* @author Bryan.Zhang
|
||||||
*/
|
*/
|
||||||
public class ExecutorHelper {
|
public class ExecutorHelper {
|
||||||
|
|
||||||
private final Logger LOG = LoggerFactory.getLogger(ExecutorHelper.class);
|
private final Logger LOG = LoggerFactory.getLogger(ExecutorHelper.class);
|
||||||
|
|
||||||
private static ExecutorHelper executorHelper;
|
private static ExecutorHelper executorHelper;
|
||||||
|
|
||||||
private ExecutorService executorService;
|
private ExecutorService executorService;
|
||||||
|
|
||||||
|
private Map<String, ExecutorService> executorServiceMap;
|
||||||
|
|
||||||
private ExecutorHelper() {
|
private ExecutorHelper() {
|
||||||
|
executorServiceMap = Maps.newConcurrentMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ExecutorHelper loadInstance(){
|
public static ExecutorHelper loadInstance() {
|
||||||
if (ObjectUtil.isNull(executorHelper)){
|
if (ObjectUtil.isNull(executorHelper)) {
|
||||||
executorHelper = new ExecutorHelper();
|
executorHelper = new ExecutorHelper();
|
||||||
}
|
}
|
||||||
return executorHelper;
|
return executorHelper;
|
||||||
@@ -56,7 +65,7 @@ public class ExecutorHelper {
|
|||||||
* @param timeout 等待时间
|
* @param timeout 等待时间
|
||||||
*/
|
*/
|
||||||
public void shutdownAwaitTermination(ExecutorService pool,
|
public void shutdownAwaitTermination(ExecutorService pool,
|
||||||
long timeout) {
|
long timeout) {
|
||||||
pool.shutdown();
|
pool.shutdown();
|
||||||
try {
|
try {
|
||||||
if (!pool.awaitTermination(timeout, TimeUnit.SECONDS)) {
|
if (!pool.awaitTermination(timeout, TimeUnit.SECONDS)) {
|
||||||
@@ -72,22 +81,37 @@ public class ExecutorHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ExecutorService buildExecutor() {
|
public ExecutorService buildExecutor() {
|
||||||
if (ObjectUtil.isNull(executorService)){
|
if (ObjectUtil.isNull(executorService)) {
|
||||||
LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class);
|
LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class);
|
||||||
|
assert liteflowConfig != null;
|
||||||
try{
|
executorService = buildExecutor(liteflowConfig.getThreadExecutorClass());
|
||||||
assert liteflowConfig != null;
|
|
||||||
ExecutorBuilder executorBuilder = (ExecutorBuilder)Class.forName(liteflowConfig.getThreadExecutorClass()).newInstance();
|
|
||||||
executorService = executorBuilder.buildExecutor();
|
|
||||||
}catch (Exception e){
|
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
throw new ThreadExecutorServiceCreateException(e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return executorService;
|
return executorService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ExecutorService buildExecutor(String threadExecutorClass) {
|
||||||
|
try {
|
||||||
|
if (StrUtil.isBlank(threadExecutorClass)) {
|
||||||
|
return buildExecutor();
|
||||||
|
}
|
||||||
|
ExecutorService executorServiceFromCache = executorServiceMap.get(threadExecutorClass);
|
||||||
|
if (executorServiceFromCache != null) {
|
||||||
|
return executorServiceFromCache;
|
||||||
|
} else {
|
||||||
|
ExecutorService executorService = getExecutorBuilder(threadExecutorClass).buildExecutor();
|
||||||
|
executorServiceMap.put(threadExecutorClass, executorService);
|
||||||
|
return executorService;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.error(e.getMessage(), e);
|
||||||
|
throw new ThreadExecutorServiceCreateException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExecutorBuilder getExecutorBuilder(String threadExecutorClass) throws Exception {
|
||||||
|
return (ExecutorBuilder) Class.forName(threadExecutorClass).newInstance();
|
||||||
|
}
|
||||||
|
|
||||||
public ExecutorService getExecutorService() {
|
public ExecutorService getExecutorService() {
|
||||||
return executorService;
|
return executorService;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class BuilderSpringbootTest extends BaseTest {
|
|||||||
|
|
||||||
//基于普通组件的builder模式测试
|
//基于普通组件的builder模式测试
|
||||||
@Test
|
@Test
|
||||||
public void testBuilder() throws Exception{
|
public void testBuilder() throws Exception {
|
||||||
LiteFlowNodeBuilder.createNode().setId("a")
|
LiteFlowNodeBuilder.createNode().setId("a")
|
||||||
.setName("组件A")
|
.setName("组件A")
|
||||||
.setType(NodeTypeEnum.COMMON)
|
.setType(NodeTypeEnum.COMMON)
|
||||||
@@ -71,13 +71,16 @@ public class BuilderSpringbootTest extends BaseTest {
|
|||||||
|
|
||||||
|
|
||||||
LiteFlowChainBuilder.createChain().setChainName("chain2").setCondition(
|
LiteFlowChainBuilder.createChain().setChainName("chain2").setCondition(
|
||||||
LiteFlowConditionBuilder.createThenCondition().setValue("c,d").build()
|
LiteFlowConditionBuilder.createWhenCondition().setValue("c,d").build()
|
||||||
).build();
|
).build();
|
||||||
|
|
||||||
LiteFlowChainBuilder.createChain().setChainName("chain1").setCondition(
|
LiteFlowChainBuilder.createChain().setChainName("chain1").setCondition(
|
||||||
LiteFlowConditionBuilder.createThenCondition().setValue("a,b").build()
|
LiteFlowConditionBuilder
|
||||||
|
.createWhenCondition()
|
||||||
|
.setValue("a,b").build()
|
||||||
).setCondition(
|
).setCondition(
|
||||||
LiteFlowConditionBuilder.createWhenCondition().setValue("e(f|g|chain2)").build()
|
LiteFlowConditionBuilder.createWhenCondition()
|
||||||
|
.setValue("e(f|g|chain2)").build()
|
||||||
).build();
|
).build();
|
||||||
|
|
||||||
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1");
|
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1");
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.yomahub.liteflow.test.customWhenThreadPool;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import com.alibaba.ttl.threadpool.TtlExecutors;
|
||||||
|
import com.yomahub.liteflow.property.LiteflowConfig;
|
||||||
|
import com.yomahub.liteflow.thread.ExecutorBuilder;
|
||||||
|
import com.yomahub.liteflow.util.SpringAware;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
public class CustomThreadExecutor1 implements ExecutorBuilder {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExecutorService buildExecutor() {
|
||||||
|
LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class);
|
||||||
|
//只有在非spring的场景下liteflowConfig才会为null
|
||||||
|
if (ObjectUtil.isNull(liteflowConfig)) {
|
||||||
|
liteflowConfig = new LiteflowConfig();
|
||||||
|
}
|
||||||
|
return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(liteflowConfig.getWhenMaxWorkers(),
|
||||||
|
liteflowConfig.getWhenMaxWorkers(),
|
||||||
|
0L, TimeUnit.MILLISECONDS,
|
||||||
|
new ArrayBlockingQueue<>(liteflowConfig.getWhenQueueLimit()),
|
||||||
|
new ThreadFactory() {
|
||||||
|
private final AtomicLong number = new AtomicLong();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread newThread(Runnable r) {
|
||||||
|
Thread newThread = Executors.defaultThreadFactory().newThread(r);
|
||||||
|
newThread.setName("Customer-when-1-thead-" + number.getAndIncrement());
|
||||||
|
newThread.setDaemon(false);
|
||||||
|
return newThread;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new ThreadPoolExecutor.AbortPolicy()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.yomahub.liteflow.test.customWhenThreadPool;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import com.alibaba.ttl.threadpool.TtlExecutors;
|
||||||
|
import com.yomahub.liteflow.property.LiteflowConfig;
|
||||||
|
import com.yomahub.liteflow.thread.ExecutorBuilder;
|
||||||
|
import com.yomahub.liteflow.util.SpringAware;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
public class CustomThreadExecutor2 implements ExecutorBuilder {
|
||||||
|
@Override
|
||||||
|
public ExecutorService buildExecutor() {
|
||||||
|
LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class);
|
||||||
|
//只有在非spring的场景下liteflowConfig才会为null
|
||||||
|
if (ObjectUtil.isNull(liteflowConfig)) {
|
||||||
|
liteflowConfig = new LiteflowConfig();
|
||||||
|
}
|
||||||
|
return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(liteflowConfig.getWhenMaxWorkers(),
|
||||||
|
liteflowConfig.getWhenMaxWorkers(),
|
||||||
|
0L, TimeUnit.MILLISECONDS,
|
||||||
|
new ArrayBlockingQueue<>(liteflowConfig.getWhenQueueLimit()),
|
||||||
|
new ThreadFactory() {
|
||||||
|
private final AtomicLong number = new AtomicLong();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread newThread(Runnable r) {
|
||||||
|
Thread newThread = Executors.defaultThreadFactory().newThread(r);
|
||||||
|
newThread.setName("Customer-when-2-thead-" + number.getAndIncrement());
|
||||||
|
newThread.setDaemon(false);
|
||||||
|
return newThread;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new ThreadPoolExecutor.AbortPolicy()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.yomahub.liteflow.test.customWhenThreadPool;
|
||||||
|
|
||||||
|
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.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 org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* springboot环境下异步线程超时日志打印测试
|
||||||
|
* @author Bryan.Zhang
|
||||||
|
* @since 2.6.4
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@TestPropertySource(value = "classpath:/customWhenThreadPool/application.properties")
|
||||||
|
@SpringBootTest(classes = CustomWhenThreadPoolSpringbootTest.class)
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ComponentScan({"com.yomahub.liteflow.test.customWhenThreadPool.cmp"})
|
||||||
|
public class CustomWhenThreadPoolSpringbootTest extends BaseTest {
|
||||||
|
|
||||||
|
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FlowExecutor flowExecutor;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCustomThreadPool() throws Exception{
|
||||||
|
LiteflowResponse<DefaultSlot> response1 = flowExecutor.execute2Resp("chain1", "arg");
|
||||||
|
Assert.assertTrue(response1.isSuccess());
|
||||||
|
Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("lf-when-thead"));
|
||||||
|
|
||||||
|
LiteflowResponse<DefaultSlot> response2 = flowExecutor.execute2Resp("chain2", "arg");
|
||||||
|
Assert.assertTrue(response2.isSuccess());
|
||||||
|
Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("Customer-when-1-thead"));
|
||||||
|
|
||||||
|
LiteflowResponse<DefaultSlot> response3 = flowExecutor.execute2Resp("chain3", "arg");
|
||||||
|
Assert.assertTrue(response3.isSuccess());
|
||||||
|
Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("Customer-when-2-thead"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* <p>Title: liteflow</p>
|
||||||
|
* <p>Description: 轻量级的组件式流程框架</p>
|
||||||
|
* @author Bryan.Zhang
|
||||||
|
* @email weenyc31@163.com
|
||||||
|
* @Date 2020/4/1
|
||||||
|
*/
|
||||||
|
package com.yomahub.liteflow.test.customWhenThreadPool.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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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.customWhenThreadPool.cmp;
|
||||||
|
|
||||||
|
import com.yomahub.liteflow.core.NodeComponent;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component("b")
|
||||||
|
public class BCmp extends NodeComponent {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process() {
|
||||||
|
this.getSlot().setData("threadName", Thread.currentThread().getName());
|
||||||
|
System.out.println("BCmp executed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
/**
|
||||||
|
* <p>Title: liteflow</p>
|
||||||
|
* <p>Description: 轻量级的组件式流程框架</p>
|
||||||
|
* @author Bryan.Zhang
|
||||||
|
* @email weenyc31@163.com
|
||||||
|
* @Date 2020/4/1
|
||||||
|
*/
|
||||||
|
package com.yomahub.liteflow.test.customWhenThreadPool.cmp;
|
||||||
|
|
||||||
|
import com.yomahub.liteflow.core.NodeComponent;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component("c")
|
||||||
|
public class CCmp extends NodeComponent {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process() {
|
||||||
|
System.out.println("CCmp executed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
/**
|
||||||
|
* <p>Title: liteflow</p>
|
||||||
|
* <p>Description: 轻量级的组件式流程框架</p>
|
||||||
|
* @author Bryan.Zhang
|
||||||
|
* @email weenyc31@163.com
|
||||||
|
* @Date 2020/4/1
|
||||||
|
*/
|
||||||
|
package com.yomahub.liteflow.test.customWhenThreadPool.cmp;
|
||||||
|
|
||||||
|
import com.yomahub.liteflow.core.NodeComponent;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component("d")
|
||||||
|
public class DCmp extends NodeComponent {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process() {
|
||||||
|
System.out.println("DCmp executed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
/**
|
||||||
|
* <p>Title: liteflow</p>
|
||||||
|
* <p>Description: 轻量级的组件式流程框架</p>
|
||||||
|
* @author Bryan.Zhang
|
||||||
|
* @email weenyc31@163.com
|
||||||
|
* @Date 2020/4/1
|
||||||
|
*/
|
||||||
|
package com.yomahub.liteflow.test.customWhenThreadPool.cmp;
|
||||||
|
|
||||||
|
import com.yomahub.liteflow.core.NodeComponent;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component("e")
|
||||||
|
public class ECmp extends NodeComponent {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process() {
|
||||||
|
System.out.println("ECmp executed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
liteflow.rule-source=customWhenThreadPool/flow.xml
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<flow>
|
||||||
|
<chain name="chain1">
|
||||||
|
<when value="a,b"/>
|
||||||
|
</chain>
|
||||||
|
<chain name="chain2">
|
||||||
|
<when value="c,d" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor1"/>
|
||||||
|
</chain>
|
||||||
|
<chain name="chain3">
|
||||||
|
<when value="e,f" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor2"/>
|
||||||
|
</chain>
|
||||||
|
</flow>
|
||||||
Reference in New Issue
Block a user