|
@@ -11,9 +11,11 @@ package com.yomahub.liteflow.thread;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.yomahub.liteflow.enums.ConditionTypeEnum;
|
|
|
import com.yomahub.liteflow.exception.ThreadExecutorServiceCreateException;
|
|
|
import com.yomahub.liteflow.flow.FlowBus;
|
|
|
import com.yomahub.liteflow.flow.element.Chain;
|
|
|
+import com.yomahub.liteflow.flow.element.Condition;
|
|
|
import com.yomahub.liteflow.flow.element.condition.LoopCondition;
|
|
|
import com.yomahub.liteflow.log.LFLog;
|
|
|
import com.yomahub.liteflow.log.LFLoggerManager;
|
|
@@ -21,6 +23,8 @@ import com.yomahub.liteflow.property.LiteflowConfig;
|
|
|
import com.yomahub.liteflow.property.LiteflowConfigGetter;
|
|
|
import com.yomahub.liteflow.slot.DataBus;
|
|
|
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
|
|
+import com.yomahub.liteflow.thread.ExecutorCondition.ExecutorCondition;
|
|
|
+import com.yomahub.liteflow.thread.ExecutorCondition.ExecutorConditionBuilder;
|
|
|
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
@@ -104,17 +108,17 @@ public class ExecutorHelper {
|
|
|
}
|
|
|
|
|
|
// 构建when线程池 - clazz和condition的hash值共同作为缓存key
|
|
|
- public ExecutorService buildWhenExecutorWithHash(String conditionHash) {
|
|
|
+ public ExecutorService buildWhenExecutorWithHash(String hash) {
|
|
|
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
|
|
- return buildWhenExecutorWithHash(liteflowConfig.getGlobalThreadPoolExecutorClass(), conditionHash);
|
|
|
+ return buildWhenExecutorWithHash(liteflowConfig.getGlobalThreadPoolExecutorClass(), hash);
|
|
|
}
|
|
|
|
|
|
// 构建when线程池 - clazz和condition的hash值共同作为缓存key
|
|
|
- public ExecutorService buildWhenExecutorWithHash(String clazz, String conditionHash) {
|
|
|
+ public ExecutorService buildWhenExecutorWithHash(String clazz, String hash) {
|
|
|
if (StrUtil.isBlank(clazz)) {
|
|
|
- return buildWhenExecutorWithHash(conditionHash);
|
|
|
+ return buildWhenExecutorWithHash(hash);
|
|
|
}
|
|
|
- return getExecutorService(clazz, conditionHash);
|
|
|
+ return getExecutorService(clazz, hash);
|
|
|
}
|
|
|
|
|
|
// 构建默认的FlowExecutor线程池,用于execute2Future方法
|
|
@@ -168,13 +172,13 @@ public class ExecutorHelper {
|
|
|
/**
|
|
|
* 根据线程执行构建者Class类名获取ExecutorService实例
|
|
|
*/
|
|
|
- private ExecutorService getExecutorService(String clazz, String conditionHash) {
|
|
|
+ private ExecutorService getExecutorService(String clazz, String hash) {
|
|
|
try {
|
|
|
String key;
|
|
|
- if (StrUtil.isBlank(conditionHash)){
|
|
|
+ if (StrUtil.isBlank(hash)) {
|
|
|
key = clazz;
|
|
|
}else{
|
|
|
- key = StrUtil.format("{}_{}", clazz, conditionHash);
|
|
|
+ key = StrUtil.format("{}_{}", clazz, hash);
|
|
|
}
|
|
|
|
|
|
ExecutorService executorServiceFromCache = executorServiceMap.get(key);
|
|
@@ -201,4 +205,45 @@ public class ExecutorHelper {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 构建执行器服务
|
|
|
+ *
|
|
|
+ * @param condition 条件对象(Loop或When条件)
|
|
|
+ * @param slotIndex 槽索引
|
|
|
+ * @param type condition类型
|
|
|
+ * @return ExecutorService
|
|
|
+ */
|
|
|
+ public ExecutorService buildExecutorService(Condition condition, Integer slotIndex, ConditionTypeEnum type) {
|
|
|
+ ExecutorService executor;
|
|
|
+ LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
|
|
+ String chainId = DataBus.getSlot(slotIndex).getChainId();
|
|
|
+ Chain chain = FlowBus.getChain(chainId);
|
|
|
+
|
|
|
+ // 构建条件判断对象
|
|
|
+ ExecutorCondition execCondition = ExecutorConditionBuilder.buildExecutorCondition(
|
|
|
+ condition,
|
|
|
+ chain,
|
|
|
+ liteflowConfig,
|
|
|
+ type
|
|
|
+ );
|
|
|
+
|
|
|
+ // 根据条件选择执行器
|
|
|
+ if (execCondition.isConditionLevel()) {
|
|
|
+ // condition层级线程池
|
|
|
+ executor = getExecutorService(execCondition.getConditionExecutorClass(),
|
|
|
+ String.valueOf(condition.hashCode()));
|
|
|
+
|
|
|
+ } else if (execCondition.isChainLevel()) {
|
|
|
+ // chain层级线程池
|
|
|
+ executor = getExecutorService(chain.getThreadPoolExecutorClass(),
|
|
|
+ String.valueOf(chain.hashCode()));
|
|
|
+ } else {
|
|
|
+ // 全局线程池
|
|
|
+ executor = getExecutorService(liteflowConfig.getGlobalThreadPoolExecutorClass());
|
|
|
+ }
|
|
|
+
|
|
|
+ return executor;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|