Activiti7_2
0、观看提醒
Activiti基础案例的测试方法基于我上一个篇章“Activiti 7学习_1”部署环境运行的
Activiti 7学习_1路径
一、Activiti的25张表
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4c8bc62298d102f681f9ffdf3e8daa84.png)
表名 | 介绍 |
---|
act_evt_log | 流程引擎通用日志表 |
act_procdef_info | 流程定义的动态变更信息 |
ACT_GE_*:
全局通用数据及设置(general),各种情况都使用的数据。
表名 | 介绍 |
---|
act_ge_bytearray | 二进制表,存储通用的流程资源 |
act_ge_property | 系统存储表,存储整个流程引擎数据,默认存储三条数据 |
ACT_RE_*:
’RE’表示repository(存储),RepositoryService接口所操作的表。带此前缀的表包含的是静态信息,如,流程定义,流程的资源(图片,规则等)
表名 | 介绍 |
---|
act_re_deployment | 部署信息表 |
act_re_model | 流程设计实体表 |
act_re_procdef | 流程定义数据表 |
ACT_RU_*:
‘RU’表示runtime,运行时表-RuntimeService。这是运行时的表存储着流程变量,用户任务,变量,职责(job)等运行时的数据。Activiti只存储实例执行期间的运行时数据,当流程实例结束时,将删除这些记录。这就保证了这些运行时的表小且快。
表名 | 介绍 |
---|
act_ru_deadletter_job | 作业失败表,失败次数>重试次数 |
act_ru_event_subscr | 运行时事件表 |
act_ru_execution | 运行时流程执行实例表 |
act_ru_identitylink | 运行时用户信息表 |
act_ru_integration | 运行时综合表 |
act_ru_job | 作业表 |
act_ru_suspended_job | 作业暂停表 |
act_ru_task | 运行时任务信息表 |
act_ru_timer_job | 运行时定时器表 |
act_ru_variable | 运行时变量表 |
ACT_HI_*:
’HI’表示history,历史数据表,HistoryService。就是这些表包含着流程执行的历史相关数据,如结束的流程实例,变量,任务,等等
表名 | 介绍 |
---|
act_hi_actinst | 历史节点表 |
act_hi_attachment | 历史附件表 |
act_hi_comment | 历史意见表 |
act_hi_detail | 历史详情表 |
act_hi_identitylink | 历史用户信息表 |
act_hi_procinst | 历史流程实例表 |
act_hi_taskinst | 历史任务实例表 |
act_hi_varinst | 历史变量表 |
二、Activiti的6大服务
1、服务介绍
服务名 | 介绍 |
---|
RepositoryService | Activiti 中每一个不同版本的业务流程的定义都需要使用一些定义文件,部署文件和支持数据 ( 例如 BPMN2.0 XML 文件,表单定义文件,流程定义图像文件等 ),这些文件都存储在 Activiti 内建的 Repository 中。Repository Service 提供了对 repository 的存取服务。 |
RuntimeService | 在 Activiti 中,每当一个流程定义被启动一次之后,都会生成一个相应的流程对象实例。Runtime Service 提供了启动流程、查询流程实例、设置获取流程实例变量等功能。此外它还提供了对流程部署,流程定义和流程实例的存取服务。 |
TaskService | 在 Activiti 中业务流程定义中的每一个执行节点被称为一个 Task,对流程中的数据存取,状态变更等操作均需要在 Task 中完成。Task Service 提供了对用户 Task 和 Form 相关的操作。它提供了运行时任务查询、领取、完成、删除以及变量设置等功能。 |
ManagementService | Management Service 提供了对 Activiti 流程引擎的管理和维护功能,这些功能不在工作流驱动的应用程序中使用,主要用于 Activiti 系统的日常维护。 |
HistoryService | History Service 用于获取正在运行或已经完成的流程实例的信息,与 Runtime Service 中获取的流程信息不同,历史信息包含已经持久化存储的永久信息,并已经被针对查询优化。 |
DynamicBpmnService | 提供对流程定义和部署存储库的访问的服务。 |
2、服务获取
public void test(){
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
TaskService taskService = defaultProcessEngine.getTaskService();
HistoryService historyService = defaultProcessEngine.getHistoryService();
DynamicBpmnService dynamicBpmnService = defaultProcessEngine.getDynamicBpmnService();
ManagementService managementService = defaultProcessEngine.getManagementService();
}
三、Activiti基础案例
1、创建Activiti的数据库表
(1)测试方法
@Test
public void createActivitiTable() {
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println(defaultProcessEngine);
System.out.println("创建成功");
}
(2)运行截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/186b3d0e84abb076b026505cadf206e5.png)
2、工作流部署
@Test
public void workflowDeployment() {
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
DeploymentBuilder deployment = repositoryService.createDeployment();
deployment.name("出差申请工作流");
deployment.addClasspathResource("bpmn/evection.bpmn20.xml");
deployment.deploy();
System.out.println("工作流部署成功");
}
(2)运行截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/45e00bed40603c6ff3cdec3dd6a462ae.png)
3、流程启动,完整执行一个流程实例
(1)测试方法
@Test
public void startProcessInstance() {
String key = "evection";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);
System.out.println("流程定义id = " + processInstance.getProcessDefinitionId());
System.out.println("流程实例id = " + processInstance.getId());
System.out.println("当前活动id = " + processInstance.getActivityId());
System.out.println("流程启动成功....");
}
@Test
public void completTask01() {
String key = "evection";
String assignee = "张三";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> list = taskQuery
.processDefinitionKey(key)
.taskAssignee(assignee)
.list();
list.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("开始完成任务...");
System.out.println("---------------------------------");
taskService.complete(task.getId());
System.out.println("任务完成...");
});
}
@Test
public void completTask02() {
String key = "evection";
String assignee = "李四";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> list = taskQuery
.processDefinitionKey(key)
.taskAssignee(assignee)
.list();
list.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("开始完成任务...");
System.out.println("---------------------------------");
taskService.complete(task.getId());
System.out.println("任务完成...");
});
}
@Test
public void completTask03() {
String key = "evection";
String assignee = "王五";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> list = taskQuery
.processDefinitionKey(key)
.taskAssignee(assignee)
.list();
list.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("开始完成任务...");
System.out.println("---------------------------------");
taskService.complete(task.getId());
System.out.println("任务完成...");
});
}
(2)运行截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a59825168600793e3a7d986b9301674a.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e02f41589381f07156ce2061fad67ff7.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/63a1d0e8463049131854d9cb2c156347.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6e6bd864afda1b5acc29601925a9110a.png)
4、获取历史信息
(1)测试方法
@Test
public void queryHistoryInfoById() {
String id = "2501";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
HistoryService historyService = defaultProcessEngine.getHistoryService();
HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
List<HistoricActivityInstance> list = historicActivityInstanceQuery.processInstanceId(id)
.orderByHistoricActivityInstanceStartTime()
.asc()
.list();
list.stream().forEach(historicActivityInstance -> {
System.out.println("----------------------");
System.out.println("流程定义id:" + historicActivityInstance.getProcessDefinitionId());
System.out.println("流程实例id:" + historicActivityInstance.getProcessInstanceId());
System.out.println("历史任务id:" + historicActivityInstance.getActivityId());
System.out.println("历史任务名称:" + historicActivityInstance.getActivityName());
System.out.println("历史任务负责人" + historicActivityInstance.getAssignee());
System.out.println("历史任务开始时间:" + historicActivityInstance.getStartTime());
if (historicActivityInstance.getEndTime() != null) {
System.out.println("任务已完成...");
System.out.println("历史任务结束时间:" + historicActivityInstance.getEndTime());
} else {
System.out.println("任务未完成...");
}
System.out.println("----------------------");
});
}
(2)运行截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1342ca741f6a9e6661c8aef995f66f83.png)
5、查询流程实例
(1)测试方法
@Test
public void queryProcessInstanceBykey() {
String key = "evection";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
List<ProcessInstance> processInstanceList = processInstanceQuery.processDefinitionKey(key).list();
processInstanceList.stream().forEach(processInstance -> {
System.out.println("---------------------------------");
System.out.println("流程实例id:" + processInstance.getId());
System.out.println("流程实例名称:" + processInstance.getName());
System.out.println("---------------------------------");
});
}
6、查询任务
(1)测试方法
@Test
public void queryTaskById() {
String id = "2501";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> taskList = taskQuery.processInstanceId(id).list();
taskList.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("---------------------------------");
});
}
@Test
public void queryTaskByAssignee() {
String assignee = "张三";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> taskList = taskQuery.taskAssignee(assignee).list();
taskList.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("---------------------------------");
});
}
7、下载部署的资源文件
(1)测试方法
@Test
public void downloadingResourceFiles() throws IOException {
String key = "evection";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
ProcessDefinition evection = processDefinitionQuery.processDefinitionKey("evection").singleResult();
String deploymentId = evection.getDeploymentId();
String resourceName = evection.getResourceName();
InputStream resourceAsStream = repositoryService.getResourceAsStream(deploymentId, resourceName);
File file = new File("C:\\Users\\dashu\\Desktop\\学习笔记\\Activiti\\"+resourceName);
FileOutputStream fileOutputStream = new FileOutputStream(file);
IOUtils.copy(resourceAsStream, fileOutputStream);
fileOutputStream.close();
resourceAsStream.close();
System.out.println("下载完成...");
}
8、根据流程定义id删除流程
(1)测试方法
@Test
public void deleteProcessById(){
String id = "1";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
repositoryService.deleteDeployment(id);
System.out.println("删除完成...");
}
9、完整测试类
package com.dahu.test;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class ActivitiTest {
private static Logger logger = Logger.getLogger(ActivitiTest.class);
@Test
public void createActivitiTable() {
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println(defaultProcessEngine);
System.out.println("创建成功");
}
@Test
public void workflowDeployment() {
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
DeploymentBuilder deployment = repositoryService.createDeployment();
deployment.name("出差申请工作流");
deployment.addClasspathResource("bpmn/evection.bpmn20.xml");
deployment.deploy();
System.out.println("工作流部署成功");
}
@Test
public void startProcessInstance() {
String key = "evection";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);
System.out.println("流程定义id = " + processInstance.getProcessDefinitionId());
System.out.println("流程实例id = " + processInstance.getId());
System.out.println("当前活动id = " + processInstance.getActivityId());
System.out.println("流程启动成功....");
}
@Test
public void completTask01() {
String key = "evection";
String assignee = "张三";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> list = taskQuery
.processDefinitionKey(key)
.taskAssignee(assignee)
.list();
list.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("开始完成任务...");
System.out.println("---------------------------------");
taskService.complete(task.getId());
System.out.println("任务完成...");
});
}
@Test
public void completTask02() {
String key = "evection";
String assignee = "李四";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> list = taskQuery
.processDefinitionKey(key)
.taskAssignee(assignee)
.list();
list.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("开始完成任务...");
System.out.println("---------------------------------");
taskService.complete(task.getId());
System.out.println("任务完成...");
});
}
@Test
public void completTask03() {
String key = "evection";
String assignee = "王五";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> list = taskQuery
.processDefinitionKey(key)
.taskAssignee(assignee)
.list();
list.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("开始完成任务...");
System.out.println("---------------------------------");
taskService.complete(task.getId());
System.out.println("任务完成...");
});
}
@Test
public void queryHistoryInfoById() {
String id = "2501";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
HistoryService historyService = defaultProcessEngine.getHistoryService();
HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
List<HistoricActivityInstance> list = historicActivityInstanceQuery.processInstanceId(id)
.orderByHistoricActivityInstanceStartTime()
.asc()
.list();
list.stream().forEach(historicActivityInstance -> {
System.out.println("----------------------");
System.out.println("流程定义id:" + historicActivityInstance.getProcessDefinitionId());
System.out.println("流程实例id:" + historicActivityInstance.getProcessInstanceId());
System.out.println("历史任务id:" + historicActivityInstance.getActivityId());
System.out.println("历史任务名称:" + historicActivityInstance.getActivityName());
System.out.println("历史任务负责人" + historicActivityInstance.getAssignee());
System.out.println("历史任务开始时间:" + historicActivityInstance.getStartTime());
if (historicActivityInstance.getEndTime() != null) {
System.out.println("任务已完成...");
System.out.println("历史任务结束时间:" + historicActivityInstance.getEndTime());
} else {
System.out.println("任务未完成...");
}
System.out.println("----------------------");
});
}
@Test
public void queryProcessInstanceBykey() {
String key = "evection";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
List<ProcessInstance> processInstanceList = processInstanceQuery.processDefinitionKey(key).list();
processInstanceList.stream().forEach(processInstance -> {
System.out.println("---------------------------------");
System.out.println("流程实例id:" + processInstance.getId());
System.out.println("流程实例名称:" + processInstance.getName());
System.out.println("---------------------------------");
});
}
@Test
public void queryTaskById() {
String id = "2501";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> taskList = taskQuery.processInstanceId(id).list();
taskList.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("---------------------------------");
});
}
@Test
public void queryTaskByAssignee() {
String assignee = "张三";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = defaultProcessEngine.getTaskService();
TaskQuery taskQuery = taskService.createTaskQuery();
List<Task> taskList = taskQuery.taskAssignee(assignee).list();
taskList.stream().forEach(task -> {
System.out.println("---------------------------------");
System.out.println("流程定义id:" + task.getProcessDefinitionId());
System.out.println("流程实例id:" + task.getProcessInstanceId());
System.out.println("任务id:" + task.getId());
System.out.println("任务名称:" + task.getName());
System.out.println("任务负责人:" + task.getAssignee());
System.out.println("---------------------------------");
});
}
@Test
public void downloadingResourceFiles() throws IOException {
String key = "evection";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
ProcessDefinition evection = processDefinitionQuery.processDefinitionKey("evection").singleResult();
String deploymentId = evection.getDeploymentId();
String resourceName = evection.getResourceName();
InputStream resourceAsStream = repositoryService.getResourceAsStream(deploymentId, resourceName);
File file = new File("C:\\Users\\dashu\\Desktop\\学习笔记\\Activiti\\"+resourceName);
FileOutputStream fileOutputStream = new FileOutputStream(file);
IOUtils.copy(resourceAsStream, fileOutputStream);
fileOutputStream.close();
resourceAsStream.close();
System.out.println("下载完成...");
}
@Test
public void deleteProcessById(){
String id = "1";
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
repositoryService.deleteDeployment(id);
System.out.println("删除完成...");
}
}