<dependencies>
<dependency>
<groupId>org.flowablegroupId>
<artifactId>flowable-engineartifactId>
<version>6.3.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.26version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.32version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.32version>
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 构建我们需要的 ProcessEngine
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);
// 核心对象,类似于jdbcConnector
ProcessEngine processEngine = configuration.buildProcessEngine();
}
}
### 设置###
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
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_* : 普通数据,各种情况都使用的数据。
对应的holiday-request.bpmn20.xml
文件请参考官方文档的 2.3.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(){
// 通过 ProcessEngineConfiguration 构建我们需要的 ProcessEngine
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(){
// 1. 获取 ProcessEngine 对象
ProcessEngine processEngine = configuration.buildProcessEngine();
// 2. 获取 RepositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
// 3. 完成流程部署操作
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() => 请假流程
/**
* 查询定义的流程信息
*/
@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();
// 通过ID删除部署流程,如果部署流程启动了,则不允许删除
// repositoryService.deleteDeployment("1");
// 级联删除。第二个参数为true时表示级联删除,如果流程启动了,则相关任务一并删除。
repositoryService.deleteDeployment("1",true);
}
@Test
public void testRunProcess() {
// 我们需要通过 RuntimeService 来自动实例
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);
// 获取流程定义ID
System.out.println("processInstance.getProcessDefinitionId() = " + processInstance.getProcessDefinitionId());
// 获取活跃的ID
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;
/**
* @author Huathy
* @date 2022-05-14 17:04
* @description 实现Java委托
* 这是一个flowable的触发器
*/
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()
// 这个流程ID在[act_hi_actinst历史表]中PROC_DEF_ID_
.processDefinitionId("holidayRequest:1:5003")
// 查询已完成的历史记录
.finished()
// 按照结束时间排序
.orderByHistoricActivityInstanceEndTime().asc()
.list();
list.forEach( history -> {
// 打印活动ID、名称、处理人、耗时
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