目录
第一步:流程定义
第二步:流程部署
第三步:流程启动
第四步:查询用户任务
注:这里有个问题是在eclipse中没有idea那样可以给某个任务环节分配用户,没找到!!!!!!
第五步:流程处理
创建工作流引擎的流程图
就是将流程图中定义的信息(.bpmn文件和pang图片)保存到数据库中
package org.wxy.com.activiti;
import java.util.Date;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
//流程部署
@Test
public void testprocessdeploy(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//通过流程架构知道所有的service都是通过ProcessEngine调用的
//创建部署对象
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.createDeployment().addClasspathResource("leveldef.bpmn")
.addClasspathResource("leveldef.png")
.name("请假审批流程")
.activateProcessDefinitionsOn(new Date())
.deploy();
}
}
act_re_deployment 记录部署信息
act_re_procdef 记录流程定义的信息
act_ge_bytearray 记录定义的bpmn文件和pang文件
package org.wxy.com.activiti;
import java.util.Date;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
//流程实例的启动
@Test
public void testprocessstart(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//创建流程启动对象
RuntimeService runtimeService = processEngine.getRuntimeService();
//根据流程定义的key,启动某一个流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
System.out.println("活动id"+ processInstance.getActivityId());
System.out.println("实例id"+ processInstance.getProcessInstanceId());
}
}
注意:如果启动流程时提示流程定义被终止了( Cannot start process instance. Process definition My process (id = xxx) is suspended)
org.activiti.engine.ActivitiException: Cannot start process instance. Process definition My process (id = myProcess:1:4) is suspended
at org.activiti.engine.impl.util.ProcessInstanceHelper.createAndStartProcessInstance(ProcessInstanceHelper.java:67)
at org.activiti.engine.impl.util.ProcessInstanceHelper.createAndStartProcessInstance(ProcessInstanceHelper.java:51)
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.createAndStartProcessInstance(StartProcessInstanceCmd.java:109)
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:102)
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:37)
at org.activiti.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:37)
at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:78)
at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:57)
at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:42)
at org.activiti.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:48)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:63)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:35)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:44)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:39)
at org.activiti.engine.impl.RuntimeServiceImpl.startProcessInstanceByKey(RuntimeServiceImpl.java:
激活流程定义
package org.wxy.com.activiti;
import java.util.Date;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
//流程实例的启动
@Test
public void testprocessstart(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//流程定义激活即有效
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.activateProcessDefinitionByKey("myProcess");
//流程定义终止即无效
//repositoryService.suspendProcessDefinitionByKey("myProcess");
//创建流程启动对象
RuntimeService runtimeService = processEngine.getRuntimeService();
//根据流程定义的key,启动某一个流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
System.out.println("活动id"+ processInstance.getActivityId());
System.out.println("实例id"+ processInstance.getProcessInstanceId());
}
}
分析执行流程部署操作涉及到的表(5张表)
act_hi_actinst 显示已经完成的活动实例
act_hi_procinst 显示历史流程实例
act_hi_taskinst 任务实例
act_ru_execution 执行表
act_ru_task 执行中的任务表
在act_hi_actinst 表中 end_time为null表示该环节流程(部门领导审批环节)还未执行,即将要执行的任务
其中在act_ru_task表中显示 当前正将要执行的任务
//流程任务查询
@Test
public void testprocessquery(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
List list = taskService.createTaskQuery().processDefinitionKey("myProcess")
.taskAssignee("zhangsan").list();
for(int i=0;i
//流程任务处理
@Test
public void testprocesstaskhandler(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
List list = taskService.createTaskQuery().processDefinitionKey("myProcess")
.taskAssignee("zhangsan").list();
for(int i=0;i
任务处理后相关变化的表
act_ru_task 显示正在运行的任务
act_hi_taskinst 显示历史运行的任务
act_hi_actinst 显示历史实例
act_ru_execution 正在执行的计划
在act_hi_taskinst 表中显示历史任务实例及将要执行的任务实例(end_time 为null)
在act_hi_actinst中显示流程中已经被执行的任务及将要执行的任务(end_time 为null)
在act_ru_task中显示当前流程中(某个环节)正在进行中的任务
流程删除的细节问题:
1.实际应用中如果流程中某个环节没有执行完,此时要求删除流程中的定义的信息怎么处理?
//流程任务处理
@Test
public void testprocesstaskdelete(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
//注意此时总经理审批还未结束
repositoryService.deleteDeployment("1");
}
直接删除会报错!!!!
如果强制要停止之后的审批流程怎么办?
//流程任务处理
@Test
public void testprocesstaskdelete(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
//注意此时总经理审批还未结束
repositoryService.deleteDeployment("1", true);
}
repositoryService.deleteDeployment("1", true);//参数true代表级联删除,此时会先删除未执行的流程节点,然后就可以删除流程定义信息。
流程实例参考:https://www.cnblogs.com/dalianpai/p/11885139.html
流程激活与终止参考:https://juejin.cn/post/6844904004766400519