Skip to content

Commit

Permalink
Merge pull request #30 from lunasaw/2.6.0
Browse files Browse the repository at this point in the history
2.6.0 流程引擎单节点扩展
  • Loading branch information
lunasaw authored May 31, 2024
2 parents 50da3f9 + 9823154 commit dd281d3
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>io.github.lunasaw</groupId>
<artifactId>luna-common</artifactId>
<name>luna-common</name>
<version>2.5.9</version>
<version>2.6.0</version>
<description>common is project which contains common utils</description>
<url>https://github.com/lunasaw/luna-common</url>

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/luna/common/engine/model/NodeName.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.luna.common.engine.model;

import java.util.Objects;

import com.luna.common.check.Assert;
import lombok.AllArgsConstructor;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
* 节点Key
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.luna.common.engine.spi;

import com.luna.common.engine.model.EngineRunData;

public abstract class AbstractBatchNodeNodeSpi<T> implements BatchNodeNodeSpi<T> {

@Override
public boolean isAccept(EngineRunData engineRunData) {
return true;
}

}
24 changes: 24 additions & 0 deletions src/main/java/com/luna/common/engine/spi/BatchNodeNodeSpi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.luna.common.engine.spi;

import com.luna.common.engine.model.EngineRunData;

/**
* 每个流程节点的扩展点
**/
public interface BatchNodeNodeSpi<T> extends NodeSpi {
/**
* 判断该扩展点是否需要执行 true:需要执行; false:不需要执行
*
* @param spiData
* @return
*/
default boolean isAccept(EngineRunData spiData) {
return true;
}

/**
* 封装spi的业务逻辑方法
*
*/
void invoke(EngineRunData spiData);
}
8 changes: 8 additions & 0 deletions src/main/java/com/luna/common/engine/spi/NodeSpi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.luna.common.engine.spi;

/**
* SPI接口
*/
public interface NodeSpi {

}
21 changes: 21 additions & 0 deletions src/main/java/com/luna/common/engine/task/AbstractEngineNode.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
package com.luna.common.engine.task;

import java.util.ArrayList;
import java.util.List;

import com.luna.common.engine.model.EngineContext;
import com.luna.common.engine.spi.NodeSpi;

import lombok.Data;

/**
* @author luna
*/
@Data
public abstract class AbstractEngineNode<T> implements EngineNode<T> {
/**
* SPI列表
*/
private List<NodeSpi> batchNodeNodeList = new ArrayList<>();

@Override
public boolean couldContinueExecute(EngineContext engineContext) {
if (engineContext.isStop()) {
return false;
}
return true;
}

@Override
public List<NodeSpi> getSpiList() {
return batchNodeNodeList;
}

public void add(NodeSpi nodeSpi) {
batchNodeNodeList.add(nodeSpi);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/luna/common/engine/task/EngineNode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.luna.common.engine.task;

import java.util.List;

import com.luna.common.engine.model.EngineContext;
import com.luna.common.engine.model.EngineRunData;
import com.luna.common.engine.spi.NodeSpi;

/**
*
Expand Down Expand Up @@ -40,4 +43,11 @@ public interface EngineNode<T> {
*/
boolean couldContinueExecute(EngineContext engineContext);

/**
* 获取扩展spi
*
* @return
*/
List<NodeSpi> getSpiList();

}
14 changes: 14 additions & 0 deletions src/main/java/com/luna/common/engine/task/NodeParallelTask.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.luna.common.engine.task;

import java.util.List;

import com.luna.common.engine.spi.NodeSpi;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.luna.common.engine.model.EngineContext;
import com.luna.common.engine.model.EngineRunData;
import com.luna.common.engine.spi.BatchNodeNodeSpi;
import com.luna.common.exception.BaseException;

/**
Expand Down Expand Up @@ -41,6 +46,15 @@ public Object execute() throws BaseException {
try {
// Node前置检查
Object o = engineNode.invokeNode(engineRunData, engineContext);
List<NodeSpi> spiList = engineNode.getSpiList();
if (CollectionUtils.isNotEmpty(spiList)) {
for (NodeSpi nodeSpi : spiList) {
BatchNodeNodeSpi batchNodeNodeSpi = (BatchNodeNodeSpi)nodeSpi;
if (batchNodeNodeSpi.isAccept(engineRunData)) {
batchNodeNodeSpi.invoke(engineRunData);
}
}
}
engineNode.afterInvoke(engineRunData, engineContext);
// 后置处理
return o;
Expand Down
28 changes: 27 additions & 1 deletion src/test/java/com/luna/common/utils/EngineNodeTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.luna.common.utils;

import org.junit.Test;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson2.JSON;
import com.luna.common.engine.model.EngineContext;
import com.luna.common.engine.model.EngineRunData;
import com.luna.common.engine.model.NodeChain;
import com.luna.common.engine.model.NodeConf;
import com.luna.common.engine.spi.AbstractBatchNodeNodeSpi;
import com.luna.common.engine.task.AbstractEngineExecute;
import com.luna.common.engine.task.AbstractEngineNode;
import com.luna.common.spring.SpringBeanService;
Expand Down Expand Up @@ -49,6 +53,28 @@ public TestEngineNode testEngineNode() {
public SpringBeanService springBeanService() {
return new SpringBeanService();
}

@Bean
public TestBatchNodeNodeSpi testBatchNodeNodeSpi() {
return new TestBatchNodeNodeSpi();
}
}

@Component
public static class TestBatchNodeNodeSpi extends AbstractBatchNodeNodeSpi<TestAdaptor> implements InitializingBean {

@Autowired
private TestEngineNode testEngineNode;

@Override
public void invoke(EngineRunData engineRunData) {
System.out.println(JSON.toJSONString(engineRunData));
}

@Override
public void afterPropertiesSet() {
testEngineNode.add(this);
}
}

@Component
Expand Down Expand Up @@ -85,7 +111,7 @@ public static class EngineFlow {
testLink.add("1", TestEngineNode.class, new NodeConf(true, 100));
testLink.add("1", TestEngineNode.class, new NodeConf(true, 100));
testLink.add("2", TestEngineNode.class, new NodeConf(true, 100));
testLink.add("2", TestEngineNode.class, new NodeConf(false, 100));
testLink.add("2", TestEngineNode.class, new NodeConf(true, 100));
}
}

Expand Down

0 comments on commit dd281d3

Please sign in to comment.