activiti 工作流 使用
一、安装activiti插件
- 由于idea中找不到actiBpmn 所以先下载好,现在导入本地的jar包
二、activiti的文件配置
<properties>
<slf4j.version>1.7.2</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- activiti start -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-image-generator</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-process-validation</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.21.0</version>
<exclusions>
<exclusion>
<artifactId>commons-dbcp</artifactId>
<groupId>commons-dbcp</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>5.21.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>5.21.0</version>
<exclusions>
<exclusion>
<artifactId>commons-collections</artifactId>
<groupId>commons-collections</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- LOGGING begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>compile</scope>
</dependency>
<!-- LOGGING end -->
</dependencies>
三、配置对应的文件 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="123456" />
</bean>
</beans>
四、activiti的实现流程
@Test
public void testDeployEmpLeave(){
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment().addClasspathResource("test2.bpmn").deploy();
System.out.println("部署文件的ID"+deploy.getId());
}
@Test
public void testDeleteBpmnFile(){
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.deleteDeployment("2501");
System.out.println("删除部署文件 id=2501");
}
@Test
public void StartInstance(){
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1");
System.out.println("启动当前实例的id值="+processInstance.getId());
procIntanceId = String.valueOf(processInstance.getId());
System.out.println(procIntanceId);
}
@Test
public void testGetAndCompleteFirstUserTask(){
ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();
TaskService taskService=processEngine.getTaskService();
Task task=taskService.createTaskQuery().processInstanceId(procIntanceId).singleResult();
System.out.println("第一个任务名="+task.getName());
System.out.println("第一个任务Id="+task.getId());
taskService.complete(task.getId());
}
@Test
public void testFlowEnd() {
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
TaskService taskService=processEngine.getTaskService();
Task task=taskService.createTaskQuery().processInstanceId(procIntanceId).singleResult();
if(task==null) {
System.out.println("此实例流程已经结束");
}else
System.out.println("此实例流程并没有结束处理");
}
@Test
public void testSetTaskVariable() {
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
TaskService taskService=processEngine.getTaskService();
Task task=taskService.createTaskQuery().processInstanceId(procIntanceId).singleResult();
taskService.setVariable(task.getId(),"username","胡图图");;
taskService.setVariable(task.getId(),"age",22);
}
@Test
public void testGetTaskVariable() {
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
TaskService taskService=processEngine.getTaskService();
Task task=taskService.createTaskQuery().processInstanceId(procIntanceId).singleResult();
System.out.println(taskService.getVariable(task.getId(), "username"));
System.out.println(taskService.getVariable(task.getId(), "age"));
}