工作流主要運(yùn)用到反射機(jī)制
- 創(chuàng)建一張工作流表
如:主鍵 | 工作流Code | 工作流內(nèi)容
其中工作流,內(nèi)容為Json格式
- 工作流內(nèi)容如下
{
"procCode": "OPS",
"procName": "c端補(bǔ)齊(乘客信息補(bǔ)全)",
"taskControlVoList": [
{
"sortNo": "10",
"taskCode": "OPC",
"taskName": "檢查對(duì)應(yīng)的訂單信息是否存在",
"tasklassName": "com.xf.flow.service.impl.OrderPeople10ServImpl"
},
{
"sortNo": "20",
"taskCode": "OPD",
"taskName": "乘客數(shù)據(jù)庫(kù)落庫(kù)",
"tasklassName": "com.xf.flow.service.impl.OrderPeople20ServImpl"
},
{
"sortNo": "90",
"taskCode": "RSP",
"taskName": "響應(yīng)消息",
"tasklassName": "com.xf.flow.service.impl.ApiResponseServImpl"
}
]
}
3.java代碼現(xiàn)根據(jù)工作流的Code 獲取當(dāng)前的工作流
4.jsonStr ————>JsonObject————>獲取taskControlVoList
5.java的工作流代碼
@Override
public ApiResponse deal(String itemNo, Object objVo, String skey, InnerVo innerVo) {
ApiResponse apiResponse = new ApiResponse();
//查詢itemNo獲取流程實(shí)例
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();
//初始化內(nèi)部數(shù)據(jù)表
String dataNo = taskManagerServ.initData(itemNo,skey,innerVo);
//循環(huán)依次執(zhí)行流程任務(wù)
String taskNo = null;
while (itTask.hasNext()){
TaskControlVo taskControlVo = itTask.next();
String taskClassName = taskControlVo.getTasklassName();
String taskItemNo = taskControlVo.getTaskCode();
String taskName = taskControlVo.getTaskName();
try {
//初始化任務(wù)
taskNo = taskManagerServ.initTask(taskItemNo,dataNo,innerVo);
//執(zhí)行任務(wù)
TaskServ taskServ = (TaskServ) SpringFactory.getBean(Class.forName(taskClassName));
taskServ.init(taskNo,objVo);
TaskAnswerVo taskAnwserVo = taskServ.deal(taskNo,objVo);
//判斷任務(wù)執(zhí)行結(jié)果
apiResponse = taskAnwserVo.getApiResponse();
if(StringUtils.equals(ResponseCodeEnums.success.getStatus(),apiResponse.getStatus())) {
//完成任務(wù)
logger.info("==========>>>>> task execute success !!! taskCode={}, taskName={}, status={}, msg={}, nextTask={}",
taskItemNo,taskName,apiResponse.getStatus(),apiResponse.getMessage(),taskAnwserVo.isNextTask());
taskManagerServ.finTask(taskNo, apiResponse.getMessage());
}else {
//異常任務(wù)
logger.info("==========>>>>> task execute error !!! taskCode={}, taskName={}, status={}, msg={}, nextTask={}",
taskItemNo,taskName,apiResponse.getStatus(),apiResponse.getMessage(),taskAnwserVo.isNextTask());
taskManagerServ.errTask(taskNo, apiResponse.getMessage());
}
//判斷流程是否繼續(xù)
if(!taskAnwserVo.isNextTask()){
break;
}
} catch (Exception e) {
//異常任務(wù)
String errorMsg = getStackTrace(e);
taskManagerServ.errTask(taskNo, errorMsg);
logger.error("==========>>>>> Execute Task Error!!! skey={}, taskControlVo={}, errorMsg={}",skey,taskControlVo,errorMsg);
//響應(yīng)異常
return new ApiResponse(ResponseCodeEnums.flow_error);
}
}
if(StringUtils.equals(ResponseCodeEnums.success.getStatus(),apiResponse.getStatus())) {
//歸檔完成流程
taskManagerServ.finData(dataNo);
}
return apiResponse;
}
SpringFactory實(shí)現(xiàn)類
/**
* 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);
}
}