feature #IBI6A3 新的基于原生态形式的javax插件

This commit is contained in:
everywhere.z
2025-01-21 12:09:14 +08:00
parent 5805a1e539
commit b8ec5fadf9
7 changed files with 327 additions and 9 deletions

View File

@@ -193,15 +193,10 @@ public class Node implements Executable, Cloneable, Rollbackable{
String errorInfo = StrUtil.format("[{}] lead the chain to end", instance.getDisplayName());
throw new ChainEndException(errorInfo);
}
}
catch (ChainEndException e) {
throw e;
}
catch (Exception e) {
}catch (Exception e) {
// 如果组件覆盖了isEnd方法或者在在逻辑中主要调用了setEnd(true)的话,流程就会立马结束
if (instance.isEnd()) {
String errorInfo = StrUtil.format("[{}] lead the chain to end", instance.getDisplayName());
throw new ChainEndException(errorInfo);
if (e instanceof ChainEndException) {
throw e;
}
// 如果组件覆盖了isContinueOnError方法返回为true那即便出了异常也会继续流程
else if (getIsContinueOnErrorResult() || instance.isContinueOnError()) {

View File

@@ -0,0 +1,95 @@
package com.yomahub.liteflow.test.script.javaxpro.override;
import cn.hutool.core.util.BooleanUtil;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
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.junit.jupiter.api.extension.ExtendWith;
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 org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.annotation.Resource;
@ExtendWith(SpringExtension.class)
@TestPropertySource(value = "classpath:/override/application.properties")
@SpringBootTest(classes = ScriptJavaxProOverrideELTest.class)
@EnableAutoConfiguration
@ComponentScan({ "com.yomahub.liteflow.test.script.javaxpro.override.cmp" })
public class ScriptJavaxProOverrideELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
//覆盖isAccess
@Test
public void testJavaxPro1() {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", 3, DefaultContext.class);
Assertions.assertEquals("a", response.getExecuteStepStr());
}
//覆盖isContinueOnError
@Test
public void testJavaxPro2() {
LiteflowResponse response = flowExecutor.execute2Resp("chain2", null, DefaultContext.class);
Assertions.assertEquals("s2==>a", response.getExecuteStepStrWithoutTime());
}
//设置setIsEnd(true)
@Test
public void testJavaxPro3() {
LiteflowResponse response = flowExecutor.execute2Resp("chain3", null, DefaultContext.class);
Assertions.assertTrue(response.isSuccess());
Assertions.assertNull(response.getCause());
Assertions.assertEquals("a==>s3", response.getExecuteStepStrWithoutTime());
}
//覆盖beforeProcess和afterProcess
@Test
public void testJavaxPro4() {
LiteflowResponse response = flowExecutor.execute2Resp("chain4", null, DefaultContext.class);
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("a==>s4", response.getExecuteStepStrWithoutTime());
DefaultContext context = response.getFirstContextBean();
Assertions.assertTrue((Boolean) context.getData("before"));
Assertions.assertTrue((Boolean) context.getData("after"));
}
//覆盖onSuccess
@Test
public void testJavaxPro5() {
LiteflowResponse response = flowExecutor.execute2Resp("chain5", null, DefaultContext.class);
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("a==>s5", response.getExecuteStepStrWithoutTime());
DefaultContext context = response.getFirstContextBean();
Assertions.assertTrue((Boolean) context.getData("successFlag"));
}
//覆盖onError
@Test
public void testJavaxPro6() {
LiteflowResponse response = flowExecutor.execute2Resp("chain6", null, DefaultContext.class);
DefaultContext context = response.getFirstContextBean();
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals("a==>s6", response.getExecuteStepStrWithoutTime());
Assertions.assertNotNull(response.getCause());
Assertions.assertTrue((Boolean) context.getData("errorFlag"));
Assertions.assertEquals("test error", context.getData("errorMsg"));
}
//覆盖rollback
@Test
public void testJavaxPro7() {
LiteflowResponse response = flowExecutor.execute2Resp("chain7", null, DefaultContext.class);
DefaultContext context = response.getFirstContextBean();
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals("s7==>b", response.getExecuteStepStrWithoutTime());
Assertions.assertEquals("b==>s7", response.getRollbackStepStrWithoutTime());
Assertions.assertTrue((Boolean) context.getData("rollbackFlag"));
}
}

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.script.javaxpro.override.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
System.out.println("ACmp executed!");
}
}

View File

@@ -0,0 +1,25 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.script.javaxpro.override.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
throw new RuntimeException("error");
}
@Override
public void rollback() throws Exception {
super.rollback();
}
}

View File

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

View File

@@ -0,0 +1,182 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE flow PUBLIC "liteflow" "liteflow.dtd">
<flow>
<nodes>
<node id="s1" type="script" language="java">
<![CDATA[
import com.yomahub.liteflow.core.NodeComponent;
import java.util.List;
public class Demo extends NodeComponent {
@Override
public void process() throws Exception {
}
@Override
public boolean isAccess() {
Integer index = this.getRequestData();
return index > 5;
}
}
]]>
</node>
<node id="s2" type="script" language="java">
<![CDATA[
import com.yomahub.liteflow.core.NodeComponent;
import java.util.List;
public class Demo extends NodeComponent {
@Override
public void process() throws Exception {
throw new RuntimeException("test error");
}
@Override
public boolean isContinueOnError() {
return true;
}
}
]]>
</node>
<node id="s3" type="script" language="java">
<![CDATA[
import com.yomahub.liteflow.core.NodeComponent;
import java.util.List;
public class Demo extends NodeComponent {
@Override
public void process() throws Exception {
this.setIsEnd(true);
}
}
]]>
</node>
<node id="s4" type="script" language="java">
<![CDATA[
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import java.util.List;
public class Demo extends NodeComponent {
@Override
public void process() throws Exception {
}
@Override
public void beforeProcess() {
DefaultContext context = this.getContextBean(DefaultContext.class);
context.setData("before", true);
}
@Override
public void afterProcess() {
DefaultContext context = this.getContextBean(DefaultContext.class);
context.setData("after", true);
}
}
]]>
</node>
<node id="s5" type="script" language="java">
<![CDATA[
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import java.util.List;
public class Demo extends NodeComponent {
@Override
public void process() throws Exception {
}
@Override
public void onSuccess() throws Exception {
DefaultContext context = this.getContextBean(DefaultContext.class);
context.setData("successFlag", true);
}
}
]]>
</node>
<node id="s6" type="script" language="java">
<![CDATA[
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import java.util.List;
public class Demo extends NodeComponent {
@Override
public void process() throws Exception {
throw new RuntimeException("test error");
}
@Override
public void onError(Exception e) throws Exception {
DefaultContext context = this.getContextBean(DefaultContext.class);
context.setData("errorFlag", true);
context.setData("errorMsg", e.getMessage());
}
}
]]>
</node>
<node id="s7" type="script" language="java">
<![CDATA[
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import java.util.List;
public class Demo extends NodeComponent {
@Override
public void process() throws Exception {
}
@Override
public void rollback() throws Exception {
DefaultContext context = this.getContextBean(DefaultContext.class);
context.setData("rollbackFlag", true);
}
}
]]>
</node>
</nodes>
<chain id="chain1">
THEN(s1, a);
</chain>
<chain id="chain2">
THEN(s2, a);
</chain>
<chain id="chain3">
THEN(a, s3);
</chain>
<chain id="chain4">
THEN(a, s4);
</chain>
<chain id="chain5">
THEN(a, s5);
</chain>
<chain id="chain6">
THEN(a, s6);
</chain>
<chain id="chain7">
THEN(s7, b);
</chain>
</flow>