资料地址1614912120/Flowable (github.com)https://github.com/1614912120/Flowable
步骤1:添加Flowable依赖
首先,您需要将Flowable引擎集成到您的项目中。您可以通过Maven、Gradle或手动下载jar包的方式来添加Flowable的依赖。
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>Flowable</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>flowable-test-b</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
<version>6.7.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
步骤2:创建流程定义
使用BPMN 2.0标准,创建一个新的流程定义文件。您可以使用Flowable提供的模型设计器,也可以手动编写BPMN XML文件。定义流程中的各个活动、网关、事件等。
步骤3:部署流程定义
将流程定义部署到Flowable引擎中,以便后续启动流程实例。您可以将流程定义文件打包成一个zip文件,并通过Flowable提供的API来进行部署。
public static void main(String[] args) {
/**
* 部署流程
*/
// 配置数据库相关信息 获取 ProcessEngineConfiguration
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://ip:3306/flowable?serverTimezone=UTC")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
// 获取流程引擎对象
ProcessEngine processEngine = cfg.buildProcessEngine();
// 部署流程 获取RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment() //创建对象
.addClasspathResource("MyHolidayUI.bpmn20.xml") //添加部署文件
// 设置部署流程的名称
.name("请求流程")
//执行操作
.deploy();
System.out.println("deployment.getId() = " + deployment.getId());
System.out.println("deployment.getName() = " + deployment.getName());
}
步骤4:启动流程实例
使用Flowable的API,通过流程定义的Key来启动一个新的流程实例。您可以在启动流程实例时传入需要的参数和变量。
/**
* 启动流程实例
*/
@Test
public void testRunProcess() {
// 配置数据库相关信息 获取 ProcessEngineConfiguration
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://ip:3306/flowable?serverTimezone=UTC")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
//获取流程引擎对象
ProcessEngine processEngine = cfg.buildProcessEngine();
// 启动流程实例通过 RuntimeService 对象
RuntimeService runtimeService = processEngine.getRuntimeService();
//构建流程变量
HashMap<String, Object> variables = new HashMap<>();
variables.put("employee","张三") ;// 谁申请请假
variables.put("nrOfHolidays",3); // 请几天假
variables.put("description","工作累了,想出去玩玩"); // 请假的原因
// 启动流程实例,第一个参数是流程定义的id
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("MyHolidayUI", variables);
// 输出相关的流程实例信息
System.out.println("流程定义的ID:" + processInstance.getProcessDefinitionId());
System.out.println("流程实例的ID:" + processInstance.getId());
System.out.println("当前活动的ID:" + processInstance.getActivityId());
}
步骤5:执行任务
一旦流程实例启动,流程引擎会根据流程定义中的逻辑自动执行各个任务。您可以使用Flowable提供的API来查询和完成任务。
@Test
public void testCompleteTask() {
// 配置数据库相关信息 获取 ProcessEngineConfiguration
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://ip:3306/flowable?serverTimezone=UTC")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
//获取流程引擎对象
ProcessEngine processEngine = cfg.buildProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery()
.processDefinitionKey("MyHolidayUI")
.taskAssignee("user2")
.singleResult();
HashMap<String, Object> variables = new HashMap<>();
variables.put("approved",true);
taskService.complete(task.getId(),variables);
}
步骤6:处理流程结果
根据任务的执行结果,您可以继续执行后续的操作,例如生成报告、发送通知等。
在这个过程中 可以查看流程定义 ,可以删除定义,可以看历史记录,可以挂起流程
/**
* 查看流程定义
*/
@Test
public void testDeployQuery() {
// 配置数据库相关信息 获取 ProcessEngineConfiguration
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://ip:3306/flowable?serverTimezone=UTC")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
ProcessEngine processEngine = cfg.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.deploymentId("10001")
.singleResult();
System.out.println("processDefinition.getId() = " + processDefinition.getId());
System.out.println("processDefinition.getName() = " + processDefinition.getName());
System.out.println("processDefinition.getDeploymentId() = " + processDefinition.getDeploymentId());
System.out.println("processDefinition.getDescription() = " + processDefinition.getDescription());
}
/**
* 删除流程定义
*/
@Test
public void testDeleteQuery() {
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://ip:3306/flowable?serverTimezone=UTC")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
ProcessEngine processEngine = cfg.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.deleteDeployment("37501",true);
}
@Test
public void testHistory() {
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://ip:3306/flowable?serverTimezone=UTC")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
//获取流程引擎对象
ProcessEngine processEngine = cfg.buildProcessEngine();
HistoryService historyService = processEngine.getHistoryService();
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processDefinitionId("MyHolidayUI:1:37504")
.finished()//查询历史记录已经是完成的
.orderByHistoricActivityInstanceEndTime() //指定排序的字段和顺序
.asc()
.list();
for (HistoricActivityInstance historicActivityInstance : list) {
System.out.println(historicActivityInstance.getActivityId() + " took "
+ historicActivityInstance.getDurationInMillis() + " milliseconds");
}
}
/**
* 挂起流程
*/
@Test
public void test05() {
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://ip:3306/flowable?serverTimezone=UTC")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
//获取流程引擎对象
ProcessEngine processEngine = cfg.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionId("MyHolidayUI:1:47504")
.singleResult();
//获取流程定义状态
boolean suspended = processDefinition.isSuspended();
System.out.println("流程定义状态"+suspended);
if(suspended) {
System.out.println("激活流程定义");
repositoryService.activateProcessDefinitionById("MyHolidayUI:1:47504",true,null);
}else {
System.out.println("挂起流程");
repositoryService.suspendProcessDefinitionById("MyHolidayUI:1:47504",true,null);
}
}
当我们启动了一个流程实例后,会在ACT_RU_*对应的表结构中操作,运行时实例涉及的表结构共10张:
-
ACT_RU_DEADLETTER_JOB 正在运行的任务表
-
ACT_RU_EVENT_SUBSCR 运行时事件
-
ACT_RU_EXECUTION 运行时流程执行实例
-
ACT_RU_HISTORY_JOB 历史作业表
-
ACT_RU_IDENTITYLINK 运行时用户关系信息
-
ACT_RU_JOB 运行时作业表
-
ACT_RU_SUSPENDED_JOB 暂停作业表
-
ACT_RU_TASK 运行时任务表
-
ACT_RU_TIMER_JOB 定时作业表
-
ACT_RU_VARIABLE 运行时变量表
启动一个流程实例的时候涉及到的表有
-
ACT_RU_EXECUTION 运行时流程执行实例
-
ACT_RU_IDENTITYLINK 运行时用户关系信息
-
ACT_RU_TASK 运行时任务表
-
ACT_RU_VARIABLE 运行时变量表
ACT_RU_EXECUTION表结构
字段 | 名称 | 备注 |
---|---|---|
ID_ | 主键 | |
REV_ | 版本号 | |
PROC_INST_ID_ | 流程实例ID | |
BUSINESS_KEY_ | 业务主键ID | |
PARENT_ID_ | 父执行流的ID | |
PROC_DEF_ID_ | 流程定义的数据ID | |
SUPER_EXEC_ | ||
ROOT_PROC_INST_ID_ | 流程实例的root流程id | |
ACT_ID_ | 节点实例ID | |
IS_ACTIVE_ | 是否存活 | |
IS_CONCURRENT_ | 执行流是否正在并行 | |
IS_SCOPE_ | ||
IS_EVENT_SCOPE_ | ||
IS_MI_ROOT_ | ||
SUSPENSION_STATE_ | 流程终端状态 | |
CACHED_ENT_STATE_ | ||
TENANT_ID_ | 租户编号 | |
NAME_ | ||
START_TIME_ | 开始时间 | |
START_USER_ID_ | 开始的用户编号 | |
LOCK_TIME_ | 锁定时间 | |
IS_COUNT_ENABLED_ | ||
EVT_SUBSCR_COUNT_ | ||
TASK_COUNT_ | ||
JOB_COUNT_ | ||
TIMER_JOB_COUNT_ | ||
SUSP_JOB_COUNT_ | ||
DEADLETTER_JOB_COUNT_ | ||
VAR_COUNT_ | ||
ID_LINK_COUNT_ |