丰富代码的注释

This commit is contained in:
bryan31
2021-03-24 15:48:23 +08:00
parent 07da60e2c5
commit c0d0e52f4e
31 changed files with 149 additions and 7 deletions

View File

@@ -9,6 +9,11 @@ package com.yomahub.liteflow.aop;
import com.yomahub.liteflow.entity.data.Slot;
/**
* 全局组件拦截器接口
* 实现这个接口并注入到spring上下文即可
* @author Bryan.Zhang
*/
public interface ICmpAroundAspect {
void beforeProcess(Slot slot);

View File

@@ -45,6 +45,7 @@ public class FlowExecutor {
private String zkNode;
//FlowExecutor的初始化化方式主要用于parse规则文件
public void init() {
if (ObjectUtil.isNull(liteflowConfig) || StrUtil.isBlank(liteflowConfig.getRuleSource())){
throw new ConfigErrorException("config error, please check liteflow config property");
@@ -55,15 +56,16 @@ public class FlowExecutor {
XmlFlowParser parser = null;
for(String path : rulePath){
try {
if(isLocalConfig(path)) {
if(isLocalConfig(path)) { //判断是否是本地的xml文件
parser = new LocalXmlFlowParser();
}else if(isZKConfig(path)){
}else if(isZKConfig(path)){ //判断是否是zk配置
if(StringUtils.isNotBlank(zkNode)) {
parser = new ZookeeperXmlFlowParser(zkNode);
}else {
parser = new ZookeeperXmlFlowParser();
}
}else if(isClassConfig(path)) {
}else if(isClassConfig(path)) { //判断是否是自定义配置
Class c = Class.forName(path);
parser = (XmlFlowParser)c.newInstance();
}

View File

@@ -28,6 +28,10 @@ import org.springframework.lang.Nullable;
import javax.annotation.Resource;
/**
* 普通组件抽象类
* @author Bryan.Zhang
*/
public abstract class NodeComponent {
private static final Logger LOG = LoggerFactory.getLogger(NodeComponent.class);
@@ -54,6 +58,7 @@ public abstract class NodeComponent {
ComponentScaner.cmpAroundAspect.beforeProcess(slot);
}
//业务处理逻辑
process();
//process后置处理
@@ -74,6 +79,7 @@ public abstract class NodeComponent {
monitorBus.addStatistics(statistics);
}
//进行判断是否是条件节点条件节点最终也会落到node节点或者chain节点上
if(this instanceof NodeCondComponent){
String condNodeId = slot.getCondResult(this.getClass().getName());
if(StringUtils.isNotBlank(condNodeId)){

View File

@@ -7,8 +7,10 @@
*/
package com.yomahub.liteflow.core;
import org.springframework.stereotype.Component;
/**
* 条件路由节点抽象类
* @author Bryan.Zhang
*/
public abstract class NodeCondComponent extends NodeComponent {
@Override
@@ -17,6 +19,7 @@ public abstract class NodeCondComponent extends NodeComponent {
this.getSlot().setCondResult(this.getClass().getName(), nodeId);
}
//用以返回路由节点的beanId
public abstract String processCond() throws Exception;
}

View File

@@ -15,6 +15,10 @@ import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Slot的抽象类实现
* @author Bryan.Zhang
*/
@SuppressWarnings("unchecked")
public abstract class AbsSlot implements Slot{

View File

@@ -9,6 +9,10 @@ package com.yomahub.liteflow.entity.data;
import java.text.MessageFormat;
/**
* 组件步骤对象
* @author Bryan.Zhang
*/
public class CmpStep {
private String nodeId;

View File

@@ -7,6 +7,10 @@
*/
package com.yomahub.liteflow.entity.data;
/**
* 组件步骤类型
* @author Bryan.Zhang
*/
public enum CmpStepType {
START,
END,

View File

@@ -15,6 +15,10 @@ import com.yomahub.liteflow.util.SpringAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 数据BUS主要用来管理Slot用以分配和回收
* @author Bryan.Zhang
*/
public class DataBus {
private static final Logger LOG = LoggerFactory.getLogger(DataBus.class);

View File

@@ -7,6 +7,11 @@
*/
package com.yomahub.liteflow.entity.data;
/**
* 默认Slot的实现类
* 不建议业务系统直接用此类此类基于弱类型Map应该自己去继承AbsSlot
* @author Bryan.Zhang
*/
public class DefaultSlot extends AbsSlot {
}

View File

@@ -7,6 +7,10 @@
*/
package com.yomahub.liteflow.entity.data;
/**
* Slot的接口
* @author Bryan.Zhang
*/
public interface Slot {
public <T> T getInput(String nodeId);

View File

@@ -9,11 +9,9 @@
package com.yomahub.liteflow.entity.flow;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.entity.data.DataBus;
import com.yomahub.liteflow.entity.data.Slot;
import com.yomahub.liteflow.enums.ExecuteTypeEnum;
import com.yomahub.liteflow.exception.ChainEndException;
import com.yomahub.liteflow.exception.FlowSystemException;
import com.yomahub.liteflow.property.LiteflowConfig;
import com.yomahub.liteflow.util.SpringAware;
@@ -25,6 +23,10 @@ import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* chain对象实现可执行器
* @author Bryan.Zhang
*/
public class Chain implements Executable {
private static final Logger LOG = LoggerFactory.getLogger(Chain.class);
@@ -65,6 +67,7 @@ public class Chain implements Executable {
this.chainName = chainName;
}
//执行chain的主方法
@Override
public void execute(Integer slotIndex) throws Exception {
if (CollectionUtils.isEmpty(conditionList)) {
@@ -73,6 +76,8 @@ public class Chain implements Executable {
Slot slot = DataBus.getSlot(slotIndex);
//循环chain里包含的condition每一个condition有可能是then也有可能是when
//when的话为异步用闭锁进行等待所有when结束后才能进入下一个condition
for (Condition condition : conditionList) {
if (condition instanceof ThenCondition) {
for (Executable executableItem : condition.getNodeList()) {

View File

@@ -9,6 +9,10 @@ package com.yomahub.liteflow.entity.flow;
import java.util.List;
/**
* 里面包含了when或者then
* @author Bryan.Zhang
*/
public class Condition {
private List<Executable> nodeList;

View File

@@ -2,6 +2,11 @@ package com.yomahub.liteflow.entity.flow;
import com.yomahub.liteflow.enums.ExecuteTypeEnum;
/**
* 可执行器接口
* 目前实现这个接口的有2个node和chain
* @author Bryan.Zhang
*/
public interface Executable {
void execute(Integer slotIndex) throws Exception;

View File

@@ -21,6 +21,10 @@ import com.yomahub.liteflow.exception.FlowSystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Node节点实现可执行器
* @author Bryan.Zhang
*/
public class Node implements Executable{
private static final Logger LOG = LoggerFactory.getLogger(Node.class);
@@ -75,19 +79,25 @@ public class Node implements Executable{
this.condNodeMap.put(nodeId, condNode);
}
//node的执行主要逻辑
//所有的可执行节点其实最终都会落到node上来因为chain中包含的也是node
@Override
public void execute(Integer slotIndex) throws Exception {
if(instance == null){
throw new FlowSystemException("there is no instance for node id " + id);
}
//每次执行node前把分配的slot index信息放入threadLocal里
instance.setSlotIndex(slotIndex);
Slot slot = DataBus.getSlot(slotIndex);
try{
//判断是否可执行所以isAccess经常作为一个组件进入的实际判断要素用作检查slot里的参数的完备性
if(instance.isAccess()){
//执行业务逻辑的主要入口
instance.execute();
//如果组件覆盖了isEnd方法或者在在逻辑中主要调用了setEnd(true)的话,流程就会立马结束
if(instance.isEnd()){
String errorInfo = StrUtil.format("[{}]:component[{}] lead the chain to end",slot.getRequestId(),instance.getClass().getSimpleName());
throw new ChainEndException(errorInfo);
@@ -96,6 +106,7 @@ public class Node implements Executable{
LOG.info("[{}]:[X]skip component[{}] execution",slot.getRequestId(),instance.getClass().getSimpleName());
}
}catch (Exception e){
//如果组件覆盖了isContinueOnError方法返回为true那即便出了异常也会继续流程
if(instance.isContinueOnError()){
String errorMsg = MessageFormat.format("[{0}]:component[{1}] cause error,but flow is still go on", slot.getRequestId(),id);
LOG.error(errorMsg,e);
@@ -105,6 +116,7 @@ public class Node implements Executable{
throw e;
}
}finally {
//移除threadLocal里的信息
instance.removeSlotIndex();
instance.removeIsEnd();
}

View File

@@ -9,6 +9,10 @@ package com.yomahub.liteflow.entity.flow;
import java.util.List;
/**
* 串行器
* @author Bryan.Zhang
*/
public class ThenCondition extends Condition {
public ThenCondition(List<Executable> nodeList) {

View File

@@ -9,6 +9,10 @@ package com.yomahub.liteflow.entity.flow;
import java.util.List;
/**
* 并行器
* @author Bryan.Zhang
*/
public class WhenCondition extends Condition{
public WhenCondition(List<Executable> nodeList) {

View File

@@ -5,6 +5,10 @@ import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
/**
* 并行器线程
* @author Bryan.Zhang
*/
public class WhenConditionThread extends Thread {
private static final Logger LOG = LoggerFactory.getLogger(WhenConditionThread.class);

View File

@@ -7,6 +7,10 @@
*/
package com.yomahub.liteflow.entity.monitor;
/**
* 统计类
* @author Bryan.Zhang
*/
public class CompStatistics implements Comparable{
private String componentClazzName;

View File

@@ -7,6 +7,10 @@
*/
package com.yomahub.liteflow.enums;
/**
* 可执行节点枚举
* @author Bryan.Zhang
*/
public enum ExecuteTypeEnum {
CHAIN,NODE
}

View File

@@ -15,6 +15,10 @@ import org.apache.commons.collections4.MapUtils;
import com.yomahub.liteflow.entity.flow.Chain;
import com.yomahub.liteflow.entity.flow.Node;
/**
* 流程元数据类
* @author Bryan.Zhang
*/
public class FlowBus {
private static Map<String, Chain> chainMap = new HashMap<>();

View File

@@ -30,6 +30,10 @@ import org.slf4j.LoggerFactory;
import com.yomahub.liteflow.entity.data.DataBus;
import com.yomahub.liteflow.entity.monitor.CompStatistics;
/**
* 监控类元数据,打印执行器类
* @author Bryan.Zhang
*/
public class MonitorBus {
private LiteflowConfig liteflowConfig;

View File

@@ -2,6 +2,10 @@ package com.yomahub.liteflow.monitor;
import java.util.TimerTask;
/**
* 监控器线程
* @author Bryan.Zhang
*/
public class MonitorTimeTask extends TimerTask {
private MonitorBus monitorBus;

View File

@@ -1,5 +1,9 @@
package com.yomahub.liteflow.parser;
/**
* 基于自定义的xml方式解析器
* @author Bryan.Zhang
*/
public abstract class ClassXmlFlowParser extends XmlFlowParser {
@Override
public void parseMain(String path) throws Exception {

View File

@@ -9,6 +9,10 @@ package com.yomahub.liteflow.parser;
import cn.hutool.core.io.FileUtil;
/**
* 基于本地的xml方式解析器
* @author Bryan.Zhang
*/
public class LocalXmlFlowParser extends XmlFlowParser{
public void parseMain(String rulePath) throws Exception {

View File

@@ -7,6 +7,10 @@
*/
package com.yomahub.liteflow.parser;
/**
* 正则实体,主要用于条件节点
* @author Bryan.Zhang
*/
public class RegexEntity {
private String item;

View File

@@ -23,6 +23,10 @@ import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.spring.ComponentScaner;
/**
* xml形式的解析器
* @author Bryan.Zhang
*/
public abstract class XmlFlowParser {
private final Logger LOG = LoggerFactory.getLogger(XmlFlowParser.class);
@@ -34,6 +38,7 @@ public abstract class XmlFlowParser {
parse(document);
}
//xml形式的主要解析过程
@SuppressWarnings("unchecked")
public void parse(Document document) throws Exception {
try {
@@ -83,6 +88,7 @@ public abstract class XmlFlowParser {
}
}
//解析一个chain的过程
private void parseOneChain(Element e) throws Exception{
String condArrayStr;
String[] condArray;
@@ -102,6 +108,7 @@ public abstract class XmlFlowParser {
RegexEntity regexEntity;
String itemExpression;
String item;
//这里解析的规则优先按照node去解析再按照chain去解析
for (int i = 0; i < condArray.length; i++) {
itemExpression = condArray[i].trim();
regexEntity = parseNodeStr(itemExpression);
@@ -109,6 +116,7 @@ public abstract class XmlFlowParser {
if(FlowBus.containNode(item)){
Node node = FlowBus.getNode(item);
chainNodeList.add(node);
//这里判断是不是条件节点条件节点会含有realItem也就是括号里的node
if(regexEntity.getRealItemArray() != null){
for(String key : regexEntity.getRealItemArray()){
if(FlowBus.containNode(key)){
@@ -137,6 +145,8 @@ public abstract class XmlFlowParser {
FlowBus.addChain(chainName, new Chain(chainName,conditionList));
}
//判断在这个FlowBus元数据里是否含有这个chain
//因为chain和node都是可执行器在一个规则文件上有可能是node有可能是chain
private boolean hasChain(Element e,String chainName) throws Exception{
Element rootElement = e.getParent();
List<Element> chainList = rootElement.elements("chain");
@@ -152,6 +162,7 @@ public abstract class XmlFlowParser {
return false;
}
//条件节点的正则解析
public static RegexEntity parseNodeStr(String str) {
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile("[^\\)\\(]+");

View File

@@ -12,6 +12,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yomahub.liteflow.exception.ParseException;
/**
* 基于zk方式的xml形式的解析器
* @author Bryan.Zhang
*/
public class ZookeeperXmlFlowParser extends XmlFlowParser{
private static final Logger LOG = LoggerFactory.getLogger(ZookeeperXmlFlowParser.class);

View File

@@ -7,6 +7,10 @@
*/
package com.yomahub.liteflow.property;
/**
* liteflow的配置实体类
* @author Bryan.Zhang
*/
public class LiteflowConfig {
//流程定义资源地址

View File

@@ -3,6 +3,10 @@ package com.yomahub.liteflow.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* logo打印器
* @author Bryan.Zhang
*/
public class LOGOPrinter {
private static final Logger LOG = LoggerFactory.getLogger(LOGOPrinter.class);

View File

@@ -13,6 +13,10 @@ import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* 有限队列集合
* @author Bryan.Zhang
*/
public class LimitQueue<E> implements Queue<E> {
/**

View File

@@ -10,6 +10,10 @@ import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 基于代码形式的spring上下文工具类
* @author Bryan.Zhang
*/
public class SpringAware implements ApplicationContextAware {
private static final Logger log = LoggerFactory.getLogger(SpringAware.class);
private static ApplicationContext applicationContext = null;