对代码进行优化

This commit is contained in:
everywhere.z
2022-06-17 19:03:46 +08:00
parent 2e82deec29
commit da73d04ab6
11 changed files with 42 additions and 23 deletions

View File

@@ -102,7 +102,7 @@ public class LiteFlowChainBuilder {
} else if (condition.getConditionType().equals(ConditionTypeEnum.TYPE_THEN)) {
if (this.conditionList.size() >= 1 &&
CollectionUtil.getLast(this.conditionList) instanceof ThenCondition) {
CollectionUtil.getLast(this.conditionList).getNodeList().addAll(condition.getNodeList());
CollectionUtil.getLast(this.conditionList).getExecutableList().addAll(condition.getExecutableList());
} else {
this.conditionList.add(condition);
}
@@ -110,7 +110,7 @@ public class LiteFlowChainBuilder {
if (this.conditionList.size() >= 1 &&
CollectionUtil.getLast(this.conditionList) instanceof WhenCondition &&
CollectionUtil.getLast(this.conditionList).getGroup().equals(condition.getGroup())) {
CollectionUtil.getLast(this.conditionList).getNodeList().addAll(condition.getNodeList());
CollectionUtil.getLast(this.conditionList).getExecutableList().addAll(condition.getExecutableList());
} else {
this.conditionList.add(condition);
}

View File

@@ -109,16 +109,16 @@ public class LiteFlowConditionBuilder {
if (FlowBus.containNode(executableEntity.getId())) {
Node node = FlowBus.copyNode(executableEntity.getId());
node.setTag(executableEntity.getTag());
this.condition.getNodeList().add(node);
this.condition.getExecutableList().add(node);
// 构建条件节点-通过是否包含条件节点列表-解析条件节点会含有realItem也就是括号里的node
buildCondNode(node, executableEntity.getNodeCondComponents());
} else if (hasChain(executableEntity.getId())) {
Chain chain = FlowBus.getChain(executableEntity.getId());
this.condition.getNodeList().add(chain);
this.condition.getExecutableList().add(chain);
} else {
//元数据没有的话从spring上下文再取一遍
//这部分有2个目的
//一是为了防止标有@Lazy懒加载的组件二是spring负责扫描而用代码的形式加载chain这种情况。
//一是为了防止标有@Lazy懒加载的组件二是spring负责扫描而用动态代码的形式加载组件这种情况。
NodeComponent nodeComponent = ContextAwareHolder.loadContextAware().getBean(executableEntity.getId());
if (ObjectUtil.isNotNull(nodeComponent)){
FlowBus.addSpringScanNode(executableEntity.getId(), nodeComponent);

View File

@@ -0,0 +1,22 @@
package com.yomahub.liteflow.exception;
public class ParserCannotFindException extends RuntimeException {
private static final long serialVersionUID = 1L;
/** 异常信息 */
private String message;
public ParserCannotFindException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@@ -7,10 +7,8 @@
*/
package com.yomahub.liteflow.flow.element.condition;
import cn.hutool.core.collection.CollUtil;
import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
import com.yomahub.liteflow.enums.ExecuteTypeEnum;
import com.yomahub.liteflow.exception.FlowSystemException;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.enums.ConditionTypeEnum;
@@ -18,12 +16,13 @@ import java.util.ArrayList;
import java.util.List;
/**
* 里面包含了when或者then
* Condition的抽象类
* @author Bryan.Zhang
*/
public abstract class Condition implements Executable{
private List<Executable> nodeList = new ArrayList<>();
//可执行元素的集合
private List<Executable> executableList = new ArrayList<>();
//只在when类型下有效以区分当when调用链调用失败时是否继续往下执行 默认false不继续执行
private boolean errorResume = false;
@@ -47,12 +46,12 @@ public abstract class Condition implements Executable{
return this.getExecuteType().name();
}
public List<Executable> getNodeList() {
return nodeList;
public List<Executable> getExecutableList() {
return executableList;
}
public void setNodeList(List<Executable> nodeList) {
this.nodeList = nodeList;
public void setExecutableList(List<Executable> executableList) {
this.executableList = executableList;
}
public boolean isErrorResume() {

View File

@@ -19,7 +19,7 @@ public class FinallyCondition extends Condition {
@Override
public void execute(Integer slotIndex) throws Exception {
for(Executable executableItem : this.getNodeList()){
for(Executable executableItem : this.getExecutableList()){
executableItem.execute(slotIndex);
}
}

View File

@@ -19,7 +19,7 @@ public class PreCondition extends Condition {
@Override
public void execute(Integer slotIndex) throws Exception {
for(Executable executableItem : this.getNodeList()){
for(Executable executableItem : this.getExecutableList()){
executableItem.execute(slotIndex);
}
}

View File

@@ -22,7 +22,7 @@ public class ThenCondition extends Condition {
@Override
public void execute(Integer slotIndex) throws Exception {
for (Executable executableItem : this.getNodeList()) {
for (Executable executableItem : this.getExecutableList()) {
executableItem.execute(slotIndex);
}
}

View File

@@ -65,7 +65,7 @@ public class WhenCondition extends Condition {
//1.根据condition.getNodeList()的集合进行流处理用map进行把executable对象转换成List<CompletableFuture<WhenFutureObj>>
//2.在转的过程中套入CompletableFutureTimeout方法进行超时判断如果超时则用WhenFutureObj.timeOut返回超时的对象
//3.第2个参数是主要的本体CompletableFuture传入了ParallelSupplier和线程池对象
List<CompletableFuture<WhenFutureObj>> completableFutureList = this.getNodeList().stream().filter(executable -> {
List<CompletableFuture<WhenFutureObj>> completableFutureList = this.getExecutableList().stream().filter(executable -> {
try {
return executable.isAccess(slotIndex);
}catch (Exception e){

View File

@@ -55,8 +55,7 @@ public abstract class BaseFlowParser implements FlowParser {
* @param chainPropBean 构建 chain 的中间属性
* @param chainBuilder chainBuilder
*/
public void buildChain(ChainPropBean chainPropBean
, LiteFlowChainBuilder chainBuilder) {
public void buildChain(ChainPropBean chainPropBean, LiteFlowChainBuilder chainBuilder) {
String condValueStr = chainPropBean.getCondValueStr();
String group = chainPropBean.getGroup();
String errorResume = chainPropBean.getErrorResume();

View File

@@ -17,6 +17,7 @@ import java.util.List;
*/
public class LocalXmlFlowParser extends XmlFlowParser{
@Override
public void parseMain(List<String> pathList) throws Exception {
List<String> contentList = PathContentParserHolder.loadContextAware().parseContent(pathList);
parse(contentList);

View File

@@ -15,12 +15,10 @@ import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import static com.yomahub.liteflow.common.ChainConstant.ANY;
import static com.yomahub.liteflow.common.ChainConstant.CHAIN;
import static com.yomahub.liteflow.common.ChainConstant.ERROR_RESUME;
@@ -46,7 +44,7 @@ public abstract class XmlFlowParser extends BaseFlowParser {
private final Logger LOG = LoggerFactory.getLogger(XmlFlowParser.class);
private final Set<String> CHAIN_NAME_SET = new CopyOnWriteArraySet<>();
private final Set<String> CHAIN_NAME_SET = new HashSet<>();
public void parse(String content) throws Exception {
parse(ListUtil.toList(content));