enhancement #I7KOPV 类级别声明组件优化建议

This commit is contained in:
everywhere.z 2023-07-21 00:29:12 +08:00
parent c18f46ac48
commit 288899d3ad
8 changed files with 33 additions and 8 deletions

View File

@ -18,10 +18,16 @@ public @interface LiteflowMethod {
/**
* 节点ID用于区分节点 默认为空 则按照Spring模式下BeanName为准
* @return
* @return nodeId
*/
String nodeId() default "";
/**
* 节点Name
* @return nodeName
*/
String nodeName() default "";
/**
* CMP类型定义
* @return AnnotationNodeTypeEnum

View File

@ -13,6 +13,7 @@ import com.yomahub.liteflow.exception.LiteFlowException;
import com.yomahub.liteflow.exception.ProxyException;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import com.yomahub.liteflow.spi.holder.LiteflowComponentSupportHolder;
import com.yomahub.liteflow.util.LiteFlowProxyUtil;
import com.yomahub.liteflow.util.SerialsUtil;
import net.bytebuddy.ByteBuddy;
@ -28,6 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
@ -94,8 +96,23 @@ public class ComponentProxy {
boolean legal = classes.size() == 1;
if (!legal) {
throw new LiteFlowException("The cmpClass of the same nodeId must be the same,you declared nodeId:"
+ activeNodeId + ",cmpClass:" + classes);
+ activeNodeId + ",cmpClass:" + clazz);
}
String activeNodeName;
if (isMethodCreate){
// 获取process上的LiteflowMethod
LiteflowMethod mainliteflowMethod = methodList.stream().filter(liteflowMethod -> liteflowMethod.value().isMainMethod()).findFirst().orElse(null);
if (mainliteflowMethod == null){
String errMsg = StrUtil.format("you have not defined @LiteFlowMethod on the processXXX method in class {}", clazz.getName());
throw new LiteFlowException(errMsg);
}
activeNodeName = mainliteflowMethod.nodeName();
}else{
activeNodeName = LiteflowComponentSupportHolder.loadLiteflowComponentSupport().getCmpName(bean);
}
// 当前节点实际LiteflowRetry注解
AtomicReference<LiteflowRetry> liteflowRetryAtomicReference = new AtomicReference<>(null);
// 相同nodeId只能有一个LiteflowRetry定义方法,且必须再Process方法上
@ -151,6 +168,8 @@ public class ComponentProxy {
NodeComponent nodeComponent = (NodeComponent) instance;
// 重设nodeId
nodeComponent.setNodeId(activeNodeId);
// 重设nodeName
nodeComponent.setName(activeNodeName);
return nodeComponent;
}
catch (Exception e) {

View File

@ -93,7 +93,7 @@ public class FlowBus {
}
nodeMap.put(nodeId,
new Node(ComponentInitializer.loadInstance().initComponent(nodeComponent, type, null, nodeId)));
new Node(ComponentInitializer.loadInstance().initComponent(nodeComponent, type, nodeComponent.getName(), nodeId)));
}
/**

View File

@ -10,6 +10,6 @@ import com.yomahub.liteflow.core.NodeComponent;
*/
public interface LiteflowComponentSupport extends SpiPriority {
String getCmpName(NodeComponent nodeComponent);
String getCmpName(Object nodeComponent);
}

View File

@ -12,7 +12,7 @@ import com.yomahub.liteflow.spi.LiteflowComponentSupport;
public class LocalLiteflowComponentSupport implements LiteflowComponentSupport {
@Override
public String getCmpName(NodeComponent nodeComponent) {
public String getCmpName(Object nodeComponent) {
return null;
}

View File

@ -14,7 +14,7 @@ import com.yomahub.liteflow.spi.LiteflowComponentSupport;
public class SolonLiteflowComponentSupport implements LiteflowComponentSupport {
@Override
public String getCmpName(NodeComponent nodeComponent) {
public String getCmpName(Object nodeComponent) {
// 判断NodeComponent是否是标识了@LiteflowComponent的标注
// 如果标注了那么要从中取到name字段
LiteflowComponent liteflowComponent = nodeComponent.getClass().getAnnotation(LiteflowComponent.class);

View File

@ -15,7 +15,7 @@ import com.yomahub.liteflow.spi.LiteflowComponentSupport;
public class SpringLiteflowComponentSupport implements LiteflowComponentSupport {
@Override
public String getCmpName(NodeComponent nodeComponent) {
public String getCmpName(Object nodeComponent) {
// 判断NodeComponent是否是标识了@LiteflowComponent的标注
// 如果标注了那么要从中取到name字段
LiteflowComponent liteflowComponent = nodeComponent.getClass().getAnnotation(LiteflowComponent.class);

View File

@ -10,7 +10,7 @@ import javax.annotation.Resource;
@LiteflowComponent
public class CmpConfig {
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "a")
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "a", nodeName = "A组件")
public void processA(NodeComponent bindCmp) {
System.out.println("ACmp executed!");
}