把线程变量移到Node类中

This commit is contained in:
everywhere.z 2024-02-15 23:32:39 +08:00
parent dd776b8c62
commit 12c37cd4ea
5 changed files with 55 additions and 54 deletions

View File

@ -117,7 +117,7 @@ public class OperatorHelper {
if (clazz.isAssignableFrom(object.getClass())) {
if (object instanceof Node) {
Node node = (Node) object;
return (T) node.copy();
return (T) node.clone();
}
else {
return (T) object;

View File

@ -7,13 +7,10 @@
*/
package com.yomahub.liteflow.core;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.yomahub.liteflow.exception.ChainEndException;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.flow.executor.NodeExecutor;
@ -23,28 +20,21 @@ import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import com.yomahub.liteflow.spi.holder.CmpAroundAspectHolder;
import com.yomahub.liteflow.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yomahub.liteflow.flow.entity.CmpStep;
import com.yomahub.liteflow.enums.CmpStepTypeEnum;
import com.yomahub.liteflow.slot.DataBus;
import com.yomahub.liteflow.slot.Slot;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.monitor.CompStatistics;
import com.yomahub.liteflow.monitor.MonitorBus;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Deque;
import java.util.Map;
import java.util.function.Predicate;
/**
* 普通组件抽象类
*
* @author Bryan.Zhang
*/
public abstract class NodeComponent {
public abstract class NodeComponent{
private final LFLog LOG = LFLoggerManager.getLogger(this.getClass());
@ -77,16 +67,11 @@ public abstract class NodeComponent {
private final TransmittableThreadLocal<Node> refNodeTL = new TransmittableThreadLocal<>();
/**
******************* 以下的属性为线程附加属性******************** 线程属性是指每一个request的值都是不一样的
******************* 以下的属性为线程附加属性********************
* 线程属性是指每一个request的值都是不一样的
* 这里NodeComponent是单例所以要用ThreadLocal来修饰
*/
// 当前slot的index
private final TransmittableThreadLocal<Integer> slotIndexTL = new TransmittableThreadLocal<>();
// 是否结束整个流程这个只对串行流程有效并行流程无效
private final TransmittableThreadLocal<Boolean> isEndTL = new TransmittableThreadLocal<>();
public NodeComponent() {
// 反射判断是否重写了rollback方法
Class<?> clazz = this.getClass();
@ -237,39 +222,25 @@ public abstract class NodeComponent {
// 是否结束整个流程(不往下继续执行)
public boolean isEnd() {
Boolean isEnd = isEndTL.get();
Boolean isEnd = this.refNodeTL.get().getIsEnd();
if (ObjectUtil.isNull(isEnd)) {
return false;
}
else {
return isEndTL.get();
}else {
return isEnd;
}
}
// 设置是否结束整个流程
public void setIsEnd(boolean isEnd) {
this.isEndTL.set(isEnd);
}
public void removeIsEnd() {
this.isEndTL.remove();
}
public NodeComponent setSlotIndex(Integer slotIndex) {
this.slotIndexTL.set(slotIndex);
return this;
this.refNodeTL.get().setIsEnd(isEnd);
}
public Integer getSlotIndex() {
return this.slotIndexTL.get();
}
public void removeSlotIndex() {
this.slotIndexTL.remove();
return this.refNodeTL.get().getSlotIndex();
}
public Slot getSlot() {
return DataBus.getSlot(this.slotIndexTL.get());
return DataBus.getSlot(this.getSlotIndex());
}
public <T> T getFirstContextBean() {

View File

@ -64,7 +64,7 @@ public class FallbackNode extends Node {
this.getCurrChainId()));
}
// 使用 node 的副本
this.fallbackNode = node.copy();
this.fallbackNode = node.clone();
}
private Node findFallbackNode(Condition condition) {
@ -168,7 +168,7 @@ public class FallbackNode extends Node {
}
@Override
public Node copy() {
public Node clone() {
// 代理节点不复制
return this;
}

View File

@ -59,10 +59,18 @@ public class Node implements Executable, Cloneable, Rollbackable{
// node isAccess 结果主要用于 WhenCondition 的提前 isAccess 判断避免 isAccess 方法重复执行
private TransmittableThreadLocal<Boolean> accessResult = new TransmittableThreadLocal<>();
// 循环下标
private TransmittableThreadLocal<Integer> loopIndexTL = new TransmittableThreadLocal<>();
// 迭代对象
private TransmittableThreadLocal<Object> currLoopObject = new TransmittableThreadLocal<>();
// 当前slot的index
private TransmittableThreadLocal<Integer> slotIndexTL = new TransmittableThreadLocal<>();
// 是否结束整个流程这个只对串行流程有效并行流程无效
private TransmittableThreadLocal<Boolean> isEndTL = new TransmittableThreadLocal<>();
public Node() {
}
@ -129,7 +137,7 @@ public class Node implements Executable, Cloneable, Rollbackable{
try {
// 把线程属性赋值给组件对象
instance.setSlotIndex(slotIndex);
this.setSlotIndex(slotIndex);
instance.setRefNode(this);
// 判断是否可执行所以isAccess经常作为一个组件进入的实际判断要素用作检查slot里的参数的完备性
@ -172,9 +180,9 @@ public class Node implements Executable, Cloneable, Rollbackable{
}
finally {
// 移除threadLocal里的信息
instance.removeSlotIndex();
instance.removeIsEnd();
instance.removeRefNode();
removeSlotIndex();
removeIsEnd();
removeLoopIndex();
removeAccessResult();
}
@ -187,7 +195,7 @@ public class Node implements Executable, Cloneable, Rollbackable{
Slot slot = DataBus.getSlot(slotIndex);
try {
// 把线程属性赋值给组件对象
instance.setSlotIndex(slotIndex);
this.setSlotIndex(slotIndex);
instance.setRefNode(this);
instance.doRollback();
}
@ -197,7 +205,7 @@ public class Node implements Executable, Cloneable, Rollbackable{
}
finally {
// 移除threadLocal里的信息
instance.removeSlotIndex();
this.removeSlotIndex();
instance.removeRefNode();
}
}
@ -209,7 +217,7 @@ public class Node implements Executable, Cloneable, Rollbackable{
@Override
public boolean isAccess(Integer slotIndex) throws Exception {
// 把线程属性赋值给组件对象
instance.setSlotIndex(slotIndex);
this.setSlotIndex(slotIndex);
instance.setRefNode(this);
return instance.isAccess();
}
@ -289,6 +297,30 @@ public class Node implements Executable, Cloneable, Rollbackable{
this.currLoopObject.remove();
}
public Integer getSlotIndex(){
return this.slotIndexTL.get();
}
public void setSlotIndex(Integer slotIndex){
this.slotIndexTL.set(slotIndex);
}
public void removeSlotIndex(){
this.slotIndexTL.remove();
}
public Boolean getIsEnd(){
return this.isEndTL.get();
}
public void setIsEnd(Boolean isEnd){
this.isEndTL.set(isEnd);
}
public void removeIsEnd(){
this.isEndTL.remove();
}
public String getLanguage() {
return language;
}
@ -303,15 +335,13 @@ public class Node implements Executable, Cloneable, Rollbackable{
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Node copy() throws Exception {
Node node = (Node)this.clone();
public Node clone() throws CloneNotSupportedException {
Node node = (Node)super.clone();
node.loopIndexTL = new TransmittableThreadLocal<>();
node.currLoopObject = new TransmittableThreadLocal<>();
node.accessResult = new TransmittableThreadLocal<>();
node.slotIndexTL = new TransmittableThreadLocal<>();
node.isEndTL = new TransmittableThreadLocal<>();
return node;
}
}

View File

@ -2,7 +2,7 @@
<!DOCTYPE flow PUBLIC "liteflow" "liteflow.dtd">
<flow>
<chain name="chain1">
THEN(a,b,WHEN(c,d));
THEN(a,b,b,a);
</chain>
<chain name="chain2">