feature #IBLJ4A step中能够加入自定义的数据

This commit is contained in:
everywhere.z
2025-02-11 15:17:55 +08:00
parent 3bdbab8ace
commit dcb734ef08
10 changed files with 186 additions and 0 deletions

View File

@@ -137,6 +137,10 @@ public abstract class NodeComponent{
final long timeSpent = stopWatch.getTotalTimeMillis();
LOG.info("component[{}] finished in {} milliseconds", this.getDisplayName(), timeSpent);
// 步骤自定义数据设置
cmpStep.setStepData(this.getRefNode().getStepData());
// 结束时间设置
cmpStep.setEndTime(new Date());
// 往CmpStep中放入时间消耗信息
@@ -487,6 +491,10 @@ public abstract class NodeComponent{
return this.getRefNode().getPreNLoopObject(n);
}
public void setStepData(Object stepData) {
this.getRefNode().setStepData(stepData);
}
@Deprecated
public void invoke(String chainId, Object param) throws Exception {
FlowExecutorHolder.loadInstance().invoke(chainId, param, this.getSlotIndex());

View File

@@ -95,6 +95,9 @@ public class Node implements Executable, Cloneable, Rollbackable{
// isContinueOnError 结果
private TransmittableThreadLocal<Boolean> isContinueOnErrorResult = new TransmittableThreadLocal<>();
// step自定义数据
private ThreadLocal<Object> stepDataTL = new ThreadLocal<>();
public Node() {
}
@@ -243,6 +246,7 @@ public class Node implements Executable, Cloneable, Rollbackable{
removeLoopIndex();
removeAccessResult();
removeIsContinueOnErrorResult();
removeStepData();
}
}
@@ -539,6 +543,19 @@ public class Node implements Executable, Cloneable, Rollbackable{
isCloned = cloned;
}
public Object getStepData(){
return this.stepDataTL.get();
}
public void setStepData(Object stepData) {
this.stepDataTL.set(stepData);
}
public void removeStepData() {
this.stepDataTL.remove();
}
@Override
public Node clone() throws CloneNotSupportedException {
Node node = (Node)super.clone();
@@ -548,6 +565,7 @@ public class Node implements Executable, Cloneable, Rollbackable{
node.slotIndexTL = new TransmittableThreadLocal<>();
node.isEndTL = new TransmittableThreadLocal<>();
node.isContinueOnErrorResult = new TransmittableThreadLocal<>();
node.stepDataTL = new ThreadLocal<>();
node.lock4LoopIndex = new ReentrantLock();
node.lock4LoopObj = new ReentrantLock();
node.bindDataMap = new HashMap<>();

View File

@@ -56,6 +56,9 @@ public class CmpStep {
// 当前执行的node
private Node refNode;
// 自定义步骤数据
private Object stepData;
public CmpStep(String nodeId, String nodeName, CmpStepTypeEnum stepType, String instanceId) {
this.nodeId = nodeId;
@@ -262,4 +265,12 @@ public class CmpStep {
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Object getStepData() {
return stepData;
}
public void setStepData(Object stepData) {
this.stepData = stepData;
}
}

View File

@@ -0,0 +1,53 @@
package com.yomahub.liteflow.test.stepData;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.flow.entity.CmpStep;
import com.yomahub.liteflow.slot.DefaultContext;
import com.yomahub.liteflow.test.BaseTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;
import javax.annotation.Resource;
import java.util.function.Consumer;
/**
* springboot环境EL常规的例子测试
*
* @author Bryan.Zhang
*/
@TestPropertySource(value = "classpath:/stepData/application.properties")
@SpringBootTest(classes = StepDataSpringbootTest.class)
@EnableAutoConfiguration
@ComponentScan({ "com.yomahub.liteflow.test.stepData.cmp" })
public class StepDataSpringbootTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
// 测试step data,每个step都不一样的数据
@Test
public void testStepData1() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assertions.assertTrue(response.isSuccess());
response.getExecuteStepQueue().forEach(
cmpStep -> Assertions.assertEquals(StrUtil.format("step_{}", cmpStep.getNodeId()), cmpStep.getStepData())
);
}
// 测试step data即便是2个相同的节点step data也可以不一样
@Test
public void testStepData2() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
final Object[] data = {null};
response.getExecuteStepQueue().forEach(cmpStep -> {
Assertions.assertNotEquals(data[0], cmpStep.getStepData());
data[0] = cmpStep.getStepData();
});
}
}

View File

@@ -0,0 +1,21 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.stepData.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
this.setStepData("step_a");
System.out.println("ACmp executed!");
}
}

View File

@@ -0,0 +1,22 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.stepData.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
this.setStepData("step_b");
System.out.println("BCmp executed!");
}
}

View File

@@ -0,0 +1,21 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.stepData.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
this.setStepData("step_c");
System.out.println("CCmp executed!");
}
}

View File

@@ -0,0 +1,20 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.stepData.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("d")
public class DCmp extends NodeComponent {
@Override
public void process() {
this.setStepData(System.nanoTime());
}
}

View File

@@ -0,0 +1 @@
liteflow.rule-source=stepData/flow.el.xml

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE flow PUBLIC "liteflow" "liteflow.dtd">
<flow>
<chain name="chain1">
THEN(a,b,c);
</chain>
<chain name="chain2">
THEN(d,d);
</chain>
</flow>