mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 12:12:08 +08:00
BUG #IAFKQV 声明式的onError方法取不到Exception
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.yomahub.liteflow.core.proxy;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.exceptions.InvocationTargetRuntimeException;
|
||||
import cn.hutool.core.lang.Tuple;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
@@ -36,6 +37,7 @@ import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* 声明式组件的代理核心生成类
|
||||
@@ -145,7 +147,7 @@ public class DeclComponentProxy {
|
||||
// 首先需要保证第一个参数是NodeComponent
|
||||
// 其次需要针对于@LiteflowFact做处理
|
||||
try {
|
||||
Object[] realArgs = loadMethodParameter(proxy, currentMethodWrapBean);
|
||||
Object[] realArgs = loadMethodParameter(proxy, currentMethodWrapBean, args);
|
||||
return ReflectUtil.invoke(declWarpBean.getRawBean(), currentMethodWrapBean.getMethod(), realArgs);
|
||||
}catch (InvocationTargetRuntimeException e) {
|
||||
InvocationTargetException targetEx = (InvocationTargetException) e.getCause();
|
||||
@@ -157,52 +159,63 @@ public class DeclComponentProxy {
|
||||
|
||||
private final ExpressRunner expressRunner = new ExpressRunner();
|
||||
|
||||
private Object[] loadMethodParameter(Object proxy, MethodWrapBean methodWrapBean){
|
||||
private Object[] loadMethodParameter(Object proxy, MethodWrapBean methodWrapBean, Object[] args){
|
||||
NodeComponent thisNodeComponent = (NodeComponent) proxy;
|
||||
|
||||
return methodWrapBean.getParameterWrapBeanList().stream().map(parameterWrapBean -> {
|
||||
// 如果参数是NodeComponent,那就返回proxy本身
|
||||
if (parameterWrapBean.getParameterType().isAssignableFrom(NodeComponent.class)) {
|
||||
return proxy;
|
||||
}
|
||||
return IntStream.range(0, methodWrapBean.getParameterWrapBeanList().size()).boxed().map(new Function<Integer, Object>() {
|
||||
@Override
|
||||
public Object apply(Integer index) {
|
||||
ParameterWrapBean parameterWrapBean = methodWrapBean.getParameterWrapBeanList().get(index);
|
||||
|
||||
// 如果没有@LiteflowFact标注,那么不处理,直接赋值null
|
||||
if (parameterWrapBean.getFact() == null) {
|
||||
return null;
|
||||
}
|
||||
// 如果参数是NodeComponent,那就返回proxy本身
|
||||
if (parameterWrapBean.getParameterType().isAssignableFrom(NodeComponent.class)) {
|
||||
return proxy;
|
||||
}
|
||||
|
||||
// 把上下文数据转换成map形式的,key为别名,value为上下文
|
||||
Map<String, Object> contextMap = DataBus.getSlot(thisNodeComponent.getSlotIndex()).getContextBeanList().stream().collect(
|
||||
Collectors.toMap(tuple -> tuple.get(0), tuple -> tuple.get(1))
|
||||
);
|
||||
|
||||
List<String> errorList = new ArrayList<>();
|
||||
|
||||
Object result = null;
|
||||
// 根据表达式去上下文里搜索相匹配的数据
|
||||
for(Map.Entry<String, Object> entry : contextMap.entrySet()){
|
||||
try{
|
||||
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache(entry.getKey() + "." + parameterWrapBean.getFact().value());
|
||||
DefaultContext<String, Object> context = new DefaultContext<>();
|
||||
context.put(entry.getKey(), entry.getValue());
|
||||
result = expressRunner.execute(instructionSet, context, errorList, false, false);
|
||||
if (result != null){
|
||||
break;
|
||||
if (parameterWrapBean.getFact() == null && ArrayUtil.isNotEmpty(args)){
|
||||
if (parameterWrapBean.getParameterType().isAssignableFrom(args[index - 1].getClass())){
|
||||
return args[index -1];
|
||||
}
|
||||
}catch (Exception ignore){}
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null){
|
||||
try{
|
||||
// 如果没有搜到,那么尝试推断表达式是指定的上下文,按照指定上下文的方式去再获取
|
||||
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache("contextMap." + parameterWrapBean.getFact().value());
|
||||
DefaultContext<String, Object> context = new DefaultContext<>();
|
||||
context.put("contextMap", contextMap);
|
||||
result = expressRunner.execute(instructionSet, context, errorList, false, false);
|
||||
}catch (Exception ignore){}
|
||||
}
|
||||
// 如果没有@LiteflowFact标注,那么不处理,直接赋值null
|
||||
if (parameterWrapBean.getFact() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
// 把上下文数据转换成map形式的,key为别名,value为上下文
|
||||
Map<String, Object> contextMap = DataBus.getSlot(thisNodeComponent.getSlotIndex()).getContextBeanList().stream().collect(
|
||||
Collectors.toMap(tuple -> tuple.get(0), tuple -> tuple.get(1))
|
||||
);
|
||||
|
||||
List<String> errorList = new ArrayList<>();
|
||||
|
||||
Object result = null;
|
||||
// 根据表达式去上下文里搜索相匹配的数据
|
||||
for(Map.Entry<String, Object> entry : contextMap.entrySet()){
|
||||
try{
|
||||
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache(entry.getKey() + "." + parameterWrapBean.getFact().value());
|
||||
DefaultContext<String, Object> context = new DefaultContext<>();
|
||||
context.put(entry.getKey(), entry.getValue());
|
||||
result = expressRunner.execute(instructionSet, context, errorList, false, false);
|
||||
if (result != null){
|
||||
break;
|
||||
}
|
||||
}catch (Exception ignore){}
|
||||
}
|
||||
|
||||
if (result == null){
|
||||
try{
|
||||
// 如果没有搜到,那么尝试推断表达式是指定的上下文,按照指定上下文的方式去再获取
|
||||
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache("contextMap." + parameterWrapBean.getFact().value());
|
||||
DefaultContext<String, Object> context = new DefaultContext<>();
|
||||
context.put("contextMap", contextMap);
|
||||
result = expressRunner.execute(instructionSet, context, errorList, false, false);
|
||||
}catch (Exception ignore){}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}).toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,9 +32,15 @@ public class BaseELDeclMultiSpringbootTest extends BaseTest {
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
@Test
|
||||
public void testBase() throws Exception {
|
||||
public void testBase1() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBase2() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,4 +35,23 @@ public class CmpConfig {
|
||||
System.out.println("CCmp executed!");
|
||||
}
|
||||
|
||||
|
||||
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "e")
|
||||
public void processE(NodeComponent bindCmp) {
|
||||
System.out.println("ECmp executed!");
|
||||
}
|
||||
|
||||
@LiteflowMethod(value = LiteFlowMethodEnum.BEFORE_PROCESS, nodeId = "e")
|
||||
public void beforeProcessE(NodeComponent bindCmp) {
|
||||
int a = 1/0;
|
||||
}
|
||||
|
||||
@LiteflowMethod(value = LiteFlowMethodEnum.ON_ERROR, nodeId = "e")
|
||||
public void onErrorE(NodeComponent bindCmp, Exception e) {
|
||||
if (e != null){
|
||||
e.printStackTrace();
|
||||
}else{
|
||||
System.out.println("no error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,8 @@
|
||||
<chain name="chain1">
|
||||
THEN(a, b, WHEN(c, d));
|
||||
</chain>
|
||||
|
||||
<chain name="chain2">
|
||||
THEN(a, e);
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -32,9 +32,15 @@ public class BaseELDeclSpringbootTest extends BaseTest {
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
@Test
|
||||
public void testBase() throws Exception {
|
||||
public void testBase1() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBase2() throws Exception {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
|
||||
Assertions.assertFalse(response.isSuccess());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,13 @@ import org.springframework.stereotype.Component;
|
||||
@LiteflowCmpDefine(NodeTypeEnum.COMMON)
|
||||
public class ACmp {
|
||||
|
||||
|
||||
|
||||
@LiteflowMethod(LiteFlowMethodEnum.PROCESS)
|
||||
public void process(NodeComponent bindCmp) {
|
||||
System.out.println("ACmp executed!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* <p>Title: liteflow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* @author Bryan.Zhang
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.base.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowCmpDefine;
|
||||
import com.yomahub.liteflow.annotation.LiteflowMethod;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
|
||||
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("e")
|
||||
@LiteflowCmpDefine(NodeTypeEnum.COMMON)
|
||||
public class ECmp {
|
||||
|
||||
@LiteflowMethod(LiteFlowMethodEnum.BEFORE_PROCESS)
|
||||
public void before(NodeComponent bindCmp) throws Exception{
|
||||
int a = 1/0;
|
||||
}
|
||||
|
||||
@LiteflowMethod(LiteFlowMethodEnum.PROCESS)
|
||||
public void process(NodeComponent bindCmp) {
|
||||
System.out.println("CCmp executed!");
|
||||
}
|
||||
|
||||
@LiteflowMethod(LiteFlowMethodEnum.ON_ERROR)
|
||||
public void onError(NodeComponent bindCmp, Exception e) {
|
||||
if (e != null){
|
||||
e.printStackTrace();
|
||||
}else{
|
||||
System.out.println("no error");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,8 @@
|
||||
<chain name="chain1">
|
||||
THEN(a, b, WHEN(c, d));
|
||||
</chain>
|
||||
|
||||
<chain name="chain2">
|
||||
THEN(a, e);
|
||||
</chain>
|
||||
</flow>
|
||||
Reference in New Issue
Block a user