概述
jBPM5的 User Task是指节点必须有人的参与后才能够完成,是BPM重要特新的体现,User Task必须使用数据库存储流程运行时数据,如下为我们本文中使用到的流程示意:
为了方便数据库方面查看,我们使用Mysql存数流程运行时状态,我们使用如下SQL语句创建数据库用户:
CREATE DATABASE jbpm;
create user 'jbpm_user'@'localhost' identified by 'jbpm_pass';
grant all on jbpm.* to jbpm_user@'localhost';
FLUSH PRIVILEGES;
我们本文的内容包括:
- 基本运行User Task
- 自定义自己的SyncWSHumanTaskHandler执行User Task
- 使用Mysql为jBPM执行数据库
- User Task中变量Mapping
- User Task执行过程数据库操作
基本运行User Task
基本运行User Task的完全代码见github,我们抽取如下部分:
setupDataSource();
KnowledgeBase kbase = readKnowledgeBase(JBPM5_BASIC_NAME);
StatefulKnowledgeSession ksession = newStatefulKnowledgeSession(kbase);
LocalTaskService localTaskService = getTaskServiceAndRegisterHumanTaskHandler(ksession);
Map<String, Object> params = new HashMap<String, Object>();
ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn.hello", params);
System.out.println("Start Process... : " + processInstance.getId());
List<TaskSummary> list = localTaskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
TaskSummary taskSummary = list.get(0);
localTaskService.start(taskSummary.getId(), "john");
localTaskService.complete(taskSummary.getId(), "john", null);
如上基本运行User Task可以分为:设定数据源,加载KnowledgeBase创建ksession,创建并注册HumanTaskJandler,启动流程,执行User Task。
自定义自己的SyncWSHumanTaskHandler执行User Task
自定义自己的SyncWSHumanTaskHandler执行User Task的代码位于github目录下,运行JBPM5HumanTaskCustomized将运行自定义的SyncWSHumanTaskHandler执行User Task,CustomizedSyncWSHumanTaskHandler为我们自定义的Handler,与SyncWSHumanTaskHandler相比,我们只是在executeWorkItem方法中添加了三行,在方法一开始执行输出workItem内容代码:
System.out.println("Before execute WorkItem, workItem.getId() = " + workItem.getId() + ", workItem.getName() = " + workItem.getName() + ", workItem.getProcessInstanceId() = " + workItem.getProcessInstanceId() + ", workItem.getState() = " + workItem.getState() + ", workItem.getParameters() = " + workItem.getParameters() + ", workItem.getResults() = " + workItem.getResults());
在executeWorkItem方法执行结束后,打印输出生成Task的ID,以及workItem内容,注意,executeWorkItem方法执行结束是将User Task中定义的内容保存到数据库中,如下代码:
try {
System.out.println("Task id(auto-generated by JPA): " + task.getId());
System.out.prin