【一】
创建java工程,导入jar包
activiti相关jar(可以去activiti资源中案例中获取)
mysql驱动包
【二】
插件定义流程文件(png、bpmn)并导入
【三】
添加配置
activiti配置
log4j配置
activiti-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 流程引擎配置对象 -->
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3307/activiti_db"/>
<property name="jdbcUsername" value="root"/>
<property name="jdbcPassword" value="123"/>
<property name="databaseSchemaUpdate" value="true"/>
</bean>
<!-- 使用工厂创建流程引擎对象 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"/>
</bean>
</beans>
大体架构图
【API操作】
package com.itheima.activiti;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipInputStream;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
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.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
public class ActivitiAPITest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 部署流程定义
* 方式一:加载单个的流程定义文件
* 方式二:加载zip文件
*
* 执行部署流程定义,程序会操作以下三张表
* act_re_deployment(部署表)
* act_re_procdef(流程定义表)
* act_ge_bytearray(二进制表)
* @throws FileNotFoundException
*/
@Test
public void test1() throws FileNotFoundException {
//创建部署构建器对象
DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
// 方式一:加载单个的流程定义文件
// deploymentBuilder.addClasspathResource("qjlc.bpmn");
// deploymentBuilder.addClasspathResource("qjlc.png");
// Deployment deployment = deploymentBuilder.deploy();
//
// System.out.println(deployment.getId());
//将zip文件转换为输入流
ZipInputStream zipInputStream = new ZipInputStream(this.getClass().getClassLoader().getResourceAsStream("process.zip"));
// 方式二:加载zip文件
//ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(new File("d:\\process.zip")));
deploymentBuilder.addZipInputStream(zipInputStream );
Deployment deployment = deploymentBuilder.deploy();
System.out.println(deployment.getId());
}
/**
* 删除流程定义
*/
@Test
public void test2(){
String deploymentId = "101";//部署id
boolean cascade = true;//级联删除
processEngine.getRepositoryService().deleteDeployment(deploymentId, cascade);
}
/**
* 查询流程定义
processEngine.getRepositoryService().createDeploymentQuery().list();
processEngine.getRuntimeService().createProcessInstanceQuery().list();
processEngine.getTaskService().createTaskQuery().list();
processEngine.getIdentityService().createUserQuery().list();
processEngine.getHistoryService().createHistoricActivityInstanceQuery().list();
*
*/
/**
* 查询流程定义 1
*/
@Test
public void test3(){
ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
List<ProcessDefinition> list = query.list(); //直接查询所有
//设置条件
// query.processDefinitionKey("qjlc");//根据key过滤
// query.orderByProcessDefinitionVersion().asc();
// List<ProcessDefinition> list = query.listPage(0, 10);
for (ProcessDefinition processDefinition : list) {
System.out.println(processDefinition.getId() + " " + processDefinition.getName());
}
}
/**
* 查询部署对应的流程定义文件名称和输入流(根据部署id)
* @throws FileNotFoundException
*/
@Test
public void test4() throws Exception{
String deploymentId = "201";//部署id
List<String> names = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
for (String name : names) {
InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId, name);
/*byte[] b = new byte[1024];
in.read(b);
outputStream.write(b);*/
FileUtils.copyInputStreamToFile(in, new File("d:\\"+name));
in.close();
}
}
/**
* 查询部署对应的流程定义输入流(根据流程定义id)
* @throws FileNotFoundException
*/
@Test
public void test5() throws Exception{
String processDefinitionId = "qjlc:6:904";//流程定义id
InputStream pngStream = processEngine.getRepositoryService().getProcessDiagram(processDefinitionId);
FileUtils.copyInputStreamToFile(pngStream, new File("d:\\abc.png"));
}
/**
* 根据流程定义启动流程实例
* 方式一:根据流程定义id启动流程实例
* 方式二:根据流程定义key启动流程实例(建议)-----可以根据当前最新版本的流程定义启动流程实例
*
* 启动流程实例,操作了两张表
* act_ru_execution(流程实例表)
* act_ru_task(任务表)
*/
@Test
public void test6() throws Exception{
//方式一:根据流程定义id启动流程实例
//String processDefinitionId = "qjlc:2:104";//流程定义id
//processEngine.getRuntimeService().startProcessInstanceById(processDefinitionId);
String processDefinitionKey = "qjlc";
//方式二:根据流程定义key启动流程实例
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId() + " " + processInstance.getProcessDefinitionId());
}
/**
* 查询流程实例
*/
@Test
public void test7() throws Exception{
List<ProcessInstance> list = processEngine.getRuntimeService().createProcessInstanceQuery().list();
for (ProcessInstance processInstance : list) {
System.out.println(processInstance.getId());
}
}
/**
* 删除流程实例
*/
@Test
public void test8() throws Exception{
String processInstanceId = "1201";//流程实例id
String deleteReason = "不请假了";//删除原因
processEngine.getRuntimeService().deleteProcessInstance(processInstanceId , deleteReason );
}
/**
* 查询个人任务
*/
@Test
public void test9() throws Exception{
TaskQuery query = processEngine.getTaskService().createTaskQuery();
query.taskAssignee("张三");
query.orderByTaskCreateTime().desc();
List<Task> list = query.list();
for (Task task : list) {
System.out.println(task.getId() + " " + task.getName());
}
}
/**
* 办理个人任务
*/
@Test
public void test10() throws Exception{
String taskId = "1304";
processEngine.getTaskService().complete(taskId);
}
}
6.API总结
6.1
几个接口(和表有对应关系):
Deployment------act_re_deployment
ProcessDefinition-----act_re_procdef
ProcessInstance------act_ru_execution
Task-----act_ru_task
6.2
几个Query对象
DeploymentQuery------act_re_deployment
ProcessDefinitionQuery-----act_re_procdef
ProcessInstanceQuery------act_ru_execution
TaskQuery-----act_ru_task
6.3
几个Service
RepositoryService----操作部署表、流程定义表等静态资源信息表
RuntimeService----操作流程实例表、任务表等动态信息表
TaskService-----操作任务表
HistoryService----操作历史表
IdentityService----操作用户表、组表、关系表