Java工作流的应用在目前十分广泛,能够熟练的实现工作流也是一种本事,本篇文章就让小编带你了解下其中的实现关键。
我们知道,工作流的实现主要依靠反射机制,想要实现它,首先我们先来创建一张工作流表
创建一张工作流表
如:主键 | 工作流Code | 工作流内容
其中工作流内容为Json格式
工作流内容
{ "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" } ] }
aava代码现根据工作流的Code 获取当前的工作流
jsonStr ————>JsonObject————>获取taskControlVoList
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; }
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常见问题及解决方法的小伙伴们请持续关注本网站吧。
推荐阅读: