发现一个学习flowable很好的教程,分享给大家。
Flowable BPMN 用户手册 (v 6.3.0)
下载flowable 6.4.0版本
参考链接
并附上完整代码及目录(已经成功运行)
目录:
?
在? pom.xml 中添加依赖:
<dependencies>
<!--Flowable流程引擎-->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
<version>6.3.0</version>
</dependency>
<!--MySQL驱动,这里采用MySQL数据库,如果采用其它数据库,需要引入对应的依赖。-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
HolidayRequest.java
import org.flowable.engine.*;
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.task.api.Task;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class HolidayRequest {
public static void main( String[] args ) {
//1、创建ProcessEngineConfiguration实例,该实例可以配置与调整流程引擎的设置
ProcessEngineConfiguration cfg=new StandaloneProcessEngineConfiguration()
//2、通常采用xml配置文件创建ProcessEngineConfiguration,这里直接采用代码的方式
//3、配置数据库相关参数
.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&nullCatalogMeansCurrent=true")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
//4、初始化ProcessEngine流程引擎实例
ProcessEngine processEngine=cfg.buildProcessEngine();
//创建RepositoryService实例
RepositoryService repositoryService=processEngine.getRepositoryService();
//加载流程
Deployment deployment=repositoryService.createDeployment()
.addClasspathResource("holiday-request.bpmn20.xml")
.deploy();
//查询流程定义
ProcessDefinition processDefinition=repositoryService.createProcessDefinitionQuery()
.deploymentId(deployment.getId())
.singleResult();
System.out.println("Found process definition : "+processDefinition.getName());
//1、获取流程初始化变量
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、使用RuntimeService启动一个流程实例
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("holiday-request",variables);
//通过TaskService查询 manager 组的任务
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());
}
//使用集合下标获取特定流程实例的变量,在控制台输出
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?");
//经理完成任务
boolean approved=scanner.nextLine().toLowerCase().equals("y");
variables = new HashMap<String, Object>();
variables.put("approved", approved);
//任务完成,并会在离开排他网关的两条路径中,基于’approved ’流程变量选择一条
taskService.complete(task.getId(),variables);
//获取HistoryService实例
HistoryService historyService=processEngine.getHistoryService();
//添加查询条件
List<HistoricActivityInstance> activities =
historyService.createHistoricActivityInstanceQuery()
//选择特定实例
.processInstanceId(processInstance.getId())
//选择已完成的
.finished()
//根据实例完成时间升序排列
.orderByHistoricActivityInstanceEndTime().asc()
.list();
for (HistoricActivityInstance activity : activities) {
System.out.println(activity.getActivityId() + " took "
+ activity.getDurationInMillis() + " milliseconds");
}
}
}
SendRejectionMail.java
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
public class SendRejectionMail implements JavaDelegate {
@Override
public void execute(DelegateExecution delegateExecution) {
System.out.println("send e-mail to employee "
+ delegateExecution.getVariable("employee"));
}
}
CallExternalSystemDelegate.java
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
public class CallExternalSystemDelegate implements JavaDelegate {
public void execute( DelegateExecution delegateExecution) {
System.out.println("Calling the external system for employee "
+ delegateExecution.getVariable("employee"));
}
}
运行截图:
?
?
有什么问题可以在评论区讨论哦!
|