spring 工作流实现

1、执行器:

bean定义:

public class ProcessContext implements Serializable {
    private String code;//工作组模版编码
    private long beginTime = 0L;//开始时间
    private long endTime = 0L;//结束时间
    private long timeout = 0L;//超时时间
    private Boolean needBreak = false;//是否中断
    private boolean isSuccess = true;//执行是否成功
    private int errorCode = 0;//错误编码
    private String errorMsg = "";//错误信息

    public Boolean getNeedBreak() {
        return this.needBreak;
    }

    public ProcessContext setNeedBreak(Boolean needBreak) {
        this.needBreak = needBreak;
        return this;
    }

    public boolean isSuccess() {
        return this.isSuccess;
    }

    public ProcessContext setSuccess(boolean success) {
        this.isSuccess = success;
        return this;
    }

    public String getErrorMsg() {
        return this.errorMsg;
    }

    public ProcessContext setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        this.needBreak = true;
        this.isSuccess = false;
        return this;
    }

    public ProcessContext() {
    }

    public String getCode() {
        return this.code;
    }

    public long getBeginTime() {
        return this.beginTime;
    }

    public long getEndTime() {
        return this.endTime;
    }

    public long getTimeout() {
        return this.timeout;
    }

    public int getErrorCode() {
        return this.errorCode;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setBeginTime(long beginTime) {
        this.beginTime = beginTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof ProcessContext)) {
            return false;
        } else {
            ProcessContext other = (ProcessContext)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$code = this.getCode();
                Object other$code = other.getCode();
                if (this$code == null) {
                    if (other$code != null) {
                        return false;
                    }
                } else if (!this$code.equals(other$code)) {
                    return false;
                }

                if (this.getBeginTime() != other.getBeginTime()) {
                    return false;
                } else if (this.getEndTime() != other.getEndTime()) {
                    return false;
                } else if (this.getTimeout() != other.getTimeout()) {
                    return false;
                } else {
                    label56: {
                        Object this$needBreak = this.getNeedBreak();
                        Object other$needBreak = other.getNeedBreak();
                        if (this$needBreak == null) {
                            if (other$needBreak == null) {
                                break label56;
                            }
                        } else if (this$needBreak.equals(other$needBreak)) {
                            break label56;
                        }

                        return false;
                    }

                    if (this.isSuccess() != other.isSuccess()) {
                        return false;
                    } else if (this.getErrorCode() != other.getErrorCode()) {
                        return false;
                    } else {
                        Object this$errorMsg = this.getErrorMsg();
                        Object other$errorMsg = other.getErrorMsg();
                        if (this$errorMsg == null) {
                            if (other$errorMsg == null) {
                                return true;
                            }
                        } else if (this$errorMsg.equals(other$errorMsg)) {
                            return true;
                        }

                        return false;
                    }
                }
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof ProcessContext;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $code = this.getCode();
        int result = result * 59 + ($code == null ? 43 : $code.hashCode());
        long $beginTime = this.getBeginTime();
        result = result * 59 + (int)($beginTime >>> 32 ^ $beginTime);
        long $endTime = this.getEndTime();
        result = result * 59 + (int)($endTime >>> 32 ^ $endTime);
        long $timeout = this.getTimeout();
        result = result * 59 + (int)($timeout >>> 32 ^ $timeout);
        Object $needBreak = this.getNeedBreak();
        result = result * 59 + ($needBreak == null ? 43 : $needBreak.hashCode());
        result = result * 59 + (this.isSuccess() ? 79 : 97);
        result = result * 59 + this.getErrorCode();
        Object $errorMsg = this.getErrorMsg();
        result = result * 59 + ($errorMsg == null ? 43 : $errorMsg.hashCode());
        return result;
    }

    public String toString() {
        return "ProcessContext(code=" + this.getCode() + ", beginTime=" + this.getBeginTime() + ", endTime=" + this.getEndTime() + ", timeout=" + this.getTimeout() + ", needBreak=" + this.getNeedBreak() + ", isSuccess=" + this.isSuccess() + ", errorCode=" + this.getErrorCode() + ", errorMsg=" + this.getErrorMsg() + ")";
    }
}

异常类

package com.fordeal.camel.proctrl.exception;

public class ProctrlException extends Exception {
    private Integer code;

    public ProctrlException() {
    }

    public ProctrlException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    public ProctrlException(String message) {
        super(message);
    }

    public ProctrlException(String message, Throwable cause) {
        super(message, cause);
    }

    public ProctrlException(Throwable cause) {
        super(cause);
    }

    protected ProctrlException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public Integer getCode() {
        return this.code;
    }
}

接口类:

public interface ProcessController {
    void process(ProcessContext var1) throws ProctrlException;
}

实现类:

public class ProcessControllerImpl implements ProcessController {
    private Logger logger = LoggerFactory.getLogger(ProcessController.class);
    private Map<String, ProcessTemplate> templateConfig = null;

    public ProcessControllerImpl() {
    }

    public void process(ProcessContext context) throws ProctrlException {
        if (context == null) {
            throw new ProctrlException(ErrorCode.CONTEXT_IS_NULL.getCode());
        } else {
            String businessCode = context.getCode();
            if (StringUtils.isEmpty(businessCode)) {
                throw new ProctrlException(ErrorCode.BUSINESSCODE_IS_NULL.getCode());
            } else {
                ProcessTemplate processTemplate = (ProcessTemplate)this.templateConfig.get(businessCode);
                if (processTemplate == null) {
                    throw new ProctrlException(ErrorCode.PROCESSTEMPLATE_IS_NULL.getCode());
                } else {
                    List<StepAction> actionList = processTemplate.getProcessConfig();
                    if (actionList != null && actionList.size() != 0) {
                        Iterator var5 = actionList.iterator();

                        while(var5.hasNext()) {
                            StepAction action = (StepAction)var5.next();
                            long stime = System.currentTimeMillis();
                            boolean var21 = false;

                            long etime;
                            long diffTime;
                            label115: {
                                try {
                                    var21 = true;
                                    action.process(context);
                                    if (context.getNeedBreak()) {
                                        var21 = false;
                                        break label115;
                                    }

                                    var21 = false;
                                } catch (ProctrlException var22) {
                                    this.logger.warn("process_action={} happened e={}, context={}", new Object[]{action.getClass().getSimpleName(), var22.getMessage(), JSONObject.toJSONString(context)});
                                    context.setErrorMsg(var22.getMessage());
                                    throw var22;
                                } catch (Exception var23) {
                                    this.logger.error("exception_process_action={} happened e={}, context={}", new Object[]{action.getClass().getSimpleName(), ExceptionUtils.getStackTrace(var23), JSONObject.toJSONString(context)});
                                    context.setErrorMsg(var23.getMessage());
                                    throw new ProctrlException(var23);
                                } finally {
                                    if (var21) {
                                        long var14 = System.currentTimeMillis();
                                        long diffTime = var14 - stime;
                                        if (diffTime > 200L) {
                                            this.logger.warn("slow_process action:{} is slow,cost time:{}", action.getClass().getSimpleName(), diffTime);
                                        }

                                    }
                                }

                                etime = System.currentTimeMillis();
                                diffTime = etime - stime;
                                if (diffTime > 200L) {
                                    this.logger.warn("slow_process action:{} is slow,cost time:{}", action.getClass().getSimpleName(), diffTime);
                                }
                                continue;
                            }

                            etime = System.currentTimeMillis();
                            diffTime = etime - stime;
                            if (diffTime > 200L) {
                                this.logger.warn("slow_process action:{} is slow,cost time:{}", action.getClass().getSimpleName(), diffTime);
                            }
                            break;
                        }

                    } else {
                        throw new ProctrlException(ErrorCode.PROCESSCONFIG_IS_NULL.getCode());
                    }
                }
            }
        }
    }

    public Map<String, ProcessTemplate> getTemplateConfig() {
        return this.templateConfig;
    }

    public void setTemplateConfig(Map<String, ProcessTemplate> templateConfig) {
        this.templateConfig = templateConfig;
    }
}


public class ProcessTemplate {
    private List<StepAction> processConfig = null;

    public ProcessTemplate() {
    }

    public List<StepAction> getProcessConfig() {
        return this.processConfig;
    }

    public void setProcessConfig(List<StepAction> processConfig) {
        this.processConfig = processConfig;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof ProcessTemplate)) {
            return false;
        } else {
            ProcessTemplate other = (ProcessTemplate)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$processConfig = this.getProcessConfig();
                Object other$processConfig = other.getProcessConfig();
                if (this$processConfig == null) {
                    if (other$processConfig != null) {
                        return false;
                    }
                } else if (!this$processConfig.equals(other$processConfig)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof ProcessTemplate;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $processConfig = this.getProcessConfig();
        int result = result * 59 + ($processConfig == null ? 43 : $processConfig.hashCode());
        return result;
    }

    public String toString() {
        return "ProcessTemplate(processConfig=" + this.getProcessConfig() + ")";
    }
}

2、配置文件:spring-workflow.xml

<bean id="procContrl" class="**.ProcessControllerImpl">
        <property name="templateConfig">
            <map>
                <entry key="test-til" value-ref="mytpl"/>
            </map>
        </property>
</bean>
<bean id="mytpl" class="***.ProcessTemplate">
        <property name="processConfig">
            <list>
                <ref bean="1Action"/>
                <ref bean="2Action"/>
                <ref bean="3Action"/>
            </list>
        </property>
    </bean>