1.什么是流程定义
流程定义是线下bpmn2.0标椎去描述业务流程,通常使用activiti-explorer(web控制台)或 activiti-eclipse-designer 插件对业务流程进行建模,这两种方式都遵循 bpmn2.0 标准。本教程使用activiti-eclipse-designer 插件完成流程建模。使用designer 设计器绘制流程,会生成两个文件:.bpmn 和.png
2. .bpmn文件
使用activiti-desinger设计业务流程,会生成.bpmn文件,首先将hoilday.bpmn文件改为hoilday.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" xmlns:tns="http://www.activiti.org/test" 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="m1585706288577" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
<process id="hoilday" isClosed="false" isExecutable="true" processType="None">
<startEvent id="_2" name="流程开始"/>
<userTask activiti:assignee="zhangsan" activiti:exclusive="true" id="_3" name="用户提交请假单"/>
<userTask activiti:assignee="lisi" activiti:exclusive="true" id="_4" name="经理审批"/>
<userTask activiti:assignee="wangwu" activiti:exclusive="true" id="_5" name="总经理审批"/>
<userTask activiti:assignee="zhaoliu" activiti:exclusive="true" id="_6" name="人事审批"/>
<endEvent id="_7" name="流程结束"/>
<sequenceFlow id="_8" sourceRef="_2" targetRef="_3"/>
<sequenceFlow id="_9" sourceRef="_3" targetRef="_4"/>
<sequenceFlow id="_10" sourceRef="_4" targetRef="_5"/>
<sequenceFlow id="_11" sourceRef="_5" targetRef="_6"/>
<sequenceFlow id="_12" sourceRef="_6" targetRef="_7"/>
</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="hoilday">
<bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">
<omgdc:Bounds height="32.0" width="32.0" x="130.0" y="-15.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="105.0" y="75.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="55.0" width="85.0" x="100.0" y="190.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_5" id="Shape-_5">
<omgdc:Bounds height="55.0" width="85.0" x="100.0" y="295.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_6" id="Shape-_6">
<omgdc:Bounds height="55.0" width="85.0" x="100.0" y="400.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_7" id="Shape-_7">
<omgdc:Bounds height="32.0" width="32.0" x="125.0" y="520.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="_12" id="BPMNEdge__12" sourceElement="_6" targetElement="_7">
<omgdi:waypoint x="141.0" y="455.0"/>
<omgdi:waypoint x="141.0" y="520.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_8" id="BPMNEdge__8" sourceElement="_2" targetElement="_3">
<omgdi:waypoint x="146.0" y="17.0"/>
<omgdi:waypoint x="146.0" y="75.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9" sourceElement="_3" targetElement="_4">
<omgdi:waypoint x="145.0" y="130.0"/>
<omgdi:waypoint x="145.0" y="190.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_11" id="BPMNEdge__11" sourceElement="_5" targetElement="_6">
<omgdi:waypoint x="142.5" y="350.0"/>
<omgdi:waypoint x="142.5" y="400.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10" sourceElement="_4" targetElement="_5">
<omgdi:waypoint x="142.5" y="245.0"/>
<omgdi:waypoint x="142.5" y="295.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>
3. .png图片文件
3.1 将hoilday.bpmn文件改为扩展名xml的文件名称:hoilday.xml
3.2 在holiday.xm文件上,点击右键并选择Diagrams菜单,在选择Show BPMN2.0 Designer
3.3 打开后,点击Export To File的小图片;
3.4 弹出小窗口,注意填写文件名以及扩展名,选择好保存图片的位置;
3.5 如果完成后,出现乱码的问题,解决如下:
在idea64.exe.vmoptions文件的最后一行追加一条命令: -Dfile.encoding=UTF-8
注意:不要有空格,否则重启idea时会打不开;完毕后,重启idea;
4.流程定义部署
4.1 什么是流程定义部署
将线下定义的流程部署到activiti数据库中,这就是流程定义部署,通过调用activiti的api将流程定义的bpmn和png两个文件一个一个添加部署到activiti中,也可以将两个文件打成ZIP包进行部署;
4.2 单个文件部署方式
分别将bpmn文件和png图片文件部署;
package com.wn;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
/*流程部署
* `act_ge_bytearray` 流程定义的资源信息,包含bpmn和png流程文件信息
* `act_re_deployment` 流程部署信息 包含流程名称,ID,key等
* `act_re_procdef` 流程定义信息
* */
public class ActivitiTest {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService对象进行流程部署
RepositoryService repositoryService = processEngine.getRepositoryService();
//进行部署,将对应的流程定义文件生成到数据库当中,作为记录进行保存
Deployment deployment= repositoryService.createDeployment()
.addClasspathResource("bpmnfile/hoilday.bpmn") //加载流程文件
.addClasspathResource("bpmnfile/hoilday.png")
.name("请假流程") //设置流程名称
.key("hoildayKey")
.deploy();//部署
//输出部署信息
System.out.println("流程名称:"+deployment.getName());
System.out.println("流程ID:"+deployment.getId());
System.out.println("流程key:"+deployment.getKey());
}
}
4.3 压缩包部署方式
将bpmn文件和png图片压缩成zip包;
package com.wn.activiti;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;
import java.io.*;
import java.util.zip.ZipInputStream;
/*压缩文件方式部署流程*/
public class DeploymentByZip {
@Test
public void deploymentByZip(){
//获取processEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取一个RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//定义一个输入流,加载Zip文件
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("bpmnfile/hoilday.zip");
//定义一个ZIPInputStream对象
ZipInputStream zipInputStream=new ZipInputStream(inputStream);
//流程部署
Deployment deploy = repositoryService.createDeployment()
.addZipInputStream(zipInputStream)
.name("请求审批流程")
.key("holidayKey")
.deploy();
System.out.println("流程名称:"+deploy.getName());
}
}
4.4 流程定义查询
查询部署的流程定义;
package com.wn.activiti;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import java.util.List;
/*流程定义信息的查看*/
public class GetProDef {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取一个RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//得到流程定义查看对象ProcessDefinitionQuery
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//查看流程
List<ProcessDefinition> hoilday = processDefinitionQuery.processDefinitionKey("hoilday").orderByProcessDefinitionKey().desc().list();
for (ProcessDefinition proDef:hoilday){
System.out.println("流程定义ID:"+proDef.getId());
System.out.println("流程定义名称:"+proDef.getName());
System.out.println("流程定义的KEY:"+proDef.getKey());
System.out.println("流程定义的版本号:"+proDef.getVersion());
}
}
}
4.5 流程定义删除
删除已经部署成功的流程定义;
package com.wn.activiti;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
public class DeleteDeployment {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取一个RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//普通删除//repositoryService.deleteDeployment("1");
//级联删除流程
repositoryService.deleteDeployment("1",true);
}
}
说明:
1.使用RepositoryService删除流程定义;
2.如果流程定义下没有正在运行的流程,则可以普通删除;
3.如果将流程定义下存在已经运行的流程,使用普通删除报错,可用级联删除方法将流程及相关级联全部删除;
5.从数据库中读取资源文件到本地
package com.wn.activiti;
import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import java.io.*;
/*从数据库中读取资源文件到本地*/
public class GetResource {
@Test
public void getResouce() throws IOException {
//获取processEngine对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取一个RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//获取到流程查询对象
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//获取到流程资源
processDefinitionQuery.processDefinitionKey("hoilday");
//获取单一结果
ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
//流程部署的ID
String deploymentId = processDefinition.getDeploymentId();
//获取到bpmnResource
InputStream bpmnStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
//获取到png
InputStream pngStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
//构建输出流
OutputStream bpmnOut = new FileOutputStream("E:\北大青鸟\Y2\Activiti\" + processDefinition.getResourceName());
OutputStream pngOut = new FileOutputStream("E:\北大青鸟\Y2\Activiti\" + processDefinition.getDiagramResourceName());
//将数据输出到磁盘当中
IOUtils.copy(bpmnStream, bpmnOut);
IOUtils.copy(pngStream, pngOut);
//关流
pngOut.close();
bpmnOut.close();
pngStream.close();
bpmnStream.close();
}
}
6.流程历史信息的查看
即使流程定义已经删除了,流程执行的历史信息通过前面的分析,依然保存在activiti的act_hi_*相关的表中;
package com.wn;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import java.util.List;
/*查看历史任务*/
public class GetHistory {
public static void main(String[] args) {
//获取ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取HistoryService对象查看历史信息
HistoryService historyService = processEngine.getHistoryService();
//获取历史任务
HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
//获取指定流程的实例任务
historicActivityInstanceQuery.processInstanceId("2501");
//获取任务了列表
List<HistoricActivityInstance> list = historicActivityInstanceQuery.list();
for (HistoricActivityInstance ai:list){
System.out.println("任务执行ID:"+ai.getActivityId());
System.out.println("任务执行名称:"+ai.getActivityName());
System.out.println("任务流程实例ID"+ai.getProcessDefinitionId());
System.out.println("任务流程实例ID"+ai.getProcessInstanceId());
System.out.println("============================");
}
}
}