项目结构
pom.xml
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.19.0.2</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>5.19.0.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
Activiti.cfg.xml --生成所需流程表
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="root" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>
流程bpmn文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/testm1480857817152" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1480857817152" name="" targetNamespace="http://www.activiti.org/testm1480857817152" typeLanguage="http://www.w3.org/2001/XMLSchema">
<process id="myProcess_1" isClosed="false" isExecutable="true" name="act1" processType="None">
<startEvent id="_2" name="StartEvent"/>
<userTask activiti:assignee="comp" activiti:exclusive="true" id="_3" name="actTask">
<extensionElements>
<activiti:executionListener event="start"/>
<activiti:formProperty/>
</extensionElements>
</userTask>
<endEvent id="_4" name="EndEvent"/>
<sequenceFlow id="_5" sourceRef="_3" targetRef="_4"/>
<sequenceFlow id="_6" sourceRef="_2" targetRef="_3"/>
</process>
</definitions>
流程图片
测试方法
public class actCreateTable {
private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 创建流程所需表
@Test
public void createTableForAct(){
// 引擎配置 创建activiti的25张表
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
// 获取流程引擎对象
ProcessEngine processEngine = configuration.buildProcessEngine();
}
// 部署流程
@Test
public void deploy(){
Deployment deployment = processEngine.getRepositoryService() // 获取部署相关的service
.createDeployment() // 创建部署
.addClasspathResource("diagrams/myActPro.bpmn")// 加载资源文件
.name("actTask")
.deploy(); // 部署文件
System.out.println("流程的ID:"+deployment.getId());
System.out.println("流程的Name:"+deployment.getName());
}
//开始运行流程
@Test
public void runTask(){
ProcessInstance processInstance = processEngine.getRuntimeService()
.startProcessInstanceByKey("myProcess_1");
System.out.println("流程实例ID:"+processInstance.getId());
System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId());
}
// 进行任务流程 走到任务节点
@Test
public void finishTask(){
List<Task> list = processEngine.getTaskService()
.createTaskQuery()
.taskAssignee("comp") // 指定委托人 查看当前委托人的所有流程
.list();
for(Task task : list){
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println(String.format( "任务创建时间:" +task.getCreateTime(), "yyyy-MM-DD"));
System.out.println("任务委托人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessDefinitionId());
}
System.out.println("任务ID:"+list);
}
// 走完流程 end
@Test
public void tory(){
processEngine.getTaskService() // 任务相关Service
.complete("7504"); // 指定要完成的任务ID
}
}
流程的curd
public class ActCurd {
private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//通过流程部署id获得流程图片
@Test
public void actQueryById() throws IOException {
InputStream in = processEngine.getRepositoryService()
// 通过流程ID和流程资源名称获得输入流
.getResourceAsStream("5001","diagrams/myActPro.myProcess_1.png");
// 将图片存放在D盘中
FileUtils.copyInputStreamToFile(in,new File("d://activit.png"));
}
// 查询流程定义的集合 -- 查询流程的最高版本
@Test
public void actQueryList(){
List<ProcessDefinition> listPro = processEngine.getRepositoryService() // 获取service
.createProcessDefinitionQuery() //创建流程定义查询
.orderByProcessDefinitionVersion().asc() // 通过流程版本的升序来查询
.list();
// 定义一个有序的map 相同的key 假如添加map的值 后面的值会覆盖前面相同key的值
Map<String,ProcessDefinition> map = new LinkedHashMap<String, ProcessDefinition>();
//遍历集合 根据key来覆盖前面的值 来保证最新的Key覆盖前面的所有老的Key的值
for (ProcessDefinition pro : listPro){
map.put(pro.getKey(),pro);
}
// 定义一个有序的list 来保证最新的Key覆盖前面的所有老的Key的值
List<ProcessDefinition> list = new LinkedList<ProcessDefinition>(map.values());
// 遍历list
for (ProcessDefinition pro : list){
System.out.println("ID_:"+pro.getId());
System.out.println("NAME_:"+pro.getName());
System.out.println("KEY_:"+pro.getKey());
System.out.println("VERSION_:"+pro.getVersion());
System.out.println("===================");
}
}
//流程定义的删除
@Test
public void actDelete(){
List<ProcessDefinition> listPro = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.processDefinitionKey("act2") // 通过key 来进行删除
.list();
// 遍历list
for (ProcessDefinition pro : listPro){
processEngine.getRepositoryService()
// true 设置级联删除,相关表数据全部删除 通过部署id 来删除
.deleteDeployment(pro.getDeploymentId(),true);
}
}
}