其中,Create a simple project 要选中。
这里的Group id:必须是 org.flowable (后面的流程配置文件需要用到这个包路径)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.flowablegroupId>
<artifactId>HolidayRequestartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.flowablegroupId>
<artifactId>flowable-engineartifactId>
<version>6.1.2version>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<version>1.3.176version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
dependencies>
project>
package org.flowable;
public class HolidayRequest {
public static void main(String[] args) {
}
}
src/main/resources/holiday-request.bpmn20.xml
<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" flowable:candidateGroups="managers"/>
<sequenceFlow sourceRef="approveTask" targetRef="decision" />
<exclusiveGateway id="decision" />
<sequenceFlow sourceRef="decision" targetRef="externalSystemCall">
<conditionExpression xsi:type="tFormalExpression">
conditionExpression>
sequenceFlow>
<sequenceFlow sourceRef="decision" targetRef="sendRejectionMail">
<conditionExpression xsi:type="tFormalExpression">
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" flowable:assignee="${employee}"/>
<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>
src/main/resources/log4j.properties
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n
这个类在前面的流程配置中被设置为id = externalSystemCall 的 serviceTask 的执行类。
package org.flowable;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
public class CallExternalSystemDelegate implements JavaDelegate {
public void execute(DelegateExecution execution) {
System.out.println("Calling the external system for employee "
+ execution.getVariable("employee"));
}
}
package holidy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.flowable.engine.HistoryService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.task.Task;
public class HolidayRequest {
public static void main(String[] args) {
// 创建流程引擎相关数据库表
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);
ProcessEngine processEngine = cfg.buildProcessEngine();
// 设计好的流程部署到流程引擎中(数据库)
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("holiday-request.bpmn20.xml")
.deploy();
// 根据流程ID查找已经部署的流程
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.deploymentId(deployment.getId()).singleResult();
System.out.println(" 发现定义的流程 : " + processDefinition.getName());
// 模拟请假流程,输入相关的参数
Scanner scanner = new Scanner(System.in);
// 请假人
System.out.println("你是谁?");
String employee = scanner.nextLine();
// 请假天数
System.out.println("你要请几天假?");
Integer nrOfHolidays = Integer.valueOf(scanner.nextLine());
// 请假理由
System.out.println("请假理由?");
String description = scanner.nextLine();
// 流程引擎服务
RuntimeService runtimeService = processEngine.getRuntimeService();
Map variables = new HashMap();
variables.put("employee", employee);
variables.put("nrOfHolidays", nrOfHolidays);
variables.put("description", description);
// 构建流程运行参数,启动流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);
// 获取待办任务列表
TaskService taskService = processEngine.getTaskService();
List tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
System.out.println("你有 " + tasks.size() + " 个待办任务:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ") " + tasks.get(i).getName());
}
// 任务选择
System.out.println("请选择你要处理的任务ID?");
int taskIndex = Integer.valueOf(scanner.nextLine());
Task task = tasks.get(taskIndex - 1);
Map processVariables = taskService.getVariables(task.getId());
// 流程审批
System.out.println(processVariables.get("employee") + " 要请 " + processVariables.get("nrOfHolidays")
+ " 天假. 是否同意? 同意回复 y ");
boolean approved = scanner.nextLine().toLowerCase().equals("y");
variables = new HashMap();
variables.put("approved", approved);
taskService.complete(task.getId(), variables);
// 审批历史数据
HistoryService historyService = processEngine.getHistoryService();
List activities = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstance.getId()).finished().orderByHistoricActivityInstanceEndTime().asc()
.list();
for (HistoricActivityInstance activity : activities) {
System.out.println(activity.getActivityId() + " took " + activity.getDurationInMillis() + " milliseconds");
}
}
}