mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 04:02:09 +08:00
feature #IAY66T 生命周期实现为单例
This commit is contained in:
@@ -662,8 +662,8 @@ public class FlowExecutor {
|
||||
boolean exist = lifeCycleList.stream()
|
||||
.anyMatch(lifeCycle -> lifeCycle instanceof RuleCacheLifeCycle);
|
||||
if (!exist) {
|
||||
RuleCacheLifeCycle ruleCacheLifeCycle = new RuleCacheLifeCycle(capacity);
|
||||
LifeCycleHolder.addLifeCycle(ruleCacheLifeCycle);
|
||||
RuleCacheLifeCycle.initIfAbsent(capacity);
|
||||
LifeCycleHolder.addLifeCycle(RuleCacheLifeCycle.getLifeCycle());
|
||||
}
|
||||
// 执行时才解析chain
|
||||
liteflowConfig.setParseMode(ParseModeEnum.PARSE_ONE_ON_FIRST_EXEC);
|
||||
|
||||
@@ -27,7 +27,12 @@ public class RuleCacheLifeCycle implements PostProcessChainExecuteLifeCycle {
|
||||
*/
|
||||
private final Cache<String, ChainState> cache;
|
||||
|
||||
public RuleCacheLifeCycle(int capacity) {
|
||||
/**
|
||||
* 实例
|
||||
*/
|
||||
private static RuleCacheLifeCycle INSTANCE;
|
||||
|
||||
private RuleCacheLifeCycle(int capacity) {
|
||||
this.cache = Caffeine.newBuilder()
|
||||
.maximumSize(capacity)
|
||||
.evictionListener(new ChainRemovalListener())
|
||||
@@ -146,4 +151,22 @@ public class RuleCacheLifeCycle implements PostProcessChainExecuteLifeCycle {
|
||||
chain.setCompiled(false);
|
||||
chain.setConditionList(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化生命周期实例
|
||||
* @param capacity 缓存容量
|
||||
*/
|
||||
public synchronized static void initIfAbsent(int capacity) {
|
||||
if (ObjectUtil.isNull(INSTANCE)) {
|
||||
INSTANCE = new RuleCacheLifeCycle(capacity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取生命周期实例
|
||||
* @return lifeCycle
|
||||
*/
|
||||
public static RuleCacheLifeCycle getLifeCycle() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,15 +222,7 @@ public class RuleCacheTest extends BaseTest {
|
||||
}
|
||||
|
||||
public Cache<String, RuleCacheLifeCycle.ChainState> getCache() {
|
||||
List<PostProcessChainExecuteLifeCycle> lifeCycleList
|
||||
= LifeCycleHolder.getPostProcessChainExecuteLifeCycleList();
|
||||
for (PostProcessChainExecuteLifeCycle lifeCycle : lifeCycleList) {
|
||||
if (lifeCycle.getClass().equals(RuleCacheLifeCycle.class)) {
|
||||
RuleCacheLifeCycle ruleCacheLifeCycle = (RuleCacheLifeCycle) lifeCycle;
|
||||
return ruleCacheLifeCycle.getCache();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return RuleCacheLifeCycle.getLifeCycle().getCache();
|
||||
}
|
||||
|
||||
// 获得淘汰的chain,传入淘汰前的chain集合
|
||||
|
||||
@@ -215,15 +215,7 @@ public class RuleCacheSolonTest extends BaseTest {
|
||||
}
|
||||
|
||||
public Cache<String, RuleCacheLifeCycle.ChainState> getCache() {
|
||||
List<PostProcessChainExecuteLifeCycle> lifeCycleList
|
||||
= LifeCycleHolder.getPostProcessChainExecuteLifeCycleList();
|
||||
for (PostProcessChainExecuteLifeCycle lifeCycle : lifeCycleList) {
|
||||
if (lifeCycle.getClass().equals(RuleCacheLifeCycle.class)) {
|
||||
RuleCacheLifeCycle ruleCacheLifeCycle = (RuleCacheLifeCycle) lifeCycle;
|
||||
return ruleCacheLifeCycle.getCache();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return RuleCacheLifeCycle.getLifeCycle().getCache();
|
||||
}
|
||||
|
||||
// 获得淘汰的chain,传入淘汰前的chain集合
|
||||
|
||||
@@ -218,15 +218,7 @@ public class RuleCacheSpringbootTest extends BaseTest {
|
||||
}
|
||||
|
||||
public Cache<String, RuleCacheLifeCycle.ChainState> getCache() {
|
||||
List<PostProcessChainExecuteLifeCycle> lifeCycleList
|
||||
= LifeCycleHolder.getPostProcessChainExecuteLifeCycleList();
|
||||
for (PostProcessChainExecuteLifeCycle lifeCycle : lifeCycleList) {
|
||||
if (lifeCycle.getClass().equals(RuleCacheLifeCycle.class)) {
|
||||
RuleCacheLifeCycle ruleCacheLifeCycle = (RuleCacheLifeCycle) lifeCycle;
|
||||
return ruleCacheLifeCycle.getCache();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return RuleCacheLifeCycle.getLifeCycle().getCache();
|
||||
}
|
||||
|
||||
// 获得淘汰的chain,传入淘汰前的chain集合
|
||||
|
||||
@@ -215,15 +215,7 @@ public class RuleCacheSpringTest extends BaseTest {
|
||||
}
|
||||
|
||||
public Cache<String, RuleCacheLifeCycle.ChainState> getCache() {
|
||||
List<PostProcessChainExecuteLifeCycle> lifeCycleList
|
||||
= LifeCycleHolder.getPostProcessChainExecuteLifeCycleList();
|
||||
for (PostProcessChainExecuteLifeCycle lifeCycle : lifeCycleList) {
|
||||
if (lifeCycle.getClass().equals(RuleCacheLifeCycle.class)) {
|
||||
RuleCacheLifeCycle ruleCacheLifeCycle = (RuleCacheLifeCycle) lifeCycle;
|
||||
return ruleCacheLifeCycle.getCache();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return RuleCacheLifeCycle.getLifeCycle().getCache();
|
||||
}
|
||||
|
||||
// 获得淘汰的chain,传入淘汰前的chain集合
|
||||
|
||||
Reference in New Issue
Block a user