mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 04:02:09 +08:00
feature #IAY66T 完善代码
This commit is contained in:
@@ -661,7 +661,10 @@ public class FlowExecutor {
|
|||||||
boolean exist = lifeCycleList.stream()
|
boolean exist = lifeCycleList.stream()
|
||||||
.anyMatch(lifeCycle -> lifeCycle instanceof RuleCacheLifeCycle);
|
.anyMatch(lifeCycle -> lifeCycle instanceof RuleCacheLifeCycle);
|
||||||
if (!exist) {
|
if (!exist) {
|
||||||
RuleCacheLifeCycle.initIfAbsent(capacity);
|
boolean success = RuleCacheLifeCycle.initIfAbsent(capacity);
|
||||||
|
if (!success) {
|
||||||
|
throw new FlowExecutorNotInitException("Initialization of RuleCacheLifeCycle failed");
|
||||||
|
}
|
||||||
LifeCycleHolder.addLifeCycle(RuleCacheLifeCycle.getLifeCycle());
|
LifeCycleHolder.addLifeCycle(RuleCacheLifeCycle.getLifeCycle());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -679,7 +682,8 @@ public class FlowExecutor {
|
|||||||
int chainNum = FlowBus.getChainMap().size();
|
int chainNum = FlowBus.getChainMap().size();
|
||||||
double threshold = chainNum * 0.3;
|
double threshold = chainNum * 0.3;
|
||||||
if (capacity < threshold) {
|
if (capacity < threshold) {
|
||||||
LOG.warn("The rule cache capacity {} is too small, it is recommended to be greater than 30% of the number of chains", capacity);
|
LOG.warn("The rule cache capacity {} is too small, the current total number of chains is {}, "
|
||||||
|
+"it is recommended to be greater than 30% of the number of chains", capacity, chainNum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ package com.yomahub.liteflow.flow.element;
|
|||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.BooleanUtil;
|
import cn.hutool.core.util.BooleanUtil;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
|
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
|
||||||
import com.yomahub.liteflow.common.ChainConstant;
|
import com.yomahub.liteflow.common.ChainConstant;
|
||||||
import com.yomahub.liteflow.enums.ExecuteableTypeEnum;
|
import com.yomahub.liteflow.enums.ExecuteableTypeEnum;
|
||||||
@@ -108,10 +109,10 @@ public class Chain implements Executable{
|
|||||||
// 因为在正式执行condition之前,this.conditionList有可能被其他线程置空
|
// 因为在正式执行condition之前,this.conditionList有可能被其他线程置空
|
||||||
// 比如,该chain在规则缓存中被淘汰
|
// 比如,该chain在规则缓存中被淘汰
|
||||||
List<Condition> conditionListRef = this.conditionList;
|
List<Condition> conditionListRef = this.conditionList;
|
||||||
// 但在编译后到拿到引用之前,this.conditionList可能已经被置空了
|
// 但在编译后到拿到引用之前,this.conditionList还是有可能已经被置空了
|
||||||
if (CollUtil.isEmpty(conditionListRef)) {
|
if (CollUtil.isEmpty(conditionListRef)) {
|
||||||
// 如果conditionListRef为空,
|
// 如果conditionListRef为空,
|
||||||
// 构建临时conditionList确保本次一定可以执行
|
// 尝试构建临时conditionList确保本次一定可以执行
|
||||||
conditionListRef = buildTemporaryConditionList();
|
conditionListRef = buildTemporaryConditionList();
|
||||||
}
|
}
|
||||||
Slot slot = DataBus.getSlot(slotIndex);
|
Slot slot = DataBus.getSlot(slotIndex);
|
||||||
@@ -248,6 +249,10 @@ public class Chain implements Executable{
|
|||||||
|
|
||||||
// 构建临时的ConditionList
|
// 构建临时的ConditionList
|
||||||
private List<Condition> buildTemporaryConditionList() {
|
private List<Condition> buildTemporaryConditionList() {
|
||||||
|
if (StrUtil.isBlank(el)) {
|
||||||
|
// 无法构建
|
||||||
|
throw new FlowSystemException("no conditionList in this chain[" + chainId + "]");
|
||||||
|
}
|
||||||
// 构建临时chain
|
// 构建临时chain
|
||||||
String tempChainId = chainId + "_temp_" + IdUtil.simpleUUID();
|
String tempChainId = chainId + "_temp_" + IdUtil.simpleUUID();
|
||||||
Chain tempChain = new Chain(tempChainId);
|
Chain tempChain = new Chain(tempChainId);
|
||||||
@@ -261,10 +266,6 @@ public class Chain implements Executable{
|
|||||||
// 移除临时chain
|
// 移除临时chain
|
||||||
FlowBus.removeChain(tempChainId);
|
FlowBus.removeChain(tempChainId);
|
||||||
|
|
||||||
if (CollUtil.isEmpty(tempConditionList)) {
|
|
||||||
throw new FlowSystemException("no conditionList in this chain[" + chainId + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 打印警告,可用于排查临时chain与已有chain重名(几乎不可能发生)而将已有chain覆盖的情况
|
// 打印警告,可用于排查临时chain与已有chain重名(几乎不可能发生)而将已有chain覆盖的情况
|
||||||
LOG.warn("The conditionList of chain[{}] is empty, " +
|
LOG.warn("The conditionList of chain[{}] is empty, " +
|
||||||
"temporarily using chain[{}] (now removed) to build it.", chainId, tempChainId);
|
"temporarily using chain[{}] (now removed) to build it.", chainId, tempChainId);
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public class RuleCacheLifeCycle implements PostProcessChainExecuteLifeCycle {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessAfterChainExecute(String chainId, Slot slot) {
|
public void postProcessAfterChainExecute(String chainId, Slot slot) {
|
||||||
// 不在缓存中、或出于非活跃状态,但未被清理
|
// 检测是否有不在缓存中、或处于非活跃状态而未被清理的 Chain
|
||||||
if (!isActive(chainId) && !isCleaned(chainId)) {
|
if (!isActive(chainId) && !isCleaned(chainId)) {
|
||||||
cleanChain(chainId);
|
cleanChain(chainId);
|
||||||
}
|
}
|
||||||
@@ -155,11 +155,14 @@ public class RuleCacheLifeCycle implements PostProcessChainExecuteLifeCycle {
|
|||||||
/**
|
/**
|
||||||
* 初始化生命周期实例
|
* 初始化生命周期实例
|
||||||
* @param capacity 缓存容量
|
* @param capacity 缓存容量
|
||||||
|
* @return 成功 true,失败返回 false
|
||||||
*/
|
*/
|
||||||
public synchronized static void initIfAbsent(int capacity) {
|
public synchronized static boolean initIfAbsent(int capacity) {
|
||||||
if (ObjectUtil.isNull(INSTANCE)) {
|
if (ObjectUtil.isNull(INSTANCE)) {
|
||||||
INSTANCE = new RuleCacheLifeCycle(capacity);
|
INSTANCE = new RuleCacheLifeCycle(capacity);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user