mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 04:02:09 +08:00
组件支持spring的自动扫描
增加基于spring的测试用例
This commit is contained in:
32
pom.xml
32
pom.xml
@@ -12,14 +12,15 @@
|
||||
<java.version>1.7</java.version>
|
||||
<commons.lang3.version>3.4</commons.lang3.version>
|
||||
<commons-collections.version>4.1</commons-collections.version>
|
||||
<spring.version>4.1.7.RELEASE</spring.version>
|
||||
<commons-io.version>2.4</commons-io.version>
|
||||
<spring.version>4.2.6.RELEASE</spring.version>
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
<log4j-slf4j.version>1.7.5</log4j-slf4j.version>
|
||||
<slf4j.version>1.7.13</slf4j.version>
|
||||
<fastjson.version>1.2.7</fastjson.version>
|
||||
<classfinder.version>1.0</classfinder.version>
|
||||
<dom4j.version>1.6.1</dom4j.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -33,9 +34,24 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -58,16 +74,16 @@
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>${fastjson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.poolik</groupId>
|
||||
<artifactId>classfinder</artifactId>
|
||||
<version>${classfinder.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>dom4j</groupId>
|
||||
<artifactId>dom4j</artifactId>
|
||||
<version>${dom4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -65,7 +65,7 @@ public class FlowExecutor {
|
||||
List<Condition> conditionList = chain.getConditionList();
|
||||
|
||||
List<Node> nodeList = null;
|
||||
Component component = null;
|
||||
NodeComponent component = null;
|
||||
for(Condition condition : conditionList){
|
||||
nodeList = condition.getNodeList();
|
||||
|
||||
@@ -76,6 +76,8 @@ public class FlowExecutor {
|
||||
component.setSlotIndex(slotIndex);
|
||||
if(component.isAccess()){
|
||||
component.execute();
|
||||
}else{
|
||||
LOG.info("component[{}] do not gain access",component.getClass().getSimpleName());
|
||||
}
|
||||
}catch(Throwable t){
|
||||
if(component.isContinueOnError()){
|
||||
|
||||
@@ -17,9 +17,9 @@ import com.thebeastshop.liteflow.entity.data.Slot;
|
||||
import com.thebeastshop.liteflow.entity.monitor.CompStatistics;
|
||||
import com.thebeastshop.liteflow.monitor.MonitorBus;
|
||||
|
||||
public abstract class Component {
|
||||
public abstract class NodeComponent {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Component.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(NodeComponent.class);
|
||||
|
||||
private InheritableThreadLocal<Integer> slotIndexTL = new InheritableThreadLocal<Integer>();
|
||||
|
||||
@@ -63,7 +63,7 @@ public abstract class Component {
|
||||
this.continueOnError = continueOnError;
|
||||
}
|
||||
|
||||
public Component setSlotIndex(Integer slotIndex) {
|
||||
public NodeComponent setSlotIndex(Integer slotIndex) {
|
||||
this.slotIndexTL.set(slotIndex);
|
||||
return this;
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
*/
|
||||
package com.thebeastshop.liteflow.entity.config;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
|
||||
public class Node {
|
||||
|
||||
@@ -17,7 +17,17 @@ public class Node {
|
||||
|
||||
private String clazz;
|
||||
|
||||
private Component instance;
|
||||
private NodeComponent instance;
|
||||
|
||||
public Node(){
|
||||
|
||||
}
|
||||
|
||||
public Node(String id, String clazz, NodeComponent instance) {
|
||||
this.id = id;
|
||||
this.clazz = clazz;
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
@@ -35,11 +45,11 @@ public class Node {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public Component getInstance() {
|
||||
public NodeComponent getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void setInstance(Component instance) {
|
||||
public void setInstance(NodeComponent instance) {
|
||||
this.instance = instance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dom4j.Document;
|
||||
@@ -21,13 +22,14 @@ import org.dom4j.Element;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
import com.thebeastshop.liteflow.entity.config.Chain;
|
||||
import com.thebeastshop.liteflow.entity.config.Condition;
|
||||
import com.thebeastshop.liteflow.entity.config.Node;
|
||||
import com.thebeastshop.liteflow.entity.config.ThenCondition;
|
||||
import com.thebeastshop.liteflow.entity.config.WhenCondition;
|
||||
import com.thebeastshop.liteflow.flow.FlowBus;
|
||||
import com.thebeastshop.liteflow.spring.ComponentScaner;
|
||||
import com.thebeastshop.liteflow.util.Dom4JReader;
|
||||
import com.thebeastshop.liteflow.util.IOUtil;
|
||||
|
||||
@@ -52,26 +54,33 @@ public class FlowParser {
|
||||
try {
|
||||
Element rootElement = document.getRootElement();
|
||||
|
||||
// 解析node节点
|
||||
List<Element> nodeList = rootElement.element("nodes").elements("node");
|
||||
String id = null;
|
||||
String clazz = null;
|
||||
Node node = null;
|
||||
Component component = null;
|
||||
//判断是以spring方式注册节点,还是以xml方式注册
|
||||
Map<String, Node> nodeMap = new HashMap<String, Node>();
|
||||
for (Element e : nodeList) {
|
||||
node = new Node();
|
||||
id = e.attributeValue("id");
|
||||
clazz = e.attributeValue("class");
|
||||
node.setId(id);
|
||||
node.setClazz(clazz);
|
||||
component = (Component) Class.forName(clazz).newInstance();
|
||||
if (component == null) {
|
||||
LOG.error("couldn't find component class [{}] ", clazz);
|
||||
if(ComponentScaner.nodeComponentMap.isEmpty()){
|
||||
// 解析node节点
|
||||
List<Element> nodeList = rootElement.element("nodes").elements("node");
|
||||
String id = null;
|
||||
String clazz = null;
|
||||
Node node = null;
|
||||
NodeComponent component = null;
|
||||
for (Element e : nodeList) {
|
||||
node = new Node();
|
||||
id = e.attributeValue("id");
|
||||
clazz = e.attributeValue("class");
|
||||
node.setId(id);
|
||||
node.setClazz(clazz);
|
||||
component = (NodeComponent) Class.forName(clazz).newInstance();
|
||||
if (component == null) {
|
||||
LOG.error("couldn't find component class [{}] ", clazz);
|
||||
}
|
||||
component.setNodeId(id);
|
||||
node.setInstance(component);
|
||||
nodeMap.put(id, node);
|
||||
}
|
||||
}else{
|
||||
for(Entry<String, NodeComponent> componentEntry : ComponentScaner.nodeComponentMap.entrySet()){
|
||||
nodeMap.put(componentEntry.getKey(), new Node(componentEntry.getKey(), componentEntry.getValue().getClass().getName(), componentEntry.getValue()));
|
||||
}
|
||||
component.setNodeId(id);
|
||||
node.setInstance(component);
|
||||
nodeMap.put(id, node);
|
||||
}
|
||||
|
||||
// 解析chain节点
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* <p>Title: liteFlow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* <p>Copyright: Copyright (c) 2017</p>
|
||||
* @author Bryan.Zhang
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2017-11-23
|
||||
* @version 1.0
|
||||
*/
|
||||
package com.thebeastshop.liteflow.spring;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.PriorityOrdered;
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
import com.thebeastshop.liteflow.entity.config.Node;
|
||||
|
||||
public class ComponentScaner implements BeanPostProcessor, PriorityOrdered {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ComponentScaner.class);
|
||||
|
||||
public static Map<String, NodeComponent> nodeComponentMap = new HashMap<String, NodeComponent>();
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
Class clazz = bean.getClass();
|
||||
if(NodeComponent.class.isAssignableFrom(clazz)){
|
||||
LOG.info("component[{}] has been found",beanName);
|
||||
NodeComponent nodeComponent = (NodeComponent)bean;
|
||||
nodeComponent.setNodeId(beanName);
|
||||
nodeComponentMap.put(beanName, nodeComponent);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.thebeastshop.liteflow.test;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.thebeastshop.liteflow.core.FlowExecutor;
|
||||
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:spring-test.xml" })
|
||||
public class TestWithSpringMain {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
@Test
|
||||
public void test1() throws Exception {
|
||||
String response = flowExecutor.execute("chain2", "it's a request");
|
||||
System.out.println(response);
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,12 @@
|
||||
*/
|
||||
package com.thebeastshop.liteflow.test.component;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public class AComponent extends Component {
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
|
||||
@Component("a")
|
||||
public class AComponent extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
|
||||
@@ -12,9 +12,12 @@ package com.thebeastshop.liteflow.test.component;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public class BComponent extends Component {
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
|
||||
@Component("b")
|
||||
public class BComponent extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
|
||||
@@ -9,9 +9,12 @@
|
||||
*/
|
||||
package com.thebeastshop.liteflow.test.component;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public class CComponent extends Component {
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
|
||||
@Component("c")
|
||||
public class CComponent extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
|
||||
@@ -9,10 +9,13 @@
|
||||
*/
|
||||
package com.thebeastshop.liteflow.test.component;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
import com.thebeastshop.liteflow.entity.data.Slot;
|
||||
|
||||
public class DComponent extends Component {
|
||||
@Component("d")
|
||||
public class DComponent extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
|
||||
@@ -9,9 +9,12 @@
|
||||
*/
|
||||
package com.thebeastshop.liteflow.test.component;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public class EComponent extends Component {
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
|
||||
@Component("e")
|
||||
public class EComponent extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
|
||||
@@ -9,9 +9,12 @@
|
||||
*/
|
||||
package com.thebeastshop.liteflow.test.component;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public class FComponent extends Component {
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
|
||||
@Component("f")
|
||||
public class FComponent extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
|
||||
@@ -9,9 +9,12 @@
|
||||
*/
|
||||
package com.thebeastshop.liteflow.test.component;
|
||||
|
||||
import com.thebeastshop.liteflow.core.Component;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public class GComponent extends Component {
|
||||
import com.thebeastshop.liteflow.core.NodeComponent;
|
||||
|
||||
@Component("g")
|
||||
public class GComponent extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
|
||||
20
src/test/resources/spring-test.xml
Normal file
20
src/test/resources/spring-test.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd"
|
||||
default-autowire="byName">
|
||||
|
||||
<context:component-scan base-package="com.thebeastshop.liteflow.test.component" />
|
||||
<bean class="com.thebeastshop.liteflow.spring.ComponentScaner"/>
|
||||
|
||||
<bean id="flowExecutor" class="com.thebeastshop.liteflow.core.FlowExecutor" init-method="init">
|
||||
<property name="rulePath">
|
||||
<list>
|
||||
<value>flow.xml</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user