美文网首页
Java 实现简单工作流

Java 实现简单工作流

作者: 消失的码农 | 来源:发表于2019-07-12 14:37 被阅读0次

工作流主要运用到反射机制

  1. 创建一张工作流表
    如:主键 | 工作流Code | 工作流内容

其中工作流,内容为Json格式

  1. 工作流内容如下
{
  "procCode": "OPS",
  "procName": "c端补齐(乘客信息补全)",
  "taskControlVoList": [
    {
      "sortNo": "10",
      "taskCode": "OPC",
      "taskName": "检查对应的订单信息是否存在",
      "tasklassName": "com.xf.flow.service.impl.OrderPeople10ServImpl"
    },
    {
      "sortNo": "20",
      "taskCode": "OPD",
      "taskName": "乘客数据库落库",
      "tasklassName": "com.xf.flow.service.impl.OrderPeople20ServImpl"
    },
    {
      "sortNo": "90",
      "taskCode": "RSP",
      "taskName": "响应消息",
      "tasklassName": "com.xf.flow.service.impl.ApiResponseServImpl"
    }
  ]
}

3.java代码现根据工作流的Code 获取当前的工作流
4.jsonStr ————>JsonObject————>获取taskControlVoList
5.java的工作流代码

 @Override
    public ApiResponse deal(String itemNo, Object objVo, String skey, InnerVo innerVo) {

        ApiResponse apiResponse = new ApiResponse();
        //查询itemNo获取流程实例
        ProcControlVo procControlVo = codeLibraryDao.queryProcConfig(itemNo);
        if (procControlVo == null) {
            return new ApiResponse(ResponseCodeEnums.stream_no_exist);
        }
        List<TaskControlVo>  taskControlVoList = procControlVo.getTaskControlVoList();
        taskControlVoList.sort((TaskControlVo h1, TaskControlVo h2) -> h1.getSortNo().compareTo(h2.getSortNo()));
        Iterator<TaskControlVo> itTask = taskControlVoList.iterator();

        //初始化内部数据表
        String dataNo = taskManagerServ.initData(itemNo,skey,innerVo);

        //循环依次执行流程任务
        String taskNo = null;
        while (itTask.hasNext()){
            TaskControlVo taskControlVo = itTask.next();
            String taskClassName = taskControlVo.getTasklassName();
            String taskItemNo = taskControlVo.getTaskCode();
            String taskName = taskControlVo.getTaskName();
            try {

                //初始化任务
                taskNo = taskManagerServ.initTask(taskItemNo,dataNo,innerVo);

                //执行任务
                TaskServ taskServ = (TaskServ) SpringFactory.getBean(Class.forName(taskClassName));
                taskServ.init(taskNo,objVo);
                TaskAnswerVo taskAnwserVo = taskServ.deal(taskNo,objVo);

                //判断任务执行结果
                apiResponse = taskAnwserVo.getApiResponse();
                if(StringUtils.equals(ResponseCodeEnums.success.getStatus(),apiResponse.getStatus())) {
                    //完成任务
                    logger.info("==========>>>>> task execute success !!! taskCode={}, taskName={}, status={}, msg={}, nextTask={}",
                            taskItemNo,taskName,apiResponse.getStatus(),apiResponse.getMessage(),taskAnwserVo.isNextTask());
                    taskManagerServ.finTask(taskNo, apiResponse.getMessage());
                }else {
                    //异常任务
                    logger.info("==========>>>>> task execute error !!! taskCode={}, taskName={}, status={}, msg={}, nextTask={}",
                            taskItemNo,taskName,apiResponse.getStatus(),apiResponse.getMessage(),taskAnwserVo.isNextTask());
                    taskManagerServ.errTask(taskNo, apiResponse.getMessage());
                }

                //判断流程是否继续
                if(!taskAnwserVo.isNextTask()){
                    break;
                }
            } catch (Exception e) {
                //异常任务
                String errorMsg = getStackTrace(e);
                taskManagerServ.errTask(taskNo, errorMsg);
                logger.error("==========>>>>> Execute Task Error!!! skey={}, taskControlVo={}, errorMsg={}",skey,taskControlVo,errorMsg);
                //响应异常
                return new ApiResponse(ResponseCodeEnums.flow_error);
            }
        }
        if(StringUtils.equals(ResponseCodeEnums.success.getStatus(),apiResponse.getStatus())) {
            //归档完成流程
            taskManagerServ.finData(dataNo);
        }
        return apiResponse;
    }
image.png

SpringFactory实现类

/**
 * Created by
 */
@Component
public class SpringFactory implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringFactory.applicationContext == null) {
            SpringFactory.applicationContext = applicationContext;
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

相关文章

网友评论

      本文标题:Java 实现简单工作流

      本文链接:https://www.haomeiwen.com/subject/cfwekctx.html