刚看了一遍,记录一下,加深印象
一、创建流程引擎
1、流程引擎配置 ProcessEngineConfiguration实例
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1")
.setJdbcUsername("sa")
.setJdbcPassword("")
.setJdbcDriver("org.h2.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
2、实例化ProcessEngine实例
ProcessEngine processEngine = cfg.buildProcessEngine();
二、部署流程定义
1、以BPMN 2.0格式定义流程,将BPMN 2.0 XML保存在src / main / resources文件夹中,名为XXX.bpmn20.xml。
官方请假案例:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:flowable="http://flowable.org/bpmn"
typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
targetNamespace="http://www.flowable.org/processdef">
<process id="holidayRequest" name="Holiday Request"
isExecutable="true">
<startEvent id="startEvent" />
<sequenceFlow sourceRef="startEvent" targetRef="approveTask" />
<userTask id="approveTask" name="Approve or reject request" />
<sequenceFlow sourceRef="approveTask" targetRef="decision" />
<exclusiveGateway id="decision" />
<sequenceFlow sourceRef="decision" targetRef="externalSystemCall">
<conditionExpression xsi:type="tFormalExpression"> <![CDATA[ ${approved} ]]>
</conditionExpression>
</sequenceFlow>
<sequenceFlow sourceRef="decision" targetRef="sendRejectionMail">
<conditionExpression xsi:type="tFormalExpression"> <![CDATA[ ${!approved} ]]>
</conditionExpression>
</sequenceFlow>
<serviceTask id="externalSystemCall" name="Enter holidays in external system"
flowable:class="org.flowable.CallExternalSystemDelegate" />
<sequenceFlow sourceRef="externalSystemCall" targetRef="holidayApprovedTask" />
<userTask id="holidayApprovedTask" name="Holiday approved" />
<sequenceFlow sourceRef="holidayApprovedTask" targetRef="approveEnd" />
<serviceTask id="sendRejectionMail" name="Send out rejection email"
flowable:class="org.flowable.SendRejectionMail" />
<sequenceFlow sourceRef="sendRejectionMail" targetRef="rejectEnd" />
<endEvent id="approveEnd" />
<endEvent id="rejectEnd" />
</process>
</definitions>
2、将流程定义部署到Flowable引擎
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("holiday-request.bpmn20.xml")
.deploy();
可以通过API查询引擎知道流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.deploymentId(deployment.getId())
.singleResult();
System.out.println("Found process definition : " + processDefinition.getName());
三、启动流程实例
1、初始流程变量,收集的数据作为java.util.Map实例传递
Scanner scanner= new Scanner(System.in);
System.out.println("Who are you?");
String employee = scanner.nextLine();
System.out.println("How many holidays do you want to request?");
Integer nrOfHolidays = Integer.valueOf(scanner.nextLine());
System.out.println("Why do you need them?");
String description = scanner.nextLine();
2、使用id启动流程实例
假设:<process id="holidayRequest" name="Holiday Request" isExecutable="true">
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("employee", employee);
variables.put("nrOfHolidays", nrOfHolidays);
variables.put("description", description);
ProcessInstance processInstance =
runtimeService.startProcessInstanceByKey("holidayRequest", variables);
四、查询任务和完成任务
1、为用户任务配置用户
管理员组:<userTask id="approveTask" name="Approve or reject request" flowable:candidateGroups="managers"/>
请假的申请者:<userTask id="holidayApprovedTask" name="Holiday approved" flowable:assignee="${employee}"/>
2、获得任务列表
TaskService taskService = processEngine.getTaskService();
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
System.out.println("You have " + tasks.size() + " tasks:");
for (int i=0; i<tasks.size(); i++) {
System.out.println((i+1) + ") " + tasks.get(i).getName());
}
3、显示特定任务的实际请求
System.out.println("Which task would you like to complete?");
int taskIndex = Integer.valueOf(scanner.nextLine());
Task task = tasks.get(taskIndex - 1);
Map<String, Object> processVariables = taskService.getVariables(task.getId());
System.out.println(processVariables.get("employee") + " wants " +
processVariables.get("nrOfHolidays") + " of holidays. Do you approve this?");
4、经理处理请求
boolean approved = scanner.nextLine().toLowerCase().equals("y");
variables = new HashMap<String, Object>();
variables.put("approved", approved);
taskService.complete(task.getId(), variables);
五、编写JavaDelegate,实现自动逻辑
<serviceTask id="externalSystemCall" name="Enter holidays in external system"
flowable:class="org.flowable.CallExternalSystemDelegate"/>
创建一个新类,将org.flowable作为包名,将CallExternalSystemDelegate作为类名。使该类实现org.flowable.engine.delegate.JavaDelegate接口并实现execute方法:
public class CallExternalSystemDelegate implements JavaDelegate {
public void execute(DelegateExecution execution) {
System.out.println("Calling the external system for employee "
+ execution.getVariable("employee"));
}
}
六、显示流程实例的持续时间
HistoryService historyService = processEngine.getHistoryService();
List<HistoricActivityInstance> activities =
historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstance.getId())//特定流程实例的id
.finished()//已完成的
.orderByHistoricActivityInstanceEndTime().asc()//按结束时间排序
.list();
for (HistoricActivityInstance activity : activities) {
System.out.println(activity.getActivityId() + " took "
+ activity.getDurationInMillis() + " milliseconds");
}