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 a269d81d..e1249a82 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 e258ca99..b21eed76 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 c1fc1875..c6f199dd 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 b959fb2c..eef67973 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 c1fc1875..c6f199dd 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