在resource
目录下新建bpmn
目录用于存放所有的流程文件 ,在bpmn
目录下新建leave.bpmn
,内容如下:
bpmn
文件本质上是xml格式,我们打开看到的是图片格式是因为我们在IDEA中安装了actiBPM的插件.
如果我们把bpmn
文件部署到Web环境,那么就只能看到xml信息,无法看到类似上面的图形了.
我们可以再部署之前,根据bpmn
文件生成png
文件,然后把bpmn
和png
文件同时部署到Web环境中.如果我们想查看流程图的话,我们就可以通过Activiti把这个png
文件读取出来. 接下来我们把bpmn
导出为png
文件
leave.bpmn
文件重命名为leave.xml
文件leave.xml
文件 --> Diagrams
--> Show BPMN Designer...
package cn.wolfcode.demo;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;
/**
* Created by wolfcode
*/
public class ActivitiTest {
@Test
public void testDeploy(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//进行部署
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmn/leave.bpmn")
.addClasspathResource("bpmn/leave.png")
.name("请假流程")
.deploy();
//输出部署的一些信息
System.out.println("流程部署ID:"+deployment.getId());
System.out.println("流程部署名称:"+deployment.getName());
}
}
观察日志发现,进行部署会操作如下表.
Preparing: update ACT_GE_PROPERTY SET REV_ = ?, VALUE_ = ? where NAME_ = ? and REV_ = ?
Parameters: 2(Integer), 2501(String), next.dbid(String), 1(Integer)
Preparing: insert into ACT_RE_PROCDEF(ID_, REV_, CATEGORY_, NAME_, KEY_, VERSION_, DEPLOYMENT_ID_, RESOURCE_NAME_, DGRM_RESOURCE_NAME_, DESCRIPTION_, HAS_START_FORM_KEY_, HAS_GRAPHICAL_NOTATION_ , SUSPENSION_STATE_, TENANT_ID_, ENGINE_VERSION_) values (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Parameters: leaveProcess:1:4(String), http://www.activiti.org/test(String), 请假流程(String), leaveProcess(String), 1(Integer), 1(String), bpmn/leave.bpmn(String), bpmn/leave.png(String), null, false(Boolean), true(Boolean), 1(Integer), (String), null
Preparing: insert into ACT_RE_DEPLOYMENT(ID_, NAME_, CATEGORY_, KEY_, TENANT_ID_, DEPLOY_TIME_, ENGINE_VERSION_) values(?, ?, ?, ?, ?, ?, ?)
Parameters: 1(String), 请假流程(String), null, null, (String), 2021-06-02 11:01:44.838(Timestamp), null
Preparing: INSERT INTO ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_, GENERATED_) VALUES (?, 1, ?, ?, ?, ?) , (?, 1, ?, ?, ?, ?)
Parameters: 2(String), bpmn/leave.png(String), java.io.ByteArrayInputStream@7fe7c640(ByteArrayInputStream), 1(String), false(Boolean), 3(String), bpmn/leave.bpmn(String), java.io.ByteArrayInputStream@4c4748bf(ByteArrayInputStream), 1(String), false(Boolean)
@Test
public void testStartProcess(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
//根据流程定义的key启动流程实例,这个key是在定义bpmn的时候设置的
ProcessInstance instance = runtimeService.
startProcessInstanceByKey("leaveProcess");
//获取流程实例的相关信息
System.out.println("流程定义的id = " + instance.getProcessDefinitionId());
System.out.println("流程实例的id = " + instance.getId());
}
观察日志发现,启动流程会操作如下表.
Preparing: insert into ACT_HI_TASKINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, OWNER_, ASSIGNEE_, START_TIME_, CLAIM_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TASK_DEF_KEY_, FORM_KEY_, PRIORITY_, DUE_DATE_, CATEGORY_, TENANT_ID_ ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
Parameters: 2505(String), leaveProcess:1:4(String), 2501(String), 2502(String), 部门经理审批(String), null, null, null, 李四(String), 2021-06-02 16:34:52.827(Timestamp), null, null, null, null, _4(String), null, 50(Integer), null, null, (String)
Preparing: insert into ACT_HI_PROCINST ( ID_, PROC_INST_ID_, BUSINESS_KEY_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_, START_USER_ID_, START_ACT_ID_, END_ACT_ID_, SUPER_PROCESS_INSTANCE_ID_, DELETE_REASON_, TENANT_ID_, NAME_ ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
Parameters: 2501(String), 2501(String), null, leaveProcess:1:4(String), 2021-06-02 16:34:52.798(Timestamp), null, null, null, _2(String), null, null, null, (String), null
Preparing: insert into ACT_HI_ACTINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, ACT_ID_, TASK_ID_, CALL_PROC_INST_ID_, ACT_NAME_, ACT_TYPE_, ASSIGNEE_, START_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TENANT_ID_ ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Parameters: 2503(String), leaveProcess:1:4(String), 2501(String), 2502(String), _2(String), null, null, StartEvent(String), startEvent(String), null, 2021-06-02 16:34:52.815(Timestamp), 2021-06-02 16:34:52.816(Timestamp), 1(Long), null, (String), 2504(String), leaveProcess:1:4(String), 2501(String), 2502(String), _4(String), 2505(String), null, 部门经理审批(String), userTask(String), 李四(String), 2021-06-02 16:34:52.817(Timestamp), null, null, null, (String)
Preparing: insert into ACT_HI_IDENTITYLINK (ID_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_) values (?, ?, ?, ?, ?, ?)
Parameters: 2506(String), participant(String), 李四(String), null, null, 2501(String)
insert into ACT_RU_EXECUTION (ID_, REV_, PROC_INST_ID_, BUSINESS_KEY_, PROC_DEF_ID_, ACT_ID_, IS_ACTIVE_, IS_CONCURRENT_, IS_SCOPE_,IS_EVENT_SCOPE_, IS_MI_ROOT_, PARENT_ID_, SUPER_EXEC_, ROOT_PROC_INST_ID_, SUSPENSION_STATE_, TENANT_ID_, NAME_, START_TIME_, START_USER_ID_, IS_COUNT_ENABLED_, EVT_SUBSCR_COUNT_, TASK_COUNT_, JOB_COUNT_, TIMER_JOB_COUNT_, SUSP_JOB_COUNT_, DEADLETTER_JOB_COUNT_, VAR_COUNT_, ID_LINK_COUNT_) values (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Parameters: 2501(String), 2501(String), null, leaveProcess:1:4(String), null, true(Boolean), false(Boolean), true(Boolean), false(Boolean), false(Boolean), null, null, 2501(String), 1(Integer), (String), null, 2021-06-02 16:34:52.798(Timestamp), null, false(Boolean), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 2502(String), 2501(String), null, leaveProcess:1:4(String), _4(String), true(Boolean), false(Boolean), false(Boolean), false(Boolean), false(Boolean), 2501(String), null, 2501(String), 1(Integer), (String), null, 2021-06-02 16:34:52.813(Timestamp), null, false(Boolean), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer)
Preparing: insert into ACT_RU_TASK (ID_, REV_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, PRIORITY_, CREATE_TIME_, OWNER_, ASSIGNEE_, DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_, DUE_DATE_, CATEGORY_, SUSPENSION_STATE_, TENANT_ID_, FORM_KEY_, CLAIM_TIME_) values (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
Parameters: 2505(String), 部门经理审批(String), null, null, 50(Integer), 2021-06-02 16:34:52.817(Timestamp), null, 李四(String), null, 2502(String), 2501(String), leaveProcess:1:4(String), _4(String), null, null, 1(Integer), (String), null, null
Preparing: insert into ACT_RU_IDENTITYLINK (ID_, REV_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_, PROC_DEF_ID_) values (?, 1, ?, ?, ?, ?, ?, ?)
Parameters: 2506(String), participant(String), 李四(String), null, null, 2501(String), null
@Test
public void testSelectTodoTaskList(){
//任务负责人
String assignee = "李四";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
System.out.println("流程定义id = " + task.getProcessDefinitionId());
System.out.println("流程实例id = " + task.getProcessInstanceId());
System.out.println("任务id = " + task.getId());
System.out.println("任务名称 = " + task.getName());
}
}
观察日志发现,查询任务会操作如下表.
Preparing: select distinct RES.* from ACT_RU_TASK RES inner join ACT_RE_PROCDEF D on RES.PROC_DEF_ID_ = D.ID_ WHERE RES.ASSIGNEE_ = ? and D.KEY_ = ? order by RES.ID_ asc LIMIT ? OFFSET ?
Parameters: 李四(String), leaveProcess(String), 2147483647(Integer), 0(Integer)
@Test
public void testCompleteTask(){
//任务负责人
String assignee = "李四";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
taskService.complete(task.getId());
}
}
观察日志发现,查询任务会操作如下表.
Preparing: update ACT_GE_PROPERTY SET REV_ = ?, VALUE_ = ? where NAME_ = ? and REV_ = ?
Parameters: 4(Integer), 7501(String), next.dbid(String), 3(Integer)
Preparing: insert into ACT_HI_TASKINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, OWNER_, ASSIGNEE_, START_TIME_, CLAIM_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TASK_DEF_KEY_, FORM_KEY_, PRIORITY_, DUE_DATE_, CATEGORY_, TENANT_ID_ ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
Parameters: 5002(String), leaveProcess:1:4(String), 2501(String), 2502(String), 人事复批(String), null, null, null, 王五(String), 2021-06-02 16:39:19.036(Timestamp), null, null, null, null, _5(String), null, 50(Integer), null, null, (String)
Preparing: insert into ACT_HI_ACTINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, ACT_ID_, TASK_ID_, CALL_PROC_INST_ID_, ACT_NAME_, ACT_TYPE_, ASSIGNEE_, START_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TENANT_ID_ ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
Parameters: 5001(String), leaveProcess:1:4(String), 2501(String), 2502(String), _5(String), 5002(String), null, 人事复批(String), userTask(String), 王五(String), 2021-06-02 16:39:19.025(Timestamp), null, null, null, (String)
Preparing: insert into ACT_HI_IDENTITYLINK (ID_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_) values (?, ?, ?, ?, ?, ?)
Parameters: 5003(String), participant(String), 王五(String), null, null, 2501(String)
Preparing: insert into ACT_RU_TASK (ID_, REV_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, PRIORITY_, CREATE_TIME_, OWNER_, ASSIGNEE_, DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_, DUE_DATE_, CATEGORY_, SUSPENSION_STATE_, TENANT_ID_, FORM_KEY_, CLAIM_TIME_) values (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
Parameters: 5002(String), 人事复批(String), null, null, 50(Integer), 2021-06-02 16:39:19.025(Timestamp), null, 王五(String), null, 2502(String), 2501(String), leaveProcess:1:4(String), _5(String), null, null, 1(Integer), (String), null, null
Preparing: insert into ACT_RU_IDENTITYLINK (ID_, REV_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_, PROC_DEF_ID_) values (?, 1, ?, ?, ?, ?, ?, ?)
Parameters: 5003(String), participant(String), 王五(String), null, null, 2501(String), null
Preparing: update ACT_RU_EXECUTION set REV_ = ?, BUSINESS_KEY_ = ?, PROC_DEF_ID_ = ?, ACT_ID_ = ?, IS_ACTIVE_ = ?, IS_CONCURRENT_ = ?, IS_SCOPE_ = ?, IS_EVENT_SCOPE_ = ?, IS_MI_ROOT_ = ?, PARENT_ID_ = ?, SUPER_EXEC_ = ?, ROOT_PROC_INST_ID_ = ?, SUSPENSION_STATE_ = ?, NAME_ = ?, IS_COUNT_ENABLED_ = ?, EVT_SUBSCR_COUNT_ = ?, TASK_COUNT_ = ?, JOB_COUNT_ = ?, TIMER_JOB_COUNT_ = ?, SUSP_JOB_COUNT_ = ?, DEADLETTER_JOB_COUNT_ = ?, VAR_COUNT_ = ?, ID_LINK_COUNT_ = ? where ID_ = ? and REV_ = ?
Parameters: 2(Integer), null, leaveProcess:1:4(String), _5(String), true(Boolean), false(Boolean), false(Boolean), false(Boolean), false(Boolean), 2501(String), null, 2501(String), 1(Integer), null, false(Boolean), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 0(Integer), 2502(String), 1(Integer)
Preparing: update ACT_HI_TASKINST set PROC_DEF_ID_ = ?, EXECUTION_ID_ = ?, NAME_ = ?, PARENT_TASK_ID_ = ?, DESCRIPTION_ = ?, OWNER_ = ?, ASSIGNEE_ = ?, CLAIM_TIME_ = ?, END_TIME_ = ?, DURATION_ = ?, DELETE_REASON_ = ?, TASK_DEF_KEY_ = ?, FORM_KEY_ = ?, PRIORITY_ = ?, DUE_DATE_ = ?, CATEGORY_ = ? where ID_ = ?
Parameters: leaveProcess:1:4(String), 2502(String), 部门经理审批(String), null, null, null, 李四(String), null, 2021-06-02 16:39:18.995(Timestamp), 266168(Long), null, _4(String), null, 50(Integer), null, null, 2505(String)
Preparing: update ACT_HI_ACTINST set EXECUTION_ID_ = ?, ASSIGNEE_ = ?, END_TIME_ = ?, DURATION_ = ?, DELETE_REASON_ = ? where ID_ = ?
Parameters: 2502(String), 李四(String), 2021-06-02 16:39:19.009(Timestamp), 266192(Long), null, 2504(String)
Preparing: delete from ACT_RU_TASK where ID_ = ? and REV_ = ?
Parameters: 2505(String), 1(Integer)
@Test
public void testAddComment(){
//任务负责人
String assignee = "王五";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
//在任务执行之前任务添加批注信息
taskService.addComment(task.getId(),task.getProcessInstanceId(),task.getName()+"审批通过");
taskService.complete(task.getId());
}
}
观察日志发现,其余操作和任务处理的表是一致的,但是添加批注会往如下表中插入记录:
Preparing: insert into ACT_HI_COMMENT (ID_, TYPE_, TIME_, USER_ID_, TASK_ID_, PROC_INST_ID_, ACTION_, MESSAGE_, FULL_MSG_) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Parameters: 7501(String), comment(String), 2021-06-02 16:43:09.13(Timestamp), null, 5002(String), 2501(String), AddComment(String), 人事复批审批通过(String), java.io.ByteArrayInputStream@1b765a2c(ByteArrayInputStream)
@Test
public void testSelectHistoryTask(){
//流程实例ID
String processInstanceId = "2501";
//任务审核人
String taskAssignee = "王五";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取historyService
HistoryService historyService = processEngine.getHistoryService();
//获取taskService
TaskService taskService = processEngine.getTaskService();
//获取历史审核信息
List list = historyService
.createHistoricActivityInstanceQuery()
.activityType("userTask")//只获取用户任务
.processInstanceId(processInstanceId)
.taskAssignee(taskAssignee)
.finished()
.list();
for(HistoricActivityInstance instance:list){
System.out.println("任务名称:"+instance.getActivityName());
System.out.println("任务开始时间:"+instance.getStartTime());
System.out.println("任务结束时间:"+instance.getEndTime());
System.out.println("任务耗时:"+instance.getDurationInMillis());
//获取审核批注信息
List taskComments = taskService.getTaskComments(instance.getTaskId());
if(taskComments.size()>0){
System.out.println("审批批注:"+taskComments.get(0).getFullMessage());
}
}
}
@Test
public void testDefinitionQuery(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取仓库服务
RepositoryService repositoryService = processEngine.getRepositoryService();
//获取流程定义集合
List<ProcessDefinition> processDefinitionList = repositoryService
.createProcessDefinitionQuery()
.processDefinitionKey("leaveProcess")
.list();
//遍历集合
for (ProcessDefinition definition:processDefinitionList){
System.out.println("流程定义ID:"+definition.getId());
System.out.println("流程定义名称:"+definition.getName());
System.out.println("流程定义key:"+definition.getKey());
System.out.println("流程定义版本:"+definition.getVersion());
System.out.println("流程部署ID:"+definition.getDeploymentId());
System.out.println("====================");
}
}
现在我们的流程资源文件已经上传到数据库了,如果其他用户想要查看这些资源文件,可以从数据库中把资源文件下载到本地。
@Test
public void testDownloadResource() throws Exception {
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取仓库服务
RepositoryService repositoryService = processEngine.getRepositoryService();
//获取流程定义集合
List<ProcessDefinition> list = repositoryService
.createProcessDefinitionQuery()
.processDefinitionKey("leaveProcess")
.orderByProcessDefinitionVersion()//按照版本排序
.desc()//降序
.list();
//获取最新那个
ProcessDefinition definition =list.get(0);
//获取部署ID
String deploymentId = definition.getDeploymentId();
//获取bpmn的输入流
InputStream bpmnInput = repositoryService.getResourceAsStream(
deploymentId,
definition.getResourceName());
//获取png的输入流
InputStream pngInput = repositoryService.getResourceAsStream(
deploymentId,
definition.getDiagramResourceName());
//设置bpmn输入
FileOutputStream bpmnOutPut = new FileOutputStream("D:/leave.bpmn");
//设置png输入
FileOutputStream pngOutPut = new FileOutputStream("D:/leave.png");
IOUtils.copy(bpmnInput,bpmnOutPut);
IOUtils.copy(pngInput,pngOutPut);
}
@Test
public void testDeleteDeploy(){
//流程部署Id
String deploymentId = "10001";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取仓库服务
RepositoryService repositoryService = processEngine.getRepositoryService();
//删除流程定义,如果该流程定义已有流程实例启动则删除时出错
repositoryService.deleteDeployment(deploymentId);
//设置true 级联删除流程定义,即使该流程有流程实例启动也可以删除,设置为false非级别删除方式,如果流程
//repositoryService.deleteDeployment(deploymentId,true);
}
说明:
如果该流程定义下没有正在运行的流程,则可以用普通删除。
如果该流程定义下存在已经运行的流程,使用普通删除报错,可用级联删除方法将流程及相关记录全部删除。
项目开发中级联删除操作一般只开放给超级管理员使用.
用户或程序按照流程定义内容发起一个流程,这就是一个流程实例。
流程定义和流程实例的图解:
请问会创建几个流程实例?
流程发起之后,目前设定的部门审批人都是李四,李四在审批之前需要看到申请人申请的时间和申请的理由,才能决定是否同意.
那么申请人的请假信息【请假时间、请假理由】是如何绑定到流程中的呢?
此时就需要使用到BusinessKey
@Test
public void testStartProcess(){
String businessKey = "8001";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
//根据流程定义的key启动流程实例,这个key是在定义bpmn的时候设置的
//在启动流程的时候将业务key加入进去
ProcessInstance instance = runtimeService
.startProcessInstanceByKey("leaveProcess",businessKey);
//获取流程实例的相关信息
System.out.println("流程定义的id = " + instance.getProcessDefinitionId());
System.out.println("流程实例的id = " + instance.getId());
}
观察数据库可以发现,在activiti的act_ru_execution表,字段BUSINESS_KEY就是存放业务KEY的。
BusinessKey
并关联对应的业务信息呢?@Test
public void testGetBusinessKey(){
//任务负责人
String assignee = "李四";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取RuntimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
System.out.println("流程定义id = " + task.getProcessDefinitionId());
System.out.println("流程实例id = " + task.getProcessInstanceId());
System.out.println("任务id = " + task.getId());
System.out.println("任务名称 = " + task.getName());
//根据任务上的流程实例Id查询出对应的流程实例对象,从流程实例对象中获取BusinessKey
ProcessInstance instance = runtimeService
.createProcessInstanceQuery()
.processInstanceId(task.getProcessInstanceId())
.singleResult();
System.out.println("业务key:"+instance.getBusinessKey());
System.out.println("===================");
}
}
全部流程实例挂起场景:
例如公司制度改变过程中的流程, 总经理更换过程中的流程,有100个人的流程, 70个人已经完成,30个人流程正好在总经理更换中,就需要挂起.
比如我们的业务流程为:
【开始节点】–>【A节点】–>【B节点】–>【C节点】–>【结束节点】
【C节点】的业务逻辑需要和外部接口交互,刚好外部接口出问题了,如果剩下的流程都走到【C节点】,执行【C节点】的业务逻辑,那都会报错,我们就可以把流程挂起,等待外部接口可用之后再重新激活流程.
@Test
public void testSuspendAllProcessInstance(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
//获取流程定义对象
ProcessDefinition processDefinition = repositoryService
.createProcessDefinitionQuery()
.processDefinitionKey("leaveProcess")
.singleResult();
boolean suspended = processDefinition.isSuspended();
//输出流程定义状态
System.out.println("流程定义状态:"+(suspended ?"已挂起":"已激活"));
String processDefinitionId = processDefinition.getId();
if(suspended){
//如果是挂起,可以执行激活操作 ,参数1 :流程定义id ,参数2:是否激活流程实例,参数3:激活时间
repositoryService.activateProcessDefinitionById(processDefinitionId,true,null);
System.out.println("流程ID:"+processDefinitionId+",已激活");
}else{
//如果是激活,可以执行挂起操作 ,参数1 :流程定义id ,参数2:是否暂停流程实例,参数3:激活时间
repositoryService.suspendProcessDefinitionById(processDefinitionId,true,null);
System.out.println("流程ID:"+processDefinitionId+",已挂起");
}
}
@Test
public void testSuspendStatus(){
//任务负责人
String assignee = "李四";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
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.isSuspended()?"已挂起":"已激活"));
System.out.println("===================");
}
}
利用之前的测试用例,测试在流程挂起的情况是否能发起新的流程实例
org.activiti.engine.ActivitiException: Cannot start process instance. Process definition 请假流程 (id = leaveProcess:1:4) is suspended
利用之前的测试用例,测试在流程挂起的情况是否可以处理任务
org.activiti.engine.ActivitiException: Cannot complete a suspended task
单个流程实例挂起场景
评分流程:可设置多级评分,评分流程会按照从上往下的顺序,依次评分;评分人必须在评分截至时间内完成评分,否则不允许继续评分,流程将会挂起,停止流转;
查询所有的流程实例
@Test
public void testQueryProcessInstance(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService
RuntimeService runtimeService = processEngine.getRuntimeService();
List<ProcessInstance> processInstanceList = runtimeService
.createProcessInstanceQuery()
.processDefinitionKey("leaveProcess")
.list();
for(ProcessInstance processInstance:processInstanceList){
System.out.println("流程实例Id:"+processInstance.getId()+",状态:"+(processInstance.isSuspended()?"已挂起":"已激活"));
}
}
挂起某个流程实例
@Test
public void testSuspendSingleProcessInstance(){
//流程实例Id
String processInstanceId = "2501";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService
RuntimeService runtimeService = processEngine.getRuntimeService();
//根据流程实例Id获取流程实例对象
ProcessInstance processInstance = runtimeService
.createProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
//状态
boolean suspended = processInstance.isSuspended();
System.out.println("流程实例ID:"+processInstanceId+",状态:"+ (suspended?"已挂起":"已激活"));
if(suspended){
runtimeService.activateProcessInstanceById(processInstanceId);
System.out.println("流程实例ID:"+processInstanceId+",状态修改为已激活");
}else{
runtimeService.suspendProcessInstanceById(processInstanceId);
System.out.println("流程实例ID:"+processInstanceId+",状态修改为已挂起");
}
}
Activiti 使用 UEL 表达式, UEL 是 java EE6 规范的一部分, UEL(Unified Expression Language)即 统一表达式语言。
IDEA中的actiBPM插件在修改Assignee存在bug,在界面上修改了,但是实际文件并没有修改.所以我们需要借助编辑工具在xml文件中修改一下Assignee
修改流程定义之后重新进行部署
编写代码配置负责人
@Test
public void testStartProcess(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("assignee0","zhangsan");
variables.put("assignee1","lisi");
//根据流程定义的key启动流程实例,这个key是在定义bpmn的时候设置的
ProcessInstance instance = runtimeService
.startProcessInstanceByKey("leaveProcess",variables);
//获取流程实例的相关信息
System.out.println("流程定义的id = " + instance.getProcessDefinitionId());
System.out.println("流程实例的id = " + instance.getId());
}
执行成功后,可以在act_ru_variable表中看到刚才map中的数据
自定义一个任务监听器类,然后此类必须实现org.activiti.engine.delegate.TaskListener接口
package cn.wolfcode;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
/**
* Created by wolfcode
*/
public class AssigneeTaskListener implements TaskListener {
public void notify(DelegateTask delegateTask) {
if(delegateTask.getName().equals("部门经理审批")){
delegateTask.setAssignee("赵六");
}else if(delegateTask.getName().equals("部门经理审批")){
delegateTask.setAssignee("孙七");
}
}
}
在bpmn文件中配置监听器
注意:虽然流程变量中可以存储业务数据,可以通过Activiti的API查询流程变量从而实现查询业务数据,但是不建议这么使用,因为业务数据查询由业务系统负责,Activiti设置流程变量是为了流程执行需要而创建的。
注意:
如果将POJO存储到流程变量中,必须实现序列化接口Serializable,为了防止由于新增字段无法反序列化。
流程变量的作用域范围可以是一个流程实例(ProcessInstance)、一个任务(Task)或一个执行实例(Execution)。
实际开发中一般不用local变量,了解即可.
在属性上使用UEL表达式
可以在 assignee 处设置 UEL 表达式,表达式的值为任务的负责人,比如: ${assignee}, assignee 就是一个流程变量名称。
Activiti获取UEL表达式的值,即流程变量assignee的值 ,将assignee的值作为任务的负责人进行任务分配
在连线上使用UEL表达式
可以在连线上设置UEL表达式,决定流程走向。
比如:${price<10000} 。price就是一个流程变量名称,uel表达式结果类型为布尔类型。
如果UEL表达式是true,要决定 流程执行走向。
需求:员工创建请假流程申请单,由部门经理审批,部门经理审批通过后请假3天以下(含3天)的由人事经理直接审批,3天以上的由总经理审批,总经理审批通过再通过人事经理审批。
本次画BPMN文件的时候将部门经理的assignee设置为李四,总经理审批的assignee设置为王五,总经理审批的assignee设置为王五,人事复批存档设置为赵六。
package cn.wolfcode.demo;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wolfcode
*/
public class VariablesTest {
/**
* 部署
*/
@Test
public void testDeploy(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//进行部署
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmn/leave-variables.bpmn")
.name("请假流程")
.deploy();
//输出部署的一些信息
System.out.println("流程部署ID:"+deployment.getId());
System.out.println("流程部署名称:"+deployment.getName());
}
@Test
public void testStartProcess(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String,Object> variables = new HashMap<String,Object>();
variables.put("day",2);
//根据流程定义的key启动流程实例,这个key是在定义bpmn的时候设置的
ProcessInstance instance = runtimeService.startProcessInstanceByKey("leaveVariablesProcess",variables);
//获取流程实例的相关信息
System.out.println("流程定义的id = " + instance.getProcessDefinitionId());
System.out.println("流程实例的id = " + instance.getId());
}
@Test
public void testSelectTodoTaskList(){
//任务负责人
String assignee = "李四";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveVariablesProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
System.out.println("流程定义id = " + task.getProcessDefinitionId());
System.out.println("流程实例id = " + task.getProcessInstanceId());
System.out.println("任务id = " + task.getId());
System.out.println("任务名称 = " + task.getName());
}
}
@Test
public void testCompleteTask(){
//任务负责人
String assignee = "李四";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveVariablesProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
taskService.complete(task.getId());
}
}
}
注意事项:
1.如果UEL表达式中流程变量名不存在则报错。
2.如果如果UEL表达式都不符合条件,流程报错。
3.如果连接不设置条件/条件都满足,每个连线都会走.
在流程定义中在任务结点的 assignee 固定设置任务负责人,在流程定义时将参与者固定设置在.bpmn 文件中,如果临时任务负责人变更则需要修改流程定义,系统可扩展性差。
针对这种情况可以给任务设置多个候选人,可以从候选人中选择参与者来完成任务。
查看bpmn文件
我们可以看到部门经理的审核人已经设置为 lisi,wangwu 这样的一组候选人,可以使用activiti:candiateUsers=”用户 1,用户 2,用户 3”的这种方式来实现设置一组候选人
package cn.wolfcode.demo;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wolfcode
*/
public class CandidateTest {
/**
* 部署
*/
@Test
public void testDeploy(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//进行部署
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmn/leave-candidate.bpmn")
.name("请假流程-候选人")
.deploy();
//输出部署的一些信息
System.out.println("流程部署ID:"+deployment.getId());
System.out.println("流程部署名称:"+deployment.getName());
}
//启动流程
@Test
public void testStartProcess(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
//根据流程定义的key启动流程实例,这个key是在定义bpmn的时候设置的
ProcessInstance instance = runtimeService.startProcessInstanceByKey("leaveCandidateProcess");
//获取流程实例的相关信息
System.out.println("流程定义的id = " + instance.getProcessDefinitionId());
System.out.println("流程实例的id = " + instance.getId());
}
}
//查询候选任务
@Test
public void testSelectCandidateTaskList(){
//任务负责人
String candidateUser = "李四";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveCandidateProcess")
.taskCandidateUser(candidateUser)
.list();
//遍历任务列表
for(Task task:taskList){
System.out.println("流程定义id = " + task.getProcessDefinitionId());
System.out.println("流程实例id = " + task.getProcessInstanceId());
System.out.println("任务id = " + task.getId());
System.out.println("任务名称 = " + task.getName());
}
}
@Test
public void testClaimTask(){
//任务ID
String taskId = "2505";
String assignee = "张三";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//领取任务
taskService.claim(taskId,assignee);
}
如果候选任务没有进行领取就直接完成的话,那么在历史记录中就不会记录是哪个用户执行了这个任务.
所以对于这种候选人的任务,我们需要先领取再完成.
//执行任务
@Test
public void testCompleteTask(){
//任务ID
String taskId = "2505";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
taskService.complete(taskId);
}
排他网关(ExclusiveGateway)(异或网关或基于数据的排他网关),用来在流程中实现决策。当流程执行到这个网关的时候,所有分支都会判断条件是否为true,如果为true则执行该分支。
注意:
排他网关只会选择一个为true的分支执行(即使有两个分支条件都为true,排他网关也只会选择一条分支去执行,选择序号小的路径执行)。
本次画BPMN文件的时候将部门经理的assignee设置为李四,总经理审批的assignee设置为王五,总经理审批的assignee设置为王五,人事复批存档设置为赵六。
package cn.wolfcode.demo;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.After;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wolfcode
*/
public class EGatewayTest {
/**
* 部署
*/
@Test
public void testDeploy(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//进行部署
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmn/leave-EGateway.bpmn")
.name("请假流程-排他网关")
.deploy();
//输出部署的一些信息
System.out.println("流程部署ID:"+deployment.getId());
System.out.println("流程部署名称:"+deployment.getName());
}
@Test
public void testStartProcess(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String,Object> variables = new HashMap<String,Object>();
variables.put("day",0);
//根据流程定义的key启动流程实例,这个key是在定义bpmn的时候设置的
ProcessInstance instance = runtimeService.startProcessInstanceByKey("leaveEGatewayProcess",variables);
//获取流程实例的相关信息
System.out.println("流程定义的id = " + instance.getProcessDefinitionId());
System.out.println("流程实例的id = " + instance.getId());
}
@Test
public void testCompleteTask(){
//任务负责人
String assignee = "张三";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveEGatewayProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
taskService.complete(task.getId());
}
}
}
并行网关(InclusiveGateway)允许将流程分成多条分支,也可以把多条分支汇聚到一起,并行网关的功能是基于进入和外出的顺序流的。
并行网关不会解析条件。即使顺序流中定义了条件,也会被忽略
本次画BPMN文件的时候将技术经理的assignee设置为李四,项目经理审批的assignee设置为王五,人事复批的assignee设置为王五
package cn.wolfcode.demo;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wolfcode
*/
public class PGatewayTest {
/**
* 部署
*/
@Test
public void testDeploy(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//进行部署
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmn/leave-PGateway.bpmn")
.name("请假流程-并行网关")
.deploy();
//输出部署的一些信息
System.out.println("流程部署ID:"+deployment.getId());
System.out.println("流程部署名称:"+deployment.getName());
}
@Test
public void testStartProcess(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String,Object> variables = new HashMap<String,Object>();
//根据流程定义的key启动流程实例,这个key是在定义bpmn的时候设置的
ProcessInstance instance = runtimeService.startProcessInstanceByKey("leavePGatewayProcess");
//获取流程实例的相关信息
System.out.println("流程定义的id = " + instance.getProcessDefinitionId());
System.out.println("流程实例的id = " + instance.getId());
}
//完成任务
@Test
public void testCompleteTask(){
//任务负责人
String assignee = "张三";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leavePGatewayProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
taskService.complete(task.getId());
}
}
}
包含网关可以看做是排他网关和并行网关的结合体。
需求:出差申请大于3天需要由项目经理审批,小于3等于天由技术经理审批,出差申请必须经过人事助理审批。
本次画BPMN文件的时候将技术经理的assignee设置为张三,项目经理的assignee设置为李四,人事助理的assignee设置为王五,人事复批存档设置为赵六。
package cn.wolfcode.demo;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wolfcode
*/
public class IGatewayTest {
/**
* 部署
*/
@Test
public void testDeploy(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//进行部署
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmn/leave-IGateway.bpmn")
.name("请假流程-包含网关")
.deploy();
//输出部署的一些信息
System.out.println("流程部署ID:"+deployment.getId());
System.out.println("流程部署名称:"+deployment.getName());
}
@Test
public void testStartProcess(){
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String,Object> variables = new HashMap<String,Object>();
variables.put("day",5);
//根据流程定义的key启动流程实例,这个key是在定义bpmn的时候设置的
ProcessInstance instance = runtimeService.startProcessInstanceByKey("leaveIGatewayProcess",variables);
//获取流程实例的相关信息
System.out.println("流程定义的id = " + instance.getProcessDefinitionId());
System.out.println("流程实例的id = " + instance.getId());
}
//完成任务
@Test
public void testCompleteTask(){
//任务负责人
String assignee = "王五";
//创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取任务集合
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("leaveIGatewayProcess")
.taskAssignee(assignee)
.list();
//遍历任务列表
for(Task task:taskList){
taskService.complete(task.getId());
}
}
}