feature #I5AYM5 组件事件回调特性支持

This commit is contained in:
everywhere.z
2022-06-07 00:44:03 +08:00
parent 9a63e34b43
commit cf4c233533
32 changed files with 914 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
package com.yomahub.liteflow.test.event;
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.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:/event/application.xml")
public class EventSpringTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
//测试组件成功事件
@Test
public void testEvent1() throws Exception{
LiteflowResponse<DefaultContext> response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("abc", response.getContextBean().getData("test"));
}
//测试组件失败事件
@Test
public void testEvent2() throws Exception{
LiteflowResponse<DefaultContext> response = flowExecutor.execute2Resp("chain2", "arg");
Assert.assertFalse(response.isSuccess());
Assert.assertEquals(NullPointerException.class, response.getCause().getClass());
Assert.assertEquals("ab", response.getContextBean().getData("test"));
Assert.assertEquals("error:d", response.getContextBean().getData("error"));
}
//测试组件失败事件本身抛出异常
@Test
public void testEvent3() throws Exception{
LiteflowResponse<DefaultContext> response = flowExecutor.execute2Resp("chain3", "arg");
Assert.assertFalse(response.isSuccess());
Assert.assertEquals(NullPointerException.class, response.getCause().getClass());
Assert.assertEquals("a", response.getContextBean().getData("test"));
Assert.assertEquals("error:e", response.getContextBean().getData("error"));
}
}

View File

@@ -0,0 +1,31 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.event.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
DefaultContext context = this.getContextBean();
context.setData("test","");
System.out.println("ACmp executed!");
}
@Override
public void onSuccess() throws Exception {
DefaultContext context = this.getContextBean();
String str = context.getData("test");
str += this.getNodeId();
context.setData("test", str);
}
}

View File

@@ -0,0 +1,29 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.event.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
System.out.println("BCmp executed!");
}
@Override
public void onSuccess() throws Exception {
DefaultContext context = this.getContextBean();
String str = context.getData("test");
str += this.getNodeId();
context.setData("test", str);
}
}

View File

@@ -0,0 +1,29 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.event.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
}
@Override
public void onSuccess() throws Exception {
DefaultContext context = this.getContextBean();
String str = context.getData("test");
str += this.getNodeId();
context.setData("test", str);
}
}

View File

@@ -0,0 +1,28 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.event.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("d")
public class DCmp extends NodeComponent {
@Override
public void process() throws Exception{
System.out.println("CCmp executed!");
throw new NullPointerException();
}
@Override
public void onError() throws Exception {
DefaultContext context = this.getContextBean();
context.setData("error","error:"+this.getNodeId());
}
}

View File

@@ -0,0 +1,29 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.event.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.stereotype.Component;
@Component("e")
public class ECmp extends NodeComponent {
@Override
public void process() throws Exception{
System.out.println("CCmp executed!");
throw new NullPointerException();
}
@Override
public void onError() throws Exception {
DefaultContext context = this.getContextBean();
context.setData("error","error:"+this.getNodeId());
throw new IllegalAccessException("错误事件回调本身抛出异常");
}
}

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.yomahub.liteflow.test.event.cmp" />
<bean id="springAware" class="com.yomahub.liteflow.spi.spring.SpringAware"/>
<bean class="com.yomahub.liteflow.spring.ComponentScanner"/>
<bean id="liteflowConfig" class="com.yomahub.liteflow.property.LiteflowConfig">
<property name="ruleSource" value="event/flow.xml"/>
</bean>
<bean id="flowExecutor" class="com.yomahub.liteflow.core.FlowExecutor">
<constructor-arg name="liteflowConfig" ref="liteflowConfig"/>
</bean>
</beans>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
<then value="a,b,c"/>
</chain>
<chain name="chain2">
<then value="a,b,d"/>
</chain>
<chain name="chain3">
<then value="a,e,b"/>
</chain>
</flow>