From a58e3da8db7a329c16b30ad48c1d79e36e2b3e4d Mon Sep 17 00:00:00 2001 From: daiqi <466608943@qq.com> Date: Tue, 18 Jan 2022 01:19:28 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/core/FlowExecutor.java | 6 ++++ .../yomahub/liteflow/entity/data/AbsSlot.java | 9 ++++++ .../test/privateDelivery/cmp/ACmp.java | 19 ++++++++++-- .../test/privateDelivery/cmp/BCmp.java | 30 ++++++++++++++----- .../test/privateDelivery/cmp/DCmp.java | 21 +++++++++++++ .../test/resources/privateDelivery/flow.xml | 15 +++------- 6 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/DCmp.java diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java index bc0d05f0d..99777f69b 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java @@ -13,6 +13,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import com.google.common.collect.Lists; +import com.yomahub.liteflow.entity.flow.Node; import com.yomahub.liteflow.enums.FlowParserTypeEnum; import com.yomahub.liteflow.exception.*; import com.yomahub.liteflow.parser.*; @@ -266,6 +267,11 @@ public class FlowExecutor { this.execute(chainId, param, slotClazz, slotIndex, true); } + public void invoke(String nodeId, Integer slotIndex) throws Exception { + Node node = FlowBus.getNode(nodeId); + node.execute(slotIndex); + } + public DefaultSlot execute(String chainId) throws Exception { return this.execute(chainId, null, DefaultSlot.class, null, false); } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java index e9061327e..ebdc4f261 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java @@ -127,6 +127,15 @@ public abstract class AbsSlot implements Slot { } } + public Queue getPrivateDeliveryQueue(String nodeId){ + String privateDKey = PRIVATE_DELIVERY_PREFIX + nodeId; + if(dataMap.containsKey(privateDKey)){ + return (Queue) dataMap.get(privateDKey); + }else{ + return null; + } + } + public T getPrivateDeliveryData(String nodeId){ String privateDKey = PRIVATE_DELIVERY_PREFIX + nodeId; if(dataMap.containsKey(privateDKey)){ diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/ACmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/ACmp.java index 26fe58c6e..88aa64dd8 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/ACmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/ACmp.java @@ -8,23 +8,36 @@ package com.yomahub.liteflow.test.privateDelivery.cmp; import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.entity.data.DefaultSlot; import com.yomahub.liteflow.entity.data.Slot; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashSet; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; @LiteflowComponent("a") public class ACmp extends NodeComponent { - + @Autowired + private FlowExecutor flowExecutor; @Override public void process() { System.out.println("ACmp executed!"); Slot slot = getSlot(); slot.setData("testSet", new HashSet<>()); - for (int i = 0; i < 100; i++) { - this.sendPrivateDeliveryData("b",i+1); + try { + Queue queue = new ConcurrentLinkedQueue<>(); + for (int i = 1; i <= 100; i++) { + queue.add(i); + } + flowExecutor.execute2Resp("chain2", queue); + + }catch (Exception e) { + e.printStackTrace(); } } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/BCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/BCmp.java index 6a0cfbb5e..d3c9b4997 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/BCmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/BCmp.java @@ -1,26 +1,42 @@ /** *

Title: liteflow

*

Description: 轻量级的组件式流程框架

+ * * @author Bryan.Zhang * @email weenyc31@163.com * @Date 2020/4/1 */ package com.yomahub.liteflow.test.privateDelivery.cmp; +import cn.hutool.core.collection.CollUtil; import com.yomahub.liteflow.annotation.LiteflowComponent; import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; +import java.util.Queue; import java.util.Set; @LiteflowComponent("b") public class BCmp extends NodeComponent { - @Override - public void process() { - System.out.println("BCmp executed!"); - Integer value = this.getPrivateDeliveryData(); - Set testSet = this.getSlot().getData("testSet"); - testSet.add(value); - } + @Override + public boolean isAccess() { + Queue values = this.getSlot().getRequestData(); + System.out.println("BCmp executed! values.size" + values.size()); + if (CollUtil.isEmpty(values)) { + return false; + } + Integer value = values.poll(); + if (value == null) { + return false; + } + this.sendPrivateDeliveryData(this.getNodeId(), value); + return true; + } + + @Override + public void process() { + Integer value = getPrivateDeliveryData(); + System.out.println("BCmp executed!" + value); + } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/DCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/DCmp.java new file mode 100644 index 000000000..a17ad9bb6 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/DCmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.privateDelivery.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("d") +public class DCmp extends NodeComponent { + + @Override + public void process() { + System.out.println("CCmp executed!"); + } + +} diff --git a/liteflow-testcase-springboot/src/test/resources/privateDelivery/flow.xml b/liteflow-testcase-springboot/src/test/resources/privateDelivery/flow.xml index bf7146217..37ce81f4a 100644 --- a/liteflow-testcase-springboot/src/test/resources/privateDelivery/flow.xml +++ b/liteflow-testcase-springboot/src/test/resources/privateDelivery/flow.xml @@ -2,17 +2,10 @@ - - - - - - - - - - - + +
+        
+    
 
\ No newline at end of file

From 717b03f6c3cfa50df9cabf92befc69aa137a47c3 Mon Sep 17 00:00:00 2001
From: sikadai <466608943@qq.com>
Date: Thu, 20 Jan 2022 21:03:42 +0800
Subject: [PATCH 2/7] =?UTF-8?q?=E6=94=AF=E6=8C=81when=E7=BB=84=E4=BB=B6?=
 =?UTF-8?q?=E7=BB=B4=E5=BA=A6=E4=BD=BF=E7=94=A8=E7=BA=BF=E7=A8=8B=E6=B1=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../builder/LiteFlowWhenConditionBuilder.java |  9 +++
 .../yomahub/liteflow/entity/flow/Chain.java   |  3 +-
 .../liteflow/entity/flow/Condition.java       | 10 ++++
 .../liteflow/entity/flow/WhenCondition.java   |  1 +
 .../liteflow/parser/JsonFlowParser.java       |  9 +--
 .../liteflow/parser/XmlFlowParser.java        | 11 +---
 .../liteflow/thread/ExecutorHelper.java       | 56 +++++++++++++------
 .../test/builder/BuilderSpringbootTest.java   | 14 ++++-
 .../CustomThreadExecutor1.java                | 38 +++++++++++++
 .../CustomThreadExecutor2.java                | 37 ++++++++++++
 10 files changed, 153 insertions(+), 35 deletions(-)
 create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor1.java
 create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor2.java

diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/LiteFlowWhenConditionBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/LiteFlowWhenConditionBuilder.java
index c61a1dda5..5e71ec8dd 100644
--- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/LiteFlowWhenConditionBuilder.java
+++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/LiteFlowWhenConditionBuilder.java
@@ -48,4 +48,13 @@ public class LiteFlowWhenConditionBuilder extends LiteFlowConditionBuilder{
         }
         return setAny(Boolean.parseBoolean(any));
     }
+
+
+    public LiteFlowWhenConditionBuilder setThreadExecutorClass(String executorServiceName){
+        if (StrUtil.isBlank(executorServiceName)) {
+            return this;
+        }
+        this.condition.setThreadExecutorClass(executorServiceName);
+        return this;
+    }
 }
diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java
index 2c85a4623..b30c5f549 100644
--- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java
+++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Chain.java
@@ -10,7 +10,6 @@ package com.yomahub.liteflow.entity.flow;
 
 import cn.hutool.core.collection.CollUtil;
 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.Slot;
 import com.yomahub.liteflow.entity.flow.parallel.CompletableFutureTimeout;
@@ -120,7 +119,7 @@ public class Chain implements Executable {
         Slot slot = DataBus.getSlot(slotIndex);
 
         //此方法其实只会初始化一次Executor,不会每次都会初始化。Executor是唯一的
-        ExecutorService parallelExecutor = ExecutorHelper.loadInstance().buildExecutor();
+        ExecutorService parallelExecutor = ExecutorHelper.loadInstance().buildExecutor(condition.getThreadExecutorClass());
 
         //获得liteflow的参数
         LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Condition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Condition.java
index 6dbbeb23e..07cf4ac53 100644
--- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Condition.java
+++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/Condition.java
@@ -31,6 +31,8 @@ public class Condition {
 
 	//只在when类型下有效,为true的话说明在多个并行节点下,任意一个成功,整个when就成功
 	private boolean any = false;
+	// when单独的线程池名称
+	private String threadExecutorClass;
 
 	public Condition(List nodeList) {
 		this.nodeList = nodeList;
@@ -77,4 +79,12 @@ public class Condition {
 	public void setAny(boolean any) {
 		this.any = any;
 	}
+
+	public String getThreadExecutorClass() {
+		return threadExecutorClass;
+	}
+
+	public void setThreadExecutorClass(String threadExecutorClass) {
+		this.threadExecutorClass = threadExecutorClass;
+	}
 }
diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/WhenCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/WhenCondition.java
index 4199b5bf0..507541966 100644
--- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/WhenCondition.java
+++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/flow/WhenCondition.java
@@ -19,6 +19,7 @@ public class WhenCondition extends Condition{
 		super.setGroup(condition.getGroup());
 		super.setErrorResume(condition.isErrorResume());
 		super.setAny(condition.isAny());
+		super.setThreadExecutorClass(condition.getThreadExecutorClass());
 	}
 
 }
diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/parser/JsonFlowParser.java b/liteflow-core/src/main/java/com/yomahub/liteflow/parser/JsonFlowParser.java
index 11e3e751c..48de2c2e2 100644
--- a/liteflow-core/src/main/java/com/yomahub/liteflow/parser/JsonFlowParser.java
+++ b/liteflow-core/src/main/java/com/yomahub/liteflow/parser/JsonFlowParser.java
@@ -2,7 +2,6 @@ package com.yomahub.liteflow.parser;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.collection.ListUtil;
-import cn.hutool.core.io.resource.ResourceUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 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.LiteFlowConditionBuilder;
 import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
-import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
 import com.yomahub.liteflow.core.NodeComponent;
-import com.yomahub.liteflow.entity.flow.*;
 import com.yomahub.liteflow.enums.ConditionTypeEnum;
 import com.yomahub.liteflow.enums.NodeTypeEnum;
 import com.yomahub.liteflow.exception.EmptyConditionValueException;
-import com.yomahub.liteflow.exception.ExecutableItemNotFoundException;
 import com.yomahub.liteflow.exception.NodeTypeNotSupportException;
 import com.yomahub.liteflow.exception.NotSupportConditionException;
 import com.yomahub.liteflow.flow.FlowBus;
 import com.yomahub.liteflow.spring.ComponentScanner;
-import org.dom4j.Element;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.*;
-import java.util.function.Consumer;
 
 /**
  * Json格式解析器
@@ -136,6 +130,7 @@ public abstract class JsonFlowParser extends FlowParser {
         String group;
         String errorResume;
         String any;
+        String threadExecutorClass;
 
         //构建chainBuilder
         String chainName = chainObject.getString("name");
@@ -148,6 +143,7 @@ public abstract class JsonFlowParser extends FlowParser {
             errorResume = condObject.getString("errorResume");
             group = condObject.getString("group");
             any = condObject.getString("any");
+            threadExecutorClass = condObject.getString("threadExecutorClass");
 
             if (ObjectUtil.isNull(conditionType)){
                 throw new NotSupportConditionException("ConditionType is not supported");
@@ -165,6 +161,7 @@ public abstract class JsonFlowParser extends FlowParser {
                                 .setErrorResume(errorResume)
                                 .setGroup(group)
                                 .setAny(any)
+                                .setThreadExecutorClass(threadExecutorClass)
                                 .setValue(condValueStr)
                                 .build()
                 ).build();
diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java b/liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java
index 63986d3a1..26e74963c 100644
--- a/liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java
+++ b/liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java
@@ -2,18 +2,12 @@ package com.yomahub.liteflow.parser;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.collection.ListUtil;
-import cn.hutool.core.io.resource.ResourceUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
 import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
 import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
-import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
 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.NodeTypeEnum;
 import com.yomahub.liteflow.exception.*;
@@ -25,11 +19,9 @@ import org.dom4j.Element;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map.Entry;
-import java.util.function.Consumer;
 
 /**
  * xml形式的解析器
@@ -127,6 +119,7 @@ public abstract class XmlFlowParser extends FlowParser {
         String group;
         String errorResume;
         String any;
+        String threadExecutorClass;
         ConditionTypeEnum conditionType;
 
         //构建chainBuilder
@@ -140,6 +133,7 @@ public abstract class XmlFlowParser extends FlowParser {
             errorResume = condE.attributeValue("errorResume");
             group = condE.attributeValue("group");
             any = condE.attributeValue("any");
+            threadExecutorClass = condE.attributeValue("threadExecutorClass");
 
             if (ObjectUtil.isNull(conditionType)){
                 throw new NotSupportConditionException("ConditionType is not supported");
@@ -156,6 +150,7 @@ public abstract class XmlFlowParser extends FlowParser {
                                 .setErrorResume(errorResume)
                                 .setGroup(group)
                                 .setAny(any)
+                                .setThreadExecutorClass(threadExecutorClass)
                                 .setValue(condValueStr)
                                 .build()
                 ).build();
diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java
index cf9a78822..d49c65930 100644
--- a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java
+++ b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java
@@ -1,6 +1,7 @@
 /**
  * 

Title: liteflow

*

Description: 轻量级的组件式流程框架

+ * * @author Bryan.Zhang * @email weenyc31@163.com * @Date 2020/4/1 @@ -8,31 +9,39 @@ package com.yomahub.liteflow.thread; 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.property.LiteflowConfig; import com.yomahub.liteflow.util.SpringAware; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + +import java.util.Map; import java.util.concurrent.*; /** * 线程池工具类 + * * @author Bryan.Zhang */ public class ExecutorHelper { - + private final Logger LOG = LoggerFactory.getLogger(ExecutorHelper.class); private static ExecutorHelper executorHelper; private ExecutorService executorService; - + + private Map executorServiceMap; + private ExecutorHelper() { + executorServiceMap = Maps.newConcurrentMap(); } - public static ExecutorHelper loadInstance(){ - if (ObjectUtil.isNull(executorHelper)){ + public static ExecutorHelper loadInstance() { + if (ObjectUtil.isNull(executorHelper)) { executorHelper = new ExecutorHelper(); } return executorHelper; @@ -56,7 +65,7 @@ public class ExecutorHelper { * @param timeout 等待时间 */ public void shutdownAwaitTermination(ExecutorService pool, - long timeout) { + long timeout) { pool.shutdown(); try { if (!pool.awaitTermination(timeout, TimeUnit.SECONDS)) { @@ -72,22 +81,37 @@ public class ExecutorHelper { } public ExecutorService buildExecutor() { - if (ObjectUtil.isNull(executorService)){ + if (ObjectUtil.isNull(executorService)) { LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class); - - try{ - 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()); - } - + assert liteflowConfig != null; + executorService = buildExecutor(liteflowConfig.getThreadExecutorClass()); } 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() { return executorService; } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/builder/BuilderSpringbootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/builder/BuilderSpringbootTest.java index aaa3a3894..b773b0354 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/builder/BuilderSpringbootTest.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/builder/BuilderSpringbootTest.java @@ -8,6 +8,8 @@ import com.yomahub.liteflow.entity.data.DefaultSlot; import com.yomahub.liteflow.entity.data.LiteflowResponse; import com.yomahub.liteflow.enums.NodeTypeEnum; import com.yomahub.liteflow.test.BaseTest; +import com.yomahub.liteflow.test.customThreadPool.CustomThreadExecutor1; +import com.yomahub.liteflow.test.customThreadPool.CustomThreadExecutor2; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -71,13 +73,19 @@ public class BuilderSpringbootTest extends BaseTest { LiteFlowChainBuilder.createChain().setChainName("chain2").setCondition( - LiteFlowConditionBuilder.createThenCondition().setValue("c,d").build() + LiteFlowConditionBuilder.createWhenCondition().setValue("c,d").build() ).build(); LiteFlowChainBuilder.createChain().setChainName("chain1").setCondition( - LiteFlowConditionBuilder.createThenCondition().setValue("a,b").build() + LiteFlowConditionBuilder + .createWhenCondition() + .setAny(true) + .setThreadExecutorClass(CustomThreadExecutor2.class.getName()) + .setValue("a,b").build() ).setCondition( - LiteFlowConditionBuilder.createWhenCondition().setValue("e(f|g|chain2)").build() + LiteFlowConditionBuilder.createWhenCondition() + .setThreadExecutorClass(CustomThreadExecutor1.class.getName()) + .setValue("e(f|g|chain2)").build() ).build(); LiteflowResponse response = flowExecutor.execute2Resp("chain1"); diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor1.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor1.java new file mode 100644 index 000000000..0cc24856c --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor1.java @@ -0,0 +1,38 @@ +package com.yomahub.liteflow.test.customThreadPool; + +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-thead-" + number.getAndIncrement()); + newThread.setDaemon(false); + return newThread; + } + }, + new ThreadPoolExecutor.AbortPolicy())); + } +} diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor2.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor2.java new file mode 100644 index 000000000..36de8f3ae --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor2.java @@ -0,0 +1,37 @@ +package com.yomahub.liteflow.test.customThreadPool; + +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-222thead-" + number.getAndIncrement()); + newThread.setDaemon(false); + return newThread; + } + }, + new ThreadPoolExecutor.AbortPolicy())); + } +} From e1ef2d4b63811028b5acca1dc96b0ea9f3e07ba2 Mon Sep 17 00:00:00 2001 From: sikadai <466608943@qq.com> Date: Fri, 21 Jan 2022 22:25:47 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=AE=8C=E5=96=84when=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/builder/BuilderSpringbootTest.java | 7 +-- .../CustomThreadExecutor1.java | 4 +- .../CustomThreadExecutor2.java | 4 +- .../CustomWhenThreadPoolSpringbootTest.java | 51 +++++++++++++++++++ .../test/customWhenThreadPool/cmp/ACmp.java | 20 ++++++++ .../test/customWhenThreadPool/cmp/BCmp.java | 22 ++++++++ .../test/customWhenThreadPool/cmp/CCmp.java | 21 ++++++++ .../test/customWhenThreadPool/cmp/DCmp.java | 21 ++++++++ .../test/customWhenThreadPool/cmp/ECmp.java | 21 ++++++++ .../application.properties | 1 + .../resources/customWhenThreadPool/flow.xml | 12 +++++ 11 files changed, 174 insertions(+), 10 deletions(-) rename liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/{customThreadPool => customWhenThreadPool}/CustomThreadExecutor1.java (90%) rename liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/{customThreadPool => customWhenThreadPool}/CustomThreadExecutor2.java (90%) create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ACmp.java create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/BCmp.java create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java create mode 100644 liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/application.properties create mode 100644 liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/builder/BuilderSpringbootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/builder/BuilderSpringbootTest.java index b773b0354..0ececacb5 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/builder/BuilderSpringbootTest.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/builder/BuilderSpringbootTest.java @@ -8,8 +8,6 @@ import com.yomahub.liteflow.entity.data.DefaultSlot; import com.yomahub.liteflow.entity.data.LiteflowResponse; import com.yomahub.liteflow.enums.NodeTypeEnum; import com.yomahub.liteflow.test.BaseTest; -import com.yomahub.liteflow.test.customThreadPool.CustomThreadExecutor1; -import com.yomahub.liteflow.test.customThreadPool.CustomThreadExecutor2; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,7 +32,7 @@ public class BuilderSpringbootTest extends BaseTest { //基于普通组件的builder模式测试 @Test - public void testBuilder() throws Exception{ + public void testBuilder() throws Exception { LiteFlowNodeBuilder.createNode().setId("a") .setName("组件A") .setType(NodeTypeEnum.COMMON) @@ -79,12 +77,9 @@ public class BuilderSpringbootTest extends BaseTest { LiteFlowChainBuilder.createChain().setChainName("chain1").setCondition( LiteFlowConditionBuilder .createWhenCondition() - .setAny(true) - .setThreadExecutorClass(CustomThreadExecutor2.class.getName()) .setValue("a,b").build() ).setCondition( LiteFlowConditionBuilder.createWhenCondition() - .setThreadExecutorClass(CustomThreadExecutor1.class.getName()) .setValue("e(f|g|chain2)").build() ).build(); diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor1.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java similarity index 90% rename from liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor1.java rename to liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java index 0cc24856c..f942c8fb2 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor1.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java @@ -1,4 +1,4 @@ -package com.yomahub.liteflow.test.customThreadPool; +package com.yomahub.liteflow.test.customWhenThreadPool; import cn.hutool.core.util.ObjectUtil; import com.alibaba.ttl.threadpool.TtlExecutors; @@ -28,7 +28,7 @@ public class CustomThreadExecutor1 implements ExecutorBuilder { @Override public Thread newThread(Runnable r) { Thread newThread = Executors.defaultThreadFactory().newThread(r); - newThread.setName("Customer-when-thead-" + number.getAndIncrement()); + newThread.setName("Customer-when-1-thead-" + number.getAndIncrement()); newThread.setDaemon(false); return newThread; } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor2.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java similarity index 90% rename from liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor2.java rename to liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java index 36de8f3ae..0e75e7a7a 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customThreadPool/CustomThreadExecutor2.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java @@ -1,4 +1,4 @@ -package com.yomahub.liteflow.test.customThreadPool; +package com.yomahub.liteflow.test.customWhenThreadPool; import cn.hutool.core.util.ObjectUtil; import com.alibaba.ttl.threadpool.TtlExecutors; @@ -27,7 +27,7 @@ public class CustomThreadExecutor2 implements ExecutorBuilder { @Override public Thread newThread(Runnable r) { Thread newThread = Executors.defaultThreadFactory().newThread(r); - newThread.setName("Customer-when-222thead-" + number.getAndIncrement()); + newThread.setName("Customer-when-2-thead-" + number.getAndIncrement()); newThread.setDaemon(false); return newThread; } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java new file mode 100644 index 000000000..8d7b7ddd7 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java @@ -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 response1 = flowExecutor.execute2Resp("chain1", "arg"); + Assert.assertTrue(response1.isSuccess()); + Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("lf-when-thead")); + + LiteflowResponse response2 = flowExecutor.execute2Resp("chain2", "arg"); + Assert.assertTrue(response2.isSuccess()); + Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("Customer-when-1-thead")); + + LiteflowResponse response3 = flowExecutor.execute2Resp("chain3", "arg"); + Assert.assertTrue(response3.isSuccess()); + Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("Customer-when-2-thead")); + } +} diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ACmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ACmp.java new file mode 100644 index 000000000..f47c972da --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ACmp.java @@ -0,0 +1,20 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } +} diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/BCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/BCmp.java new file mode 100644 index 000000000..69b1a3845 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/BCmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } + +} diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java new file mode 100644 index 000000000..81ccd9353 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } + +} diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java new file mode 100644 index 000000000..9d96d43c1 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } + +} diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java new file mode 100644 index 000000000..2a403abf1 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } + +} diff --git a/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/application.properties b/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/application.properties new file mode 100644 index 000000000..3447aaa3f --- /dev/null +++ b/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=customWhenThreadPool/flow.xml \ No newline at end of file diff --git a/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml b/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml new file mode 100644 index 000000000..eb5e85959 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file From a7f53be766a706702db4d7a0a127c3b4ef9ca49e Mon Sep 17 00:00:00 2001 From: daiqi <466608943@qq.com> Date: Fri, 21 Jan 2022 23:16:18 +0800 Subject: [PATCH 4/7] =?UTF-8?q?1.=20=E6=B7=BB=E5=8A=A0springboot=E4=B8=8Bw?= =?UTF-8?q?hen-thread=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=202.=20?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E6=8A=BD=E5=8F=96=E9=BB=98=E8=AE=A4=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E7=BA=BF=E7=A8=8B=E6=B1=A0=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/thread/ExecutorBuilder.java | 35 +++++++++++++++ .../liteflow/thread/ExecutorHelper.java | 27 +++++++++++- .../LiteFlowDefaultExecutorBuilder.java | 19 ++------ .../CustomThreadExecutor1.java | 19 ++------ .../CustomThreadExecutor2.java | 19 ++------ .../CustomThreadExecutor3.java | 24 +++++++++++ .../CustomWhenThreadPoolSpringbootTest.java | 43 +++++++++++++++++-- .../test/customWhenThreadPool/cmp/CCmp.java | 1 + .../test/customWhenThreadPool/cmp/DCmp.java | 1 + .../test/customWhenThreadPool/cmp/ECmp.java | 1 + .../test/customWhenThreadPool/cmp/FCmp.java | 22 ++++++++++ .../resources/customWhenThreadPool/flow.xml | 6 +-- 12 files changed, 164 insertions(+), 53 deletions(-) create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor3.java create mode 100644 liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/FCmp.java diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorBuilder.java index a974a2261..3a8e80e48 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorBuilder.java @@ -1,13 +1,48 @@ package com.yomahub.liteflow.thread; +import com.alibaba.ttl.threadpool.TtlExecutors; + import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicLong; /** * 并行多线程执行器构造器接口 + * * @author Bryan.Zhang * @since 2.6.6 */ public interface ExecutorBuilder { ExecutorService buildExecutor(); + + /** + *

+ * 构建默认的线程池对象 + *

+ * @author sikadai + * @date 2022/1/21 23:07 + * @param corePoolSize : 核心线程池数量 + * @param maximumPoolSize : 最大线程池数量 + * @param queueCapacity : 队列的容量 + * @param threadName : 线程吃名称 + * @return java.util.concurrent.ExecutorService + */ + default ExecutorService buildDefaultExecutor(int corePoolSize, int maximumPoolSize, int queueCapacity, String threadName) { + return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(corePoolSize, + maximumPoolSize, + 0L, TimeUnit.MILLISECONDS, + new ArrayBlockingQueue<>(queueCapacity), + new ThreadFactory() { + private final AtomicLong number = new AtomicLong(); + + @Override + public Thread newThread(Runnable r) { + Thread newThread = Executors.defaultThreadFactory().newThread(r); + newThread.setName(threadName + number.getAndIncrement()); + newThread.setDaemon(false); + return newThread; + } + }, + new ThreadPoolExecutor.AbortPolicy())); + } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java index d49c65930..a269d81dc 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java @@ -34,7 +34,12 @@ public class ExecutorHelper { private ExecutorService executorService; - private Map executorServiceMap; + /** + * 此处使用Map缓存线程池信息 + * key - 线程池构建者的Class全类名 + * value - 线程池对象 + * */ + private final Map executorServiceMap; private ExecutorHelper() { executorServiceMap = Maps.newConcurrentMap(); @@ -80,6 +85,7 @@ public class ExecutorHelper { } } + /** 构建全局默认线程池 */ public ExecutorService buildExecutor() { if (ObjectUtil.isNull(executorService)) { LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class); @@ -89,6 +95,15 @@ public class ExecutorHelper { return executorService; } + /** + *

+ * 构建线程池执行器 - 支持多个when公用一个线程池 + *

+ * @author sikadai + * @date 2022/1/21 23:00 + * @param threadExecutorClass : 线程池构建者的Class全类名 + * @return java.util.concurrent.ExecutorService + */ public ExecutorService buildExecutor(String threadExecutorClass) { try { if (StrUtil.isBlank(threadExecutorClass)) { @@ -108,6 +123,16 @@ public class ExecutorHelper { } } + /** + *

+ * 根据线程执行构建者Class类名获取ExecutorBuilder实例 + *

+ * + * @author sikadai + * @date 2022/1/21 23:04 + * @param threadExecutorClass + * @return com.yomahub.liteflow.thread.ExecutorBuilder + */ private ExecutorBuilder getExecutorBuilder(String threadExecutorClass) throws Exception { return (ExecutorBuilder) Class.forName(threadExecutorClass).newInstance(); } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/LiteFlowDefaultExecutorBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/LiteFlowDefaultExecutorBuilder.java index c0ea6878f..76c2b87ba 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/LiteFlowDefaultExecutorBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/LiteFlowDefaultExecutorBuilder.java @@ -21,21 +21,10 @@ public class LiteFlowDefaultExecutorBuilder implements ExecutorBuilder{ if (ObjectUtil.isNull(liteflowConfig)){ liteflowConfig = new LiteflowConfig(); } - - return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(liteflowConfig.getWhenMaxWorkers(), + return buildDefaultExecutor( 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("lf-when-thead-" + number.getAndIncrement()); - newThread.setDaemon(false); - return newThread; - } - }, - new ThreadPoolExecutor.AbortPolicy())); + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenQueueLimit(), + "lf-when-thead-"); } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java index f942c8fb2..ba9fe5eaf 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java @@ -18,21 +18,10 @@ public class CustomThreadExecutor1 implements ExecutorBuilder { if (ObjectUtil.isNull(liteflowConfig)) { liteflowConfig = new LiteflowConfig(); } - return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(liteflowConfig.getWhenMaxWorkers(), + return buildDefaultExecutor( 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())); + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenQueueLimit(), + "customer-when-1-thead-"); } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java index 0e75e7a7a..1ff6d22a0 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java @@ -17,21 +17,10 @@ public class CustomThreadExecutor2 implements ExecutorBuilder { if (ObjectUtil.isNull(liteflowConfig)) { liteflowConfig = new LiteflowConfig(); } - return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(liteflowConfig.getWhenMaxWorkers(), + return buildDefaultExecutor( 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())); + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenQueueLimit(), + "customer-when-2-thead-"); } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor3.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor3.java new file mode 100644 index 000000000..3519cfe20 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor3.java @@ -0,0 +1,24 @@ +package com.yomahub.liteflow.test.customWhenThreadPool; + +import cn.hutool.core.util.ObjectUtil; +import com.yomahub.liteflow.property.LiteflowConfig; +import com.yomahub.liteflow.thread.ExecutorBuilder; +import com.yomahub.liteflow.util.SpringAware; + +import java.util.concurrent.ExecutorService; + +public class CustomThreadExecutor3 implements ExecutorBuilder { + @Override + public ExecutorService buildExecutor() { + LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class); + //只有在非spring的场景下liteflowConfig才会为null + if (ObjectUtil.isNull(liteflowConfig)) { + liteflowConfig = new LiteflowConfig(); + } + return buildDefaultExecutor( + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenQueueLimit(), + "customer-when-3-thead-"); + } +} diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java index 8d7b7ddd7..60eddce48 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java @@ -1,8 +1,12 @@ package com.yomahub.liteflow.test.customWhenThreadPool; +import com.yomahub.liteflow.builder.LiteFlowChainBuilder; +import com.yomahub.liteflow.builder.LiteFlowConditionBuilder; +import com.yomahub.liteflow.builder.LiteFlowNodeBuilder; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.entity.data.DefaultSlot; import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.enums.NodeTypeEnum; import com.yomahub.liteflow.test.BaseTest; import org.junit.Assert; import org.junit.Test; @@ -19,6 +23,7 @@ import javax.annotation.Resource; /** * springboot环境下异步线程超时日志打印测试 + * * @author Bryan.Zhang * @since 2.6.4 */ @@ -35,17 +40,47 @@ public class CustomWhenThreadPoolSpringbootTest extends BaseTest { private FlowExecutor flowExecutor; @Test - public void testCustomThreadPool() throws Exception{ + public void testCustomThreadPool() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain", "arg"); + Assert.assertTrue(response.isSuccess()); + Assert.assertTrue(response.getSlot().getData("threadName").toString().startsWith("lf-when-thead")); + LiteflowResponse response1 = flowExecutor.execute2Resp("chain1", "arg"); Assert.assertTrue(response1.isSuccess()); - Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("lf-when-thead")); + Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead")); LiteflowResponse response2 = flowExecutor.execute2Resp("chain2", "arg"); Assert.assertTrue(response2.isSuccess()); - Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("Customer-when-1-thead")); + Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-2-thead")); + + // 使用build模式构建chain测试when条件的多线程 + LiteFlowNodeBuilder.createNode().setId("a") + .setName("组件A") + .setType(NodeTypeEnum.COMMON) + .setClazz("com.yomahub.liteflow.test.builder.cmp.ACmp") + .build(); + LiteFlowNodeBuilder.createNode().setId("b") + .setName("组件B") + .setType(NodeTypeEnum.COMMON) + .setClazz("com.yomahub.liteflow.test.builder.cmp.BCmp") + .build(); + LiteFlowNodeBuilder.createNode().setId("c") + .setName("组件C") + .setType(NodeTypeEnum.COMMON) + .setClazz("com.yomahub.liteflow.test.builder.cmp.CCmp") + .build(); + + + LiteFlowChainBuilder.createChain().setChainName("chain3").setCondition( + LiteFlowConditionBuilder + .createWhenCondition() + .setThreadExecutorClass(CustomThreadExecutor3.class.getName()) + .setValue("a,b,c,d") + .build() + ).build(); LiteflowResponse response3 = flowExecutor.execute2Resp("chain3", "arg"); Assert.assertTrue(response3.isSuccess()); - Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("Customer-when-2-thead")); + Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("customer-when-3-thead")); } } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java index 81ccd9353..df355c4b6 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java @@ -15,6 +15,7 @@ public class CCmp extends NodeComponent { @Override public void process() { + this.getSlot().setData("threadName", Thread.currentThread().getName()); System.out.println("CCmp executed!"); } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java index 9d96d43c1..a67ec4b9b 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java @@ -15,6 +15,7 @@ public class DCmp extends NodeComponent { @Override public void process() { + this.getSlot().setData("threadName", Thread.currentThread().getName()); System.out.println("DCmp executed!"); } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java index 2a403abf1..929e41767 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java @@ -15,6 +15,7 @@ public class ECmp extends NodeComponent { @Override public void process() { + this.getSlot().setData("threadName", Thread.currentThread().getName()); System.out.println("ECmp executed!"); } diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/FCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/FCmp.java new file mode 100644 index 000000000..f4285a637 --- /dev/null +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/FCmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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("f") +public class FCmp extends NodeComponent { + + @Override + public void process() { + this.getSlot().setData("threadName", Thread.currentThread().getName()); + System.out.println("FCmp executed!"); + } + +} diff --git a/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml b/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml index eb5e85959..c1fc18751 100644 --- a/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml +++ b/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml @@ -1,12 +1,12 @@ - + - + - + \ No newline at end of file From 5713431caf2c9395178e6e10bd21a4bae55a7897 Mon Sep 17 00:00:00 2001 From: daiqi <466608943@qq.com> Date: Fri, 21 Jan 2022 23:30:44 +0800 Subject: [PATCH 5/7] =?UTF-8?q?1.=20=E6=B7=BB=E5=8A=A0springboot=E4=B8=8Bw?= =?UTF-8?q?hen-thread=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=202.=20?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E6=8A=BD=E5=8F=96=E9=BB=98=E8=AE=A4=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E7=BA=BF=E7=A8=8B=E6=B1=A0=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=203.=20=E6=B7=BB=E5=8A=A0spring=E4=B8=8Bwhen-thread=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomWhenThreadPoolSpringbootTest.java | 6 +- .../CustomThreadExecutor1.java | 25 ++++++ .../CustomThreadExecutor2.java | 24 ++++++ .../CustomThreadExecutor3.java | 24 ++++++ .../CustomWhenThreadPoolSpringbootTest.java | 80 +++++++++++++++++++ .../test/customWhenThreadPool/cmp/ACmp.java | 20 +++++ .../test/customWhenThreadPool/cmp/BCmp.java | 22 +++++ .../test/customWhenThreadPool/cmp/CCmp.java | 22 +++++ .../test/customWhenThreadPool/cmp/DCmp.java | 22 +++++ .../test/customWhenThreadPool/cmp/ECmp.java | 22 +++++ .../test/customWhenThreadPool/cmp/FCmp.java | 22 +++++ .../customWhenThreadPool/application.xml | 23 ++++++ .../resources/customWhenThreadPool/flow.xml | 12 +++ 13 files changed, 321 insertions(+), 3 deletions(-) create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor3.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ACmp.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/BCmp.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java create mode 100644 liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/FCmp.java create mode 100644 liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/application.xml create mode 100644 liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/flow.xml diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java index 60eddce48..e258ca995 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java @@ -58,17 +58,17 @@ public class CustomWhenThreadPoolSpringbootTest extends BaseTest { LiteFlowNodeBuilder.createNode().setId("a") .setName("组件A") .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.builder.cmp.ACmp") + .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.ACmp") .build(); LiteFlowNodeBuilder.createNode().setId("b") .setName("组件B") .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.builder.cmp.BCmp") + .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.BCmp") .build(); LiteFlowNodeBuilder.createNode().setId("c") .setName("组件C") .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.builder.cmp.CCmp") + .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.CCmp") .build(); diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java new file mode 100644 index 000000000..0158b31ec --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor1.java @@ -0,0 +1,25 @@ +package com.yomahub.liteflow.test.customWhenThreadPool; + +import cn.hutool.core.util.ObjectUtil; +import com.yomahub.liteflow.property.LiteflowConfig; +import com.yomahub.liteflow.thread.ExecutorBuilder; +import com.yomahub.liteflow.util.SpringAware; + +import java.util.concurrent.ExecutorService; + +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 buildDefaultExecutor( + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenQueueLimit(), + "customer-when-1-thead-"); + } +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java new file mode 100644 index 000000000..930105c6d --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor2.java @@ -0,0 +1,24 @@ +package com.yomahub.liteflow.test.customWhenThreadPool; + +import cn.hutool.core.util.ObjectUtil; +import com.yomahub.liteflow.property.LiteflowConfig; +import com.yomahub.liteflow.thread.ExecutorBuilder; +import com.yomahub.liteflow.util.SpringAware; + +import java.util.concurrent.ExecutorService; + +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 buildDefaultExecutor( + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenQueueLimit(), + "customer-when-2-thead-"); + } +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor3.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor3.java new file mode 100644 index 000000000..3519cfe20 --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomThreadExecutor3.java @@ -0,0 +1,24 @@ +package com.yomahub.liteflow.test.customWhenThreadPool; + +import cn.hutool.core.util.ObjectUtil; +import com.yomahub.liteflow.property.LiteflowConfig; +import com.yomahub.liteflow.thread.ExecutorBuilder; +import com.yomahub.liteflow.util.SpringAware; + +import java.util.concurrent.ExecutorService; + +public class CustomThreadExecutor3 implements ExecutorBuilder { + @Override + public ExecutorService buildExecutor() { + LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class); + //只有在非spring的场景下liteflowConfig才会为null + if (ObjectUtil.isNull(liteflowConfig)) { + liteflowConfig = new LiteflowConfig(); + } + return buildDefaultExecutor( + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenMaxWorkers(), + liteflowConfig.getWhenQueueLimit(), + "customer-when-3-thead-"); + } +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java new file mode 100644 index 000000000..b959fb2ca --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java @@ -0,0 +1,80 @@ +package com.yomahub.liteflow.test.customWhenThreadPool; + +import com.yomahub.liteflow.builder.LiteFlowChainBuilder; +import com.yomahub.liteflow.builder.LiteFlowConditionBuilder; +import com.yomahub.liteflow.builder.LiteFlowNodeBuilder; +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.DefaultSlot; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.enums.NodeTypeEnum; +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.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +/** + * springboot环境下异步线程超时日志打印测试 + * + * @author Bryan.Zhang + * @since 2.6.4 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration("classpath:/customWhenThreadPool/application.xml") +public class CustomWhenThreadPoolSpringbootTest extends BaseTest { + + private final Logger log = LoggerFactory.getLogger(this.getClass()); + + @Resource + private FlowExecutor flowExecutor; + + @Test + public void testCustomThreadPool() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain", "arg"); + Assert.assertTrue(response.isSuccess()); + Assert.assertTrue(response.getSlot().getData("threadName").toString().startsWith("lf-when-thead")); + + LiteflowResponse response1 = flowExecutor.execute2Resp("chain1", "arg"); + Assert.assertTrue(response1.isSuccess()); + Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead")); + + LiteflowResponse response2 = flowExecutor.execute2Resp("chain2", "arg"); + Assert.assertTrue(response2.isSuccess()); + Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-2-thead")); + + + // 使用build模式构建chain测试when条件的多线程 + LiteFlowNodeBuilder.createNode().setId("a") + .setName("组件A") + .setType(NodeTypeEnum.COMMON) + .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.ACmp") + .build(); + LiteFlowNodeBuilder.createNode().setId("b") + .setName("组件B") + .setType(NodeTypeEnum.COMMON) + .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.BCmp") + .build(); + LiteFlowNodeBuilder.createNode().setId("c") + .setName("组件C") + .setType(NodeTypeEnum.COMMON) + .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.CCmp") + .build(); + + + LiteFlowChainBuilder.createChain().setChainName("chain3").setCondition( + LiteFlowConditionBuilder + .createWhenCondition() + .setThreadExecutorClass(CustomThreadExecutor3.class.getName()) + .setValue("a,b,c,d") + .build() + ).build(); + LiteflowResponse response3 = flowExecutor.execute2Resp("chain3", "arg"); + Assert.assertTrue(response3.isSuccess()); + Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("customer-when-3-thead")); + } +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ACmp.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ACmp.java new file mode 100644 index 000000000..f47c972da --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ACmp.java @@ -0,0 +1,20 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/BCmp.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/BCmp.java new file mode 100644 index 000000000..69b1a3845 --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/BCmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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!"); + } + +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java new file mode 100644 index 000000000..df355c4b6 --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/CCmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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() { + this.getSlot().setData("threadName", Thread.currentThread().getName()); + System.out.println("CCmp executed!"); + } + +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java new file mode 100644 index 000000000..a67ec4b9b --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/DCmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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() { + this.getSlot().setData("threadName", Thread.currentThread().getName()); + System.out.println("DCmp executed!"); + } + +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java new file mode 100644 index 000000000..929e41767 --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/ECmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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() { + this.getSlot().setData("threadName", Thread.currentThread().getName()); + System.out.println("ECmp executed!"); + } + +} diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/FCmp.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/FCmp.java new file mode 100644 index 000000000..f4285a637 --- /dev/null +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/cmp/FCmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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("f") +public class FCmp extends NodeComponent { + + @Override + public void process() { + this.getSlot().setData("threadName", Thread.currentThread().getName()); + System.out.println("FCmp executed!"); + } + +} diff --git a/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/application.xml b/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/application.xml new file mode 100644 index 000000000..66777a653 --- /dev/null +++ b/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/application.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/flow.xml b/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/flow.xml new file mode 100644 index 000000000..c1fc18751 --- /dev/null +++ b/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/flow.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file From 87cae4a23e06b563e0aaff3790728bfc7fab9738 Mon Sep 17 00:00:00 2001 From: dq-open-cloud <466608943@qq.com> Date: Mon, 24 Jan 2022 10:05:53 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/thread/ExecutorHelper.java | 52 +++++++++-------- .../CustomWhenThreadPoolSpringbootTest.java | 56 +++++++------------ .../resources/customWhenThreadPool/flow.xml | 2 +- ...va => CustomWhenThreadPoolSpringTest.java} | 54 ++++++++---------- .../resources/customWhenThreadPool/flow.xml | 2 +- 5 files changed, 73 insertions(+), 93 deletions(-) rename liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/{CustomWhenThreadPoolSpringbootTest.java => CustomWhenThreadPoolSpringTest.java} (53%) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java index a269d81dc..e1249a820 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/ExecutorHelper.java @@ -38,7 +38,7 @@ public class ExecutorHelper { * 此处使用Map缓存线程池信息 * key - 线程池构建者的Class全类名 * value - 线程池对象 - * */ + */ private final Map executorServiceMap; private ExecutorHelper() { @@ -85,12 +85,14 @@ public class ExecutorHelper { } } - /** 构建全局默认线程池 */ + /** + * 构建全局默认线程池 + */ public ExecutorService buildExecutor() { if (ObjectUtil.isNull(executorService)) { LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class); assert liteflowConfig != null; - executorService = buildExecutor(liteflowConfig.getThreadExecutorClass()); + executorService = getExecutorBuilder(liteflowConfig.getThreadExecutorClass()).buildExecutor(); } return executorService; } @@ -99,27 +101,23 @@ public class ExecutorHelper { *

* 构建线程池执行器 - 支持多个when公用一个线程池 *

- * @author sikadai - * @date 2022/1/21 23:00 + * * @param threadExecutorClass : 线程池构建者的Class全类名 * @return java.util.concurrent.ExecutorService + * @author sikadai + * @date 2022/1/21 23:00 */ 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()); + 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; } } @@ -128,13 +126,19 @@ public class ExecutorHelper { * 根据线程执行构建者Class类名获取ExecutorBuilder实例 *

* - * @author sikadai - * @date 2022/1/21 23:04 * @param threadExecutorClass * @return com.yomahub.liteflow.thread.ExecutorBuilder + * @author sikadai + * @date 2022/1/21 23:04 */ - private ExecutorBuilder getExecutorBuilder(String threadExecutorClass) throws Exception { - return (ExecutorBuilder) Class.forName(threadExecutorClass).newInstance(); + private ExecutorBuilder getExecutorBuilder(String threadExecutorClass) { + try { + return (ExecutorBuilder) Class.forName(threadExecutorClass).newInstance(); + } catch (Exception e) { + LOG.error(e.getMessage(), e); + throw new ThreadExecutorServiceCreateException(e.getMessage()); + } + } public ExecutorService getExecutorService() { diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java index e258ca995..b21eed76f 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java @@ -1,12 +1,8 @@ package com.yomahub.liteflow.test.customWhenThreadPool; -import com.yomahub.liteflow.builder.LiteFlowChainBuilder; -import com.yomahub.liteflow.builder.LiteFlowConditionBuilder; -import com.yomahub.liteflow.builder.LiteFlowNodeBuilder; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.entity.data.DefaultSlot; import com.yomahub.liteflow.entity.data.LiteflowResponse; -import com.yomahub.liteflow.enums.NodeTypeEnum; import com.yomahub.liteflow.test.BaseTest; import org.junit.Assert; import org.junit.Test; @@ -39,48 +35,38 @@ public class CustomWhenThreadPoolSpringbootTest extends BaseTest { @Resource private FlowExecutor flowExecutor; + /** + * 测试全局线程池配置 + */ @Test - public void testCustomThreadPool() throws Exception { + public void testGlobalThreadPool() { LiteflowResponse response = flowExecutor.execute2Resp("chain", "arg"); Assert.assertTrue(response.isSuccess()); Assert.assertTrue(response.getSlot().getData("threadName").toString().startsWith("lf-when-thead")); + } + /** + * 测试全局和when上自定义线程池-优先以when上为准 + */ + @Test + public void testGlobalAndCustomWhenThreadPool() { LiteflowResponse response1 = flowExecutor.execute2Resp("chain1", "arg"); Assert.assertTrue(response1.isSuccess()); Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead")); + } + + /** + * when配置的线程池可以共用 + */ + @Test + public void testCustomWhenThreadPool() { + // 使用when - thread1 + testGlobalAndCustomWhenThreadPool(); + // chain配置同一个thead1 LiteflowResponse response2 = flowExecutor.execute2Resp("chain2", "arg"); Assert.assertTrue(response2.isSuccess()); - Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-2-thead")); + Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead")); - - // 使用build模式构建chain测试when条件的多线程 - LiteFlowNodeBuilder.createNode().setId("a") - .setName("组件A") - .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.ACmp") - .build(); - LiteFlowNodeBuilder.createNode().setId("b") - .setName("组件B") - .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.BCmp") - .build(); - LiteFlowNodeBuilder.createNode().setId("c") - .setName("组件C") - .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.CCmp") - .build(); - - - LiteFlowChainBuilder.createChain().setChainName("chain3").setCondition( - LiteFlowConditionBuilder - .createWhenCondition() - .setThreadExecutorClass(CustomThreadExecutor3.class.getName()) - .setValue("a,b,c,d") - .build() - ).build(); - LiteflowResponse response3 = flowExecutor.execute2Resp("chain3", "arg"); - Assert.assertTrue(response3.isSuccess()); - Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("customer-when-3-thead")); } } diff --git a/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml b/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml index c1fc18751..c6f199ddb 100644 --- a/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml +++ b/liteflow-testcase-springboot/src/test/resources/customWhenThreadPool/flow.xml @@ -7,6 +7,6 @@ - + \ No newline at end of file diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringTest.java similarity index 53% rename from liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java rename to liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringTest.java index b959fb2ca..eef679734 100644 --- a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringbootTest.java +++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringTest.java @@ -26,55 +26,45 @@ import javax.annotation.Resource; */ @RunWith(SpringRunner.class) @ContextConfiguration("classpath:/customWhenThreadPool/application.xml") -public class CustomWhenThreadPoolSpringbootTest extends BaseTest { +public class CustomWhenThreadPoolSpringTest extends BaseTest { private final Logger log = LoggerFactory.getLogger(this.getClass()); @Resource private FlowExecutor flowExecutor; + /** + * 测试全局线程池配置 + */ @Test - public void testCustomThreadPool() throws Exception { + public void testGlobalThreadPool() { LiteflowResponse response = flowExecutor.execute2Resp("chain", "arg"); Assert.assertTrue(response.isSuccess()); Assert.assertTrue(response.getSlot().getData("threadName").toString().startsWith("lf-when-thead")); + } + /** + * 测试全局和when上自定义线程池-优先以when上为准 + */ + @Test + public void testGlobalAndCustomWhenThreadPool() { LiteflowResponse response1 = flowExecutor.execute2Resp("chain1", "arg"); Assert.assertTrue(response1.isSuccess()); Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead")); + } + + /** + * when配置的线程池可以共用 + */ + @Test + public void testCustomWhenThreadPool() { + // 使用when - thread1 + testGlobalAndCustomWhenThreadPool(); + // chain配置同一个thead1 LiteflowResponse response2 = flowExecutor.execute2Resp("chain2", "arg"); Assert.assertTrue(response2.isSuccess()); - Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-2-thead")); + Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead")); - - // 使用build模式构建chain测试when条件的多线程 - LiteFlowNodeBuilder.createNode().setId("a") - .setName("组件A") - .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.ACmp") - .build(); - LiteFlowNodeBuilder.createNode().setId("b") - .setName("组件B") - .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.BCmp") - .build(); - LiteFlowNodeBuilder.createNode().setId("c") - .setName("组件C") - .setType(NodeTypeEnum.COMMON) - .setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.CCmp") - .build(); - - - LiteFlowChainBuilder.createChain().setChainName("chain3").setCondition( - LiteFlowConditionBuilder - .createWhenCondition() - .setThreadExecutorClass(CustomThreadExecutor3.class.getName()) - .setValue("a,b,c,d") - .build() - ).build(); - LiteflowResponse response3 = flowExecutor.execute2Resp("chain3", "arg"); - Assert.assertTrue(response3.isSuccess()); - Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("customer-when-3-thead")); } } diff --git a/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/flow.xml b/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/flow.xml index c1fc18751..c6f199ddb 100644 --- a/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/flow.xml +++ b/liteflow-testcase-springnative/src/test/resources/customWhenThreadPool/flow.xml @@ -7,6 +7,6 @@ - + \ No newline at end of file From b2ef9d60408305e4560f969c9b74582d8374d8d9 Mon Sep 17 00:00:00 2001 From: dq-open-cloud <466608943@qq.com> Date: Mon, 24 Jan 2022 10:23:38 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/privateDelivery/cmp/ACmp.java | 21 ++++----------- .../test/privateDelivery/cmp/BCmp.java | 26 +++++-------------- .../test/resources/privateDelivery/flow.xml | 15 ++++++++--- .../CustomWhenThreadPoolSpringTest.java | 4 --- 4 files changed, 22 insertions(+), 44 deletions(-) diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/ACmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/ACmp.java index 88aa64dd8..89802080b 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/ACmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/ACmp.java @@ -8,36 +8,25 @@ package com.yomahub.liteflow.test.privateDelivery.cmp; import com.yomahub.liteflow.annotation.LiteflowComponent; -import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.core.NodeComponent; -import com.yomahub.liteflow.entity.data.DefaultSlot; import com.yomahub.liteflow.entity.data.Slot; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashSet; -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; +@Component("a") @LiteflowComponent("a") public class ACmp extends NodeComponent { - @Autowired - private FlowExecutor flowExecutor; + @Override public void process() { System.out.println("ACmp executed!"); Slot slot = getSlot(); slot.setData("testSet", new HashSet<>()); - try { - Queue queue = new ConcurrentLinkedQueue<>(); - for (int i = 1; i <= 100; i++) { - queue.add(i); - } - flowExecutor.execute2Resp("chain2", queue); - - }catch (Exception e) { - e.printStackTrace(); + for (int i = 0; i < 100; i++) { + this.sendPrivateDeliveryData("b",i+1); } } } + diff --git a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/BCmp.java b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/BCmp.java index d3c9b4997..4e6023b90 100644 --- a/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/BCmp.java +++ b/liteflow-testcase-springboot/src/test/java/com/yomahub/liteflow/test/privateDelivery/cmp/BCmp.java @@ -1,42 +1,28 @@ /** *

Title: liteflow

*

Description: 轻量级的组件式流程框架

- * * @author Bryan.Zhang * @email weenyc31@163.com * @Date 2020/4/1 */ package com.yomahub.liteflow.test.privateDelivery.cmp; -import cn.hutool.core.collection.CollUtil; import com.yomahub.liteflow.annotation.LiteflowComponent; import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; -import java.util.Queue; import java.util.Set; +@Component("b") @LiteflowComponent("b") public class BCmp extends NodeComponent { - @Override - public boolean isAccess() { - Queue values = this.getSlot().getRequestData(); - System.out.println("BCmp executed! values.size" + values.size()); - if (CollUtil.isEmpty(values)) { - return false; - } - Integer value = values.poll(); - if (value == null) { - return false; - } - this.sendPrivateDeliveryData(this.getNodeId(), value); - return true; - } - @Override public void process() { - Integer value = getPrivateDeliveryData(); - System.out.println("BCmp executed!" + value); + System.out.println("BCmp executed!"); + Integer value = this.getPrivateDeliveryData(); + Set testSet = this.getSlot().getData("testSet"); + testSet.add(value); } } + diff --git a/liteflow-testcase-springboot/src/test/resources/privateDelivery/flow.xml b/liteflow-testcase-springboot/src/test/resources/privateDelivery/flow.xml index 37ce81f4a..bf7146217 100644 --- a/liteflow-testcase-springboot/src/test/resources/privateDelivery/flow.xml +++ b/liteflow-testcase-springboot/src/test/resources/privateDelivery/flow.xml @@ -2,10 +2,17 @@ + + + + + + + + + + + - -
-        
-    
 
\ No newline at end of file
diff --git a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringTest.java b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringTest.java
index eef679734..b6cd16e45 100644
--- a/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringTest.java
+++ b/liteflow-testcase-springnative/src/test/java/com/yomahub/liteflow/test/customWhenThreadPool/CustomWhenThreadPoolSpringTest.java
@@ -1,12 +1,8 @@
 package com.yomahub.liteflow.test.customWhenThreadPool;
 
-import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
-import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
-import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
 import com.yomahub.liteflow.core.FlowExecutor;
 import com.yomahub.liteflow.entity.data.DefaultSlot;
 import com.yomahub.liteflow.entity.data.LiteflowResponse;
-import com.yomahub.liteflow.enums.NodeTypeEnum;
 import com.yomahub.liteflow.test.BaseTest;
 import org.junit.Assert;
 import org.junit.Test;