定义可装配的流程编排器

流程编排器

ProcessorArrayList.java

package com;


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

/**
 *
 * @author lingao
 * @version $Id: ProcessorArrayList.java, v 0.1 2018年12月21日 1:32 PM lingao Exp $
 */
public final class ProcessorArrayList<E extends Object> extends ArrayList<E> {

    private List<E> procList;

    private final String name;

    public ProcessorArrayList(String name){
        this.name = name;
    }

    public void init(){
        if(!isEmpty(procList)){
            /** 存在list中的值还是个list情况 */
            for(Object obj : procList){
                if(obj instanceof List){
                    addAll((List<E>)obj);
                }else{
                    add((E) obj);
                }
            }
        }
    }

    private boolean isEmpty(List list){
        return list == null || list.isEmpty();
    }

    /**
     * Getter method for property <tt>procList</tt>.
     *
     * @return property value of procList
     */
    public List<E> getProcList() {
        return procList;
    }

    /**
     * Setter method for property <tt>procList</tt>.
     *
     * @param procList value to be assigned to property procList
     */
    public void setProcList(List<E> procList) {
        this.procList = procList;
    }

}
package com.processor;

/**
 *
 * @author lingao
 * @version $Id: ApplyPayProcessor.java, v 0.1 2018年12月22日 6:04 PM lingao Exp $
 */
public class ApplyPayProcessor {

    public ApplyPayProcessor(){
    }
}
package com.processor;

/**
 *
 * @author lingao
 * @version $Id: RequestLogProcessor.java, v 0.1 2018年12月21日 2:22 PM lingao Exp $
 */
public class RequestLogProcessor {

    public RequestLogProcessor(){
    }
}
package com.processor;

/**
 *
 * @author lingao
 * @version $Id: ResponseLogProcessor.java, v 0.1 2018年12月21日 2:22 PM lingao Exp $
 */
public class ResponseLogProcessor {
    public ResponseLogProcessor(){
    }
}
package com.processor;

/**
 *
 * @author lingao
 * @version $Id: RiskVerifyProcessor.java, v 0.1 2018年12月21日 2:29 PM lingao Exp $
 */
public class RiskVerifyProcessor {

    public RiskVerifyProcessor(){
    }
}

xml编排

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="requestLog1Processor" class="com.processor.RequestLogProcessor" />
    <bean id="responseLog1Processor" class="com.processor.ResponseLogProcessor" />
    <bean id="riskVerify1Processor" class="com.processor.RiskVerifyProcessor" />
    <bean id="applyPay1Processor" class="com.processor.ApplyPayProcessor"/>

    <bean id="prefix" class="com.ProcessorArrayList" init-method="init">
        <constructor-arg value="prefix" />
        <property name="procList">
            <list>
                <ref bean="requestLog1Processor" />
            </list>
        </property>
    </bean>

    <bean id="suffix" class="com.ProcessorArrayList" init-method="init">
        <constructor-arg value="suffix"/>
        <property name="procList">
            <list>
                <ref bean="responseLog1Processor" />
            </list>
        </property>
    </bean>

    <bean id="commonPayProcessors" class="com.ProcessorArrayList" init-method="init">
        <constructor-arg value="commonPayProcessors"/>
        <property name="procList">
            <list>
                <ref bean="prefix"/>
                <ref bean="riskVerify1Processor"/>
                <ref bean="suffix"/>
            </list>
        </property>
    </bean>

    <bean id="testProc" class="com.ProcessorArrayList" init-method="init">
        <constructor-arg value="testProc"/>
        <property name="procList">
            <list>
                <ref bean="commonPayProcessors"/>
                <ref bean="applyPay1Processor"/>
            </list>
        </property>
    </bean>

</beans>

附带策略组装器

TaskStrategy.java

public abstract class TaskStrategy<R> {

    /**
     * 默认构造注册
     */
    public TaskStrategy() {
        // 注册到组件中
        StrategyComponent.register(getName(), this);
    }

    /**
     * 策略名称
     * @return
     */
    public abstract String getName();

    /**
     * 策略执行
     * @return
     */
    public abstract R execute(Object ...params);
}

StrategyComponent.java

public final class StrategyComponent {

    /**
     * 策略配置
     */
    private static Map<String, TaskStrategy> taskStrategyMap = Maps.newHashMap();

    /**
     * 策略注册
     * @param code
     * @param taskStrategy
     */
    protected static void register(String code, TaskStrategy taskStrategy) {
        taskStrategyMap.putIfAbsent(code, taskStrategy);
    }

    /**
     * 根据code 获取策略信息
     * @param code
     * @return
     */
    public static TaskStrategy getStrategy(String code) {
        return taskStrategyMap.get(code);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值