activti简单使用
2018-08-05 本文已影响55人
小鱼嘻嘻
创建springboot项目
创建一个springboot项目引入相关的依赖,引入activiti 需要的相关依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.activiti.org</groupId>
<artifactId>activiti</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<activiti.version>5.22.0</activiti.version>
<h2.version>1.4.180</h2.version>
<junit.version>4.10</junit.version>
<mockito.version>1.10.19</mockito.version>
<powermock.version>1.6.6</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<!-- TEST begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.8</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
idea 上安装activiti 插件
可以参考这篇文章:安装activiti 插件
使用的效果如图:
image.png
生成的文件是以bpmn结尾,同时我们可以把bpmn文件转化成xml 文件,生成的xml文件如下:
<?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:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
expressionLanguage="http://www.w3.org/1999/XPath"
id="m1533434256601" name="" targetNamespace="http://www.activiti.org/test"
typeLanguage="http://www.w3.org/2001/XMLSchema">
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
<startEvent activiti:formKey="leave" activiti:initiator="xixi" id="start" name="StartEvent"/>
<userTask activiti:assignee="yuxi" activiti:exclusive="true" id="_3" name="审核"/>
<endEvent id="end" name="EndEvent"/>
<sequenceFlow id="_5" sourceRef="start" targetRef="_3"/>
<sequenceFlow id="_6" sourceRef="_3" targetRef="end"/>
</process>
<bpmndi:BPMNDiagram
documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0"
id="Diagram-_1" name="New Diagram">
<bpmndi:BPMNPlane bpmnElement="myProcess_1">
<bpmndi:BPMNShape bpmnElement="start" id="Shape-start">
<omgdc:Bounds height="32.0" width="32.0" x="175.0" y="50.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3">
<omgdc:Bounds height="55.0" width="85.0" x="145.0" y="160.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4">
<omgdc:Bounds height="32.0" width="32.0" x="170.0" y="310.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="_5" id="BPMNEdge__5" sourceElement="_2" targetElement="_3">
<omgdi:waypoint x="191.0" y="82.0"/>
<omgdi:waypoint x="191.0" y="160.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="_3" targetElement="_4">
<omgdi:waypoint x="186.0" y="215.0"/>
<omgdi:waypoint x="186.0" y="310.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
启动一个简单的流程
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
public class VeryProcessTest {
ProcessEngine processEngine;
@Before
public void testBefore() {
// 创建流程引擎
processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
.buildProcessEngine();
}
@Test
public void test() {
//部署流程定义文件
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = processEngine.getRepositoryService()
.createDeployment()
.addClasspathResource("leave.bpmn")
.deploy();
assertNotNull(deploy);
RuntimeService runtimeService = processEngine.getRuntimeService();
//创建流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(
deploy.getId()).singleResult();
//初始化流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinition.getKey());
assertNotNull(processInstance);
}
}
- 需要注意的是这里使用的是内存数据库,可以参考pom依赖文件。
- 需要注意activiti 部署流程资源有很多种方式,可以选择你比较喜欢的。
流程部署方式
创建一个流程的步骤是:
获取流程引擎(ProcessEngine)-->部署流程定义文件-->创建流程定义--->初始化具体流程