一、发展历程与官方手册
- 发展介绍:
- 官方手册
https://tkjohn.github.io/flowable-userguide/#_getting_started
二、获取流程引擎对象
- 创建Maven工程并导入依赖
<dependencies>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
<version>6.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
- 编写测试代码
package com.hx;
import static org.junit.Assert.assertTrue;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
import org.junit.Test;
public class AppTest {
@Test
public void testProcessEngine(){
ProcessEngineConfiguration configuration = new StandaloneInMemProcessEngineConfiguration();
configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
configuration.setJdbcUsername("root");
configuration.setJdbcPassword("whx@2022");
configuration.setJdbcUrl("jdbc:mysql://sh-cynosdbmysql-grp-cnmbmcfy.sql.tencentcdb.com:26384/flowable_learn?serverTimeZone=UTC");
configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
ProcessEngine processEngine = configuration.buildProcessEngine();
}
}
三、Flowable日志和表结构介绍
1. 日志
### 设置###
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{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
2. 表结构
ACT_RE_* : ’RE’表示repository(存储)。RepositoryService接口操作的表。带此前缀的表包含的是静态信息,如,流程定义,流程的资源(图片,规则等)。 ACT_RU_* : ’RU’表示runtime。RuntimeService、TaskService等接口操作的表。这是运行时的表存储着流程变量,用户任务,变量,职责(job)等运行时的数据。flowable只存储实例执行期间的运行时数据,当流程实例结束时,将删除这些记录。这就保证了这些运行时的表小且快。 ACT_ID_* : ’ID’表示identity(组织机构)。IdentityService接口操作的表。这些表包含标识的信息,如用户,用户组,等等。 ACT_HI_* : ’HI’表示history。HistoryService接口操作的表。就是这些表包含着历史的相关数据,如结束的流程实例,变量,任务,等等。 ACT_GE_* : 普通数据,各种情况都使用的数据。
四、部署流程
1. 流程定义文件解析
对应的holiday-request.bpmn20.xml 文件请参考官方文档的 2.3.2. 部署流程定义
2. 部署实现
package com.hx;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.junit.Before;
import org.junit.Test;
public class AppTest {
ProcessEngineConfiguration configuration = null;
@Before
public void before(){
configuration = new StandaloneInMemProcessEngineConfiguration();
configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
configuration.setJdbcUsername("flowable");
configuration.setJdbcPassword("whx@xxxx");
configuration.setJdbcUrl("jdbc:mysql://sh-cynosdbmysql-grp-cnmbmcfy.sql.tencentcdb.com:26384/flowable_learn?serverTimeZone=UTC");
configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
}
@Test
public void testDeploy(){
ProcessEngine processEngine = configuration.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("holiday-request.bpmn20.xml")
.name("请假流程")
.deploy();
System.out.println("deploy.getId() => " + deploy.getId());
System.out.println("deploy.getName() => " + deploy.getName());
}
}
运行结果分析
- 控制台输出
deploy.getId() => 1
deploy.getName() => 请假流程
-
查看表结构中的变动
- ACT_RE_DEPLOYMENT部署数据表
- ACT_GE_BYTEARRAY资源表
- ACT_RE_PROCDEF流程定义表
3. 查询和删除操作
查询
- 代码
@Test
public void testDeployQuery(){
ProcessEngine processEngine = configuration.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
ProcessDefinition processDefinition = processDefinitionQuery.deploymentId("1").singleResult();
System.out.println("getDeploymentId => " + processDefinition.getDeploymentId());
System.out.println("getName => " + processDefinition.getName());
System.out.println("getDescription => " + processDefinition.getDescription());
System.out.println("getId => " + processDefinition.getId());
}
- 测试输出
getDeploymentId => 1
getName => 请假流程
getDescription => null
getId => holidayRequest:1:3
删除
@Test
public void testDeleteDeploy(){
ProcessEngine processEngine = configuration.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.deleteDeployment("1",true);
}
五、启动流程实例
- 编码
@Test
public void testRunProcess() {
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String, Object> params = new HashMap<>();
params.put("employee","Huathy");
params.put("nrOfHolidays", 3);
params.put("description","员工测试请假");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", params);
System.out.println("processInstance.getProcessDefinitionId() = " + processInstance.getProcessDefinitionId());
System.out.println("processInstance.getActivityId() = " + processInstance.getActivityId());
System.out.println("processInstance.getId() = " + processInstance.getId());
}
- 测试输出
processInstance.getProcessDefinitionId() = holidayRequest:1:3
processInstance.getActivityId() = null
processInstance.getId() = 2501
- 查看数据库表结构变动
六、任务的查询、处理、历史任务查询
查询任务
- 在
holiday-request.bpmn20.xml 文件中的userTask中添加flowable:assignee="whx" 来指定用户。并将原有流程删除后,重新部署流程!
<userTask id="approveTask" name="同意或者拒绝请假" flowable:assignee="whx"/>
- 编写测试
@Test
public void testQueryTask(){
TaskService taskService = configuration.getTaskService();
List<Task> list = taskService.createTaskQuery()
.processDefinitionKey("holidayRequest")
.taskAssignee("whx").list();
list.forEach( task -> {
System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
System.out.println("task.getName() = " + task.getName());
System.out.println("task.getAssignee() = " + task.getAssignee());
System.out.println("task.getDescription() = " + task.getDescription());
System.out.println("task.getId() = " + task.getId());
});
}
- 测试输出
task.getProcessDefinitionId() = holidayRequest:1:5003
task.getName() = 同意或者拒绝请假
task.getAssignee() = whx
task.getDescription() = null
task.getId() = 7508
处理任务
- 编写测试类
@Test
public void testCompleteTask() {
TaskService taskService = configuration.getTaskService();
Task task = taskService.createTaskQuery().processDefinitionKey("holidayRequest").taskAssignee("whx").singleResult();
Map<String, Object> params = new HashMap<>();
params.put("approved", false);
taskService.complete(task.getId(), params);
}
- 编写
SendRejectionMail 处理类
package org.flowable;
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("=========================");
System.out.println("抱歉,您的请假申请被拒绝……");
System.out.println("=========================");
}
}
查询历史流程
- 测试代码
@Test
public void testHistory(){
HistoryService historyService = processEngine.getHistoryService();
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processDefinitionId("holidayRequest:1:5003")
.finished()
.orderByHistoricActivityInstanceEndTime().asc()
.list();
list.forEach( history -> {
System.out.println(history.getActivityId() + " : "
+ history.getActivityName() + " : "
+ history.getAssignee() + " : "
+ history.getDurationInMillis()+"毫秒");
});
}
- 测试输出结果:
startEvent : null : null : 2
approveTask : 同意或者拒绝请假 : whx : 2358653
decision : null : null : 17
sendRejectionMail : Send out rejection email : null : 3
rejectEnd : null : null : 2
|