大家好,又见面了,我是你们的朋友全栈君。
启动流程实例
什么是流程实例?根据一个流程定义具体的一次执行过程就是一个流程实例,一个流程定义对应多个流程实例(一对多关系)
为了演示:在流程图中指定办理人是谁,现在是写死的,表示只能张三能提交请假申请。后面会讲解如何动态指定。
//根据流程定义的Id启动一个流程实例(操作ACT_RU_EXECUTION、ACT_RU_TASK、ACT_HI_PROCINST、ACT_HI_ACTINST、ACT_HI_TASKINST、ACT_RU_IDENTITYLINK、ACT_HI_IDENTITYLINK表)
@Test
public void test6() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
String processDefinitionId=”qjlc:1:104″;
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceById(processDefinitionId);
System.out.print(processInstance.getId());//201
}
查询个人任务列表
@Test
public void test7() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskQuery query = processEngine.getTaskService().createTaskQuery();
query.taskAssignee(“张三”);
List list = query.list();
for (Task item : list) {
System.out.print(item.getId()+”===”+item.getName());//204===提交请假申请
}
}
办理任务
//办理任务(主要操作ACT_RU_EXECUTION、ACT_RU_TASK表)
@Test
public void test8() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
processEngine.getTaskService().complete(“204”);
}
办理任务后,流程就走到了下一个节点,再次查询张三个人任务列表就查不到了,此时用李四去查就可以了。
使用Activiti框架的API操作流程
/**
* 使用Activiti框架的API操作流程
*/
public class ActivitiAPITest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 部署流程定义 方式一:读取单个的流程定义文件 方式二:读取zip压缩文件
*/
@Test
public void test1() {
DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
// 方式一:读取单个的流程定义文件
deploymentBuilder.addClasspathResource(“test1.bpmn”);
deploymentBuilder.addClasspathResource(“test1.png”);
Deployment deployment = deploymentBuilder.deploy();
// 方式二:读取zip压缩文件
/*ZipInputStream zipInputStream = new ZipInputStream(this.getClass().getClassLoader().getResourceAsStream(“process.zip”));
deploymentBuilder.addZipInputStream(zipInputStream);
deploymentBuilder.name(“请假流程部署”);
Deployment deployment = deploymentBuilder.deploy();*/
}
/**
* 查询部署列表
*/
@Test
public void test2() {
// 部署查询对象,查询表act_re_deployment
DeploymentQuery query = processEngine.getRepositoryService().createDeploymentQuery();
List list = query.list();
for (Deployment deployment : list) {
String id = deployment.getId();
System.out.println(id);
}
}
/**
* 查询流程定义列表
*/
@Test
public void test3() {
// 流程定义查询对象,查询表act_re_procdef
ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
List list = query.list();
for (ProcessDefinition pd : list) {
System.out.println(pd.getName() + “” + pd.getId());
}
}
/**
* 删除部署信息
*/
@Test
public void test4() {
String deploymentId = “1001”;
// processEngine.getRepositoryService().deleteDeployment(deploymentId);
processEngine.getRepositoryService().deleteDeployment(deploymentId,true);
}
/**
* 删除流程定义(通过删除部署信息达到删除流程定义的目的)
*/
@Test
public void test5() {
String deploymentId = “1401”;
// processEngine.getRepositoryService().deleteDeployment(deploymentId);
processEngine.getRepositoryService().deleteDeployment(deploymentId,
true);
}
/**
* 查询一次部署对应的流程定义文件名称和对应的输入流(bpmn png)
*
* @throws Exception
*/
@Test
public void test6() throws Exception {
String deploymentId = “101”;
List names = processEngine.getRepositoryService()
.getDeploymentResourceNames(deploymentId);
for (String name : names) {
System.out.println(name);
InputStream in = processEngine.getRepositoryService()
.getResourceAsStream(deploymentId, name);
// 将文件保存到本地磁盘
/*
* OutputStream out = new FileOutputStream(new File(“d:\\” + name));
* byte[] b = new byte[1024]; int len = 0; while((len =
* in.read(b))!=-1) { out.write(b, 0, len); } out.close();
*/
FileUtils.copyInputStreamToFile(in, new File(“d:\\” + name));
in.close();
}
}
/**
* 获得png文件的输入流
*
* @throws Exception
*/
@Test
public void test7() throws Exception {
String processDefinitionId = “qjlc:9:1204”;
InputStream pngInputStream = processEngine.getRepositoryService()
.getProcessDiagram(processDefinitionId);
FileUtils.copyInputStreamToFile(pngInputStream, new File(“d:\\my.png”));
}
/**
* 启动流程实例 方式一:根据流程定义的id启动 方式二:根据流程定义的key启动(自动选择最新版本的流程定义启动流程实例)
*/
@Test
public void test8() {
/*
* String processDefinitionId = “qjlc:9:1204”; ProcessInstance
* processInstance =
* processEngine.getRuntimeService().startProcessInstanceById
* (processDefinitionId ); System.out.println(processInstance.getId());
*/
String processDefinitionKey = “qjlc”;
ProcessInstance processInstance = processEngine.getRuntimeService()
.startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId());
}
/**
* 查询流程实例列表,查询act_ru_execution表
*/
@Test
public void test9(){
//流程实例查询对象,查询act_ru_execution表
ProcessInstanceQuery query = processEngine.getRuntimeService().createProcessInstanceQuery();
query.processDefinitionKey(“qjlc”);
query.orderByProcessInstanceId().desc();
query.listPage(0, 2);
List list = query.list();
for (ProcessInstance pi : list) {
System.out.println(pi.getId() + ” ” + pi.getActivityId());
}
}
/**
* 结束流程实例,操作的表act_ru_execution act_ru_task
*/
@Test
public void test10(){
String processInstanceId = “1601”;
processEngine.getRuntimeService().deleteProcessInstance(processInstanceId , “我愿意”);
}
/**
* 查询任务列表
*/
@Test
public void test11(){
//任务查询对象,查询act_ru_task表
TaskQuery query = processEngine.getTaskService().createTaskQuery();
String assignee = “李四”;
query.taskAssignee(assignee);
query.orderByTaskCreateTime().desc();
List list = query.list();
for (Task task : list) {
System.out.println(task.getId());
}
}
/**
* 办理任务
*/
@Test
public void test12(){
String taskId = “2902”;
processEngine.getTaskService().complete(taskId);
}
/**
* 直接将流程向下执行一步
*/
@Test
public void test13(){
String executionId = “2701”;//流程实例id
processEngine.getRuntimeService().signal(executionId);
}
/**
* 查询最新版本的流程定义列表
*/
@Test
public void test14(){
ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
query.orderByProcessDefinitionVersion().asc();
List list = query.list();
Map map = new HashMap();
for (ProcessDefinition pd : list) {
map.put(pd.getKey(), pd);
}
ArrayList lastList = new ArrayList<>(map.values());
for (ProcessDefinition processDefinition : lastList) {
System.out.println(processDefinition.getName() + ” “+ processDefinition.getVersion() );
}
}
}
Activiti框架提供的Service对象
RepositoryService—-操作静态的资源(流程定义,bpmn、png)
RuntimeService—–操作流程实例(启动流程实例、查询流程实例、结束流程实例)
TaskService—–操作任务(查询任务、办理任务)
HistoryService—-操作历史数据
Activiti框架提供的对象(和表有对应关系)
Deployment—–act_re_deployment
ProcessDefinition—-act_re_procdef
ProcessInstance—–act_ru_execution
Task—–act_ru_task
使用网页版流程设计器
1、将activiti-explorer.war复制到tomcat中
2、启动tomcat,访问http://lcoalhost:8080/activiti-explorer
3、使用kermit/kermit登录
4、
5、
参考文档:http://www.cnblogs.com/llzgzljl/archive/2013/10/07/3356108.html
学习资源:http://www.mossle.com/index.do
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/133045.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...