组件支持spring的自动扫描

增加基于spring的测试用例
This commit is contained in:
bryan.zhang
2017-11-23 14:27:49 +08:00
parent b0c8cc115d
commit 07c0ab8b41
15 changed files with 203 additions and 49 deletions

View File

@@ -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()){

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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节点

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View 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>