目录
7.查询历史任务
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取历史相关的 Service
HistoryService historyService = processEngine.getHistoryService();
//获取任务 Service
TaskService taskService = processEngine.getTaskService();
// 查询历史任务信息 流程的 ID
String processInstanceId = "2501";
List<HistoricTaskInstance> taskList = historyService.createHistoricTaskInstanceQuery()
.processDefinitionKey("leaveProcess")
.processInstanceId(processInstanceId)
.list();
for (HistoricTaskInstance taskInstance : taskList) {
System.out.println("任务名称"+taskInstance.getName());
System.out.println("任务处理人"+taskInstance.getAssignee());
System.out.println("任务开始时间"+taskInstance.getStartTime());
System.out.println(taskInstance.getClaimTime());
System.out.println("任务结束时间"+taskInstance.getEndTime());
System.out.println("任务耗时"+taskInstance.getDurationInMillis());
//查询历史的审批信息
List<Comment> taskComments = taskService.getTaskComments(taskInstance.getId());
if (taskComments != null && taskComments.size()>0){
System.out.println("批注信息:"+taskComments.get(0).getFullMessage());
}
System.out.println("-------------------");
}
8.候选人认领任务
当一个任务里面有多个候选人的时候,需要候选人认领任务,然后才能执行。
8.1 查询候选人待办任务
@Test
public void testQueryTask(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取任务 Service
TaskService taskService = processEngine.getTaskService();
// 获取运行时 Service
RuntimeService runtimeService = processEngine.getRuntimeService();
//指定用户的待办任务
String userId = "大白";
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveProcessCandidate")
.taskCandidateUser(userId) //查询候选人
.list();
for (Task task : taskList) {
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务创建时间: "+task.getCreateTime());
System.out.println("任务处理人: "+task.getAssignee());
System.out.println("流程实例ID: "+task.getProcessInstanceId());
}
}
8.2 候选人认领任务
// 认领任务
@Test
public void testCliamTask(){
//流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 任务 Service
TaskService taskService = processEngine.getTaskService();
//认领任务
String userId = "大白";
String taskId = "2505";
taskService.claim(taskId,userId);
}
8.3 执行任务
//执行任务
@Test
public void testCompleteTask(){
// 获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取任务的 Service
TaskService taskService = processEngine.getTaskService();
String taskId = "5006";
taskService.complete(taskId);
}
9.挂起/激活流程定义
//挂起和激活流程定义
@Test
public void testSuspendProcessDefinition(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取仓库相关的Service
RepositoryService repositoryService = processEngine.getRepositoryService();
//查询流程定义的激活/挂起状态
ProcessDefinition leaveProcess = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey("leaveProcess")
.latestVersion()
.singleResult();
System.out.println("流程是否挂起"+leaveProcess.isSuspended());
if (leaveProcess.isSuspended()){
//流程定义属于挂起状态
repositoryService.activateProcessDefinitionByKey("leaveProcess",true,null);
System.out.println("激活流程定义");
}else {
//表示流程定义属于激活状态
repositoryService.suspendProcessDefinitionByKey("leaveProcess",true,null);
System.out.println("挂起流程定义");
}
}
10.挂起/激活流程实例
//挂起/激活流程实例
@Test
public void testSuspendProcessInstance(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取运行时的Service
RuntimeService runtimeService = processEngine.getRuntimeService();
String instanceID = "15001";
//查询流程实例对象
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(instanceID)
.singleResult();
System.out.println("当前流程实例是否挂起:"+processInstance.isSuspended());
if (processInstance.isSuspended()){
//流程实例属于挂起状态
runtimeService.activateProcessInstanceById(instanceID);
System.out.println("流程实例激活");
}else {
//流程实例属于激活状态
runtimeService.suspendProcessInstanceById(instanceID);
System.out.println("流程实例挂起");
}
}
11.下载流程定义文件
//下载流程定义文件
@Test
public void testDownloadFile() throws IOException {
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取资源相关的 Service
RepositoryService repositoryService = processEngine.getRepositoryService();
//查询流程定义
List<ProcessDefinition> processDefinitionList = repositoryService
.createProcessDefinitionQuery().list();
for (ProcessDefinition processDefinition : processDefinitionList) {
// 获取 bpmn 文件名
String bpmnFileName = processDefinition.getResourceName();
//获取 png 的文件名
String pngFileName = processDefinition.getDiagramResourceName();
// 获取 bpmn的输入流 (部署id,bpmn文件)
InputStream bpmnInputStream = repositoryService
.getResourceAsStream(processDefinition.getDeploymentId(), bpmnFileName);
// 获取 bpmn的输入流 (部署id,bpmn文件)
InputStream pngInputStream = repositoryService
.getResourceAsStream(processDefinition.getDeploymentId(), pngFileName);
//将文件保存到指定目录
FileOutputStream bpmnFileOutputStream = new FileOutputStream("D:/Activiti7/leaveProcess.bpmn");
//将文件保存到指定目录
FileOutputStream pngFileOutputStream = new FileOutputStream("D:/Activiti7/leaveProcess.png");
IOUtils.copy(bpmnInputStream,bpmnFileOutputStream);
IOUtils.copy(pngInputStream,pngFileOutputStream);
//释放资源
bpmnFileOutputStream.close();
pngFileOutputStream.close();
}
}
12.流程定义删除
//流程定义删除
@Test
public void deleteDefinition(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取仓库 Service
RepositoryService repositoryService = processEngine.getRepositoryService();
String deployment = "17501";
//流程定义删除
// 如果这个流程定义下面没有正在运行的流程实例,直接删除就可以了
// 如果已经跑了一些实例,删除会报错
// repositoryService.deleteDeployment(deployment);
// cascade 默认是false,如果设置为 true,删除流程定义,其下面的实例也全部删除,包括正在运行的任务
repositoryService.deleteDeployment(deployment,true);
}
13.查询流程定义
// 查询流程引擎对象 (查看 Activiti 部署了多少个流程)
@Test
public void testQueryDefinition(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取仓库
RepositoryService repositoryService = processEngine.getRepositoryService();
//查询流程定义
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
.list();
for (ProcessDefinition processDefinition : list) {
System.out.println("流程名称:"+processDefinition.getName());
System.out.println("部署对象ID:"+processDefinition.getDeploymentId());
}
// 部署对象 deployment 和 流程定义对象 ProcessDefinition
// 部署对象 deployment 记录什么时间进行部署动作
// 流程定义对象 ProcessDefinition 存储 bpmn 的具体信息
}
14.查询流程实例
runtimeService.createProcessInstanceQuery().list()
15.查询历史的实例
historyService.createHistoricTaskInstanceQuery().list()
16.使用UEL表达式
//启动流程
@Test
public void testStartInstance(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取运行时 Service
RuntimeService runtimeService = processEngine.getRuntimeService();
String businessKey = "8001";
//启动流程 流程实例 processInstance
// 启动的时候设置变量
Map<String,Object> params = new HashMap<>();
params.put("assignee1","大大白");
params.put("assignee2","大大黑");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcessUEL",businessKey,params);
System.out.println(processInstance.getId());
}
//执行任务
@Test
public void testCompleteTask(){
// 获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取任务的 Service
TaskService taskService = processEngine.getTaskService();
String taskId = "2507";
taskService.complete(taskId);
}
17.使用监听器分配任务负责人
public class TaskAssigneeListener implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
System.out.println("监听-------------------");
if (("部门经理审批".equals(delegateTask.getName()))) {
delegateTask.setAssignee("大白");
}else {
delegateTask.setAssignee("大黑");
}
}
}
其他执行代码一样。在bpmn里面设置监听器。
18.流程变量的使用
//启动流程
@Test
public void testStartInstance(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取运行时 Service
RuntimeService runtimeService = processEngine.getRuntimeService();
String businessKey = "8001";
//启动流程 流程实例 processInstance
//启动的时候设置变量
Map<String,Object> params = new HashMap<>();
params.put("day","5");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcessVariable1",params);
System.out.println(processInstance.getId());
}
//执行任务
@Test
public void testCompleteTask(){
// 获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取任务的 Service
TaskService taskService = processEngine.getTaskService();
String taskId = "5006";
taskService.complete(taskId);
}
19.排他网关
排他网关(ExclusiveGateway)(异或网关或基于数据的排他网关),用来在流程中实现决策。当流程执行到这个网关的时候,所有分支都会判断条件是否为true,如果为true则执行该分支。
ExclusiveGateway
public class EGatewatTest {
@Test
public void testDeploy(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取仓库的 Service 资源管理API
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment() //创建部署对象
.addClasspathResource("bpmn/leaveProcessEGateway1.bpmn") //需要部署什么资源文件
.deploy();//进行部署
System.out.println("部署的id:"+deploy.getId());
}
//启动流程
@Test
public void testStartInstance(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取运行时 Service
RuntimeService runtimeService = processEngine.getRuntimeService();
//启动流程 流程实例 processInstance
//启动的时候设置变量
Map<String,Object> params = new HashMap<>();
params.put("day","5");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcessEGateway",params);
System.out.println(processInstance.getId());
}
//执行任务
@Test
public void testCompleteTask(){
// 获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取任务的 Service
TaskService taskService = processEngine.getTaskService();
String taskId = "2506";
taskService.complete(taskId);
}
}
20.并行网关
并行网关(ParallelGateway)允许将流程分成多条分支,也可以把多条分支汇聚到一起,并行网关的功能是基于进入和外出的顺序流的。
ParallelGateway
public class PGatewatTest {
@Test
public void testDeploy(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取仓库的 Service 资源管理API
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment() //创建部署对象
.addClasspathResource("bpmn/leaveProcessPGateway.bpmn") //需要部署什么资源文件
.deploy();//进行部署
System.out.println("部署的id:"+deploy.getId());
}
//启动流程
@Test
public void testStartInstance(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取运行时 Service
RuntimeService runtimeService = processEngine.getRuntimeService();
//启动流程 流程实例 processInstance
//启动的时候设置变量
Map<String,Object> params = new HashMap<>();
params.put("day","5");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcessPGateway",params);
System.out.println(processInstance.getId());
}
//执行任务
@Test
public void testCompleteTask(){
// 获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取任务的 Service
TaskService taskService = processEngine.getTaskService();
String taskId = "2510";
taskService.complete(taskId);
}
}
21.包含网关
包含网关可以看做是排他网关和并行网关的结合体。
InclusiveGateway
需求:出差申请大于3天需要由项目经理审批,小于3等于天由技术经理审批,出差申请必须经过人事助理审批。
public class IGatewatTest {
@Test
public void testDeploy(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取仓库的 Service 资源管理API
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment() //创建部署对象
.addClasspathResource("bpmn/leaveProcessIGateway.bpmn") //需要部署什么资源文件
.deploy();//进行部署
System.out.println("部署的id:"+deploy.getId());
}
//启动流程
@Test
public void testStartInstance(){
//获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取运行时 Service
RuntimeService runtimeService = processEngine.getRuntimeService();
//启动流程 流程实例 processInstance
//启动的时候设置变量
Map<String,Object> params = new HashMap<>();
params.put("day","5");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcessIGateway",params);
System.out.println(processInstance.getId());
}
//执行任务
@Test
public void testCompleteTask(){
// 获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取任务的 Service
TaskService taskService = processEngine.getTaskService();
String taskId = "5010";
taskService.complete(taskId);
}
}