本实例是activiti的入门实例,讲述如何来配置activiti,以及定义第一个属于你自己的activiti流程。该流程定义了一个开始事件、一个结束事件和两个普通的用户任务。
流程图如下:
环境搭建:
IDE为eclipase
activiti5.14(下载地址:https://github.com/Activiti/Activiti/releases/download/activiti-5.14/activiti-5.14.zip)
环境搭建后如下图:
所需依赖包:
activiti配置文件(activiti.cfg.xml):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <!-- 配置jdbc访问数据库的url --> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti" /> <!-- 配置jdbc数据库驱动类 --> <property name="jdbcDriver" value="com.mysql.jdbc.Driver" /> <!-- 配置数据库用户名 --> <property name="jdbcUsername" value="root" /> <!-- 配置数据库密码 --> <property name="jdbcPassword" value="aaaaaa" /> <!-- 配置启动JobExecutor,使用定时器时需要 --> <property name="jobExecutorActivate" value="true" /> <!-- 配置当前数据库类型,支持:oracle、mysql等 --> <property name="databaseType" value="mysql"/> <!-- 配置activiti创建数据库的模式 --> <property name="databaseSchemaUpdate" value="false" /> <!-- 配置activiti保存历史数据的级别 --> <property name="history" value="full"/> </bean> </beans>
流程文件内容如下(activiti_test01.bpmn20.xml):
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="https://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="https://activiti.org/bpmn" xmlns:bpmndi="https://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="https://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="https://www.omg.org/spec/DD/20100524/DI" typeLanguage="https://www.w3.org/2001/XMLSchema" expressionLanguage="https://www.w3.org/1999/XPath" targetNamespace="https://www.activiti.org/processdef"> <process id="activiti_test01" name="activiti_test01" isExecutable="true"> <endEvent id="end" name="结束"></endEvent> <startEvent id="start" name="开始"></startEvent> <userTask id="task01" name="任务1"></userTask> <userTask id="task02" name="任务2"></userTask> <sequenceFlow id="sid-0BD9BF34-F3E5-4739-BC31-3D8E6625AD06" sourceRef="start" targetRef="task01"></sequenceFlow> <sequenceFlow id="sid-3BAB9DF2-1381-44E4-9551-E8052BB693FE" sourceRef="task01" targetRef="task02"></sequenceFlow> <sequenceFlow id="sid-B148A91B-4D70-4B51-BF56-C779E92C573A" sourceRef="task02" targetRef="end"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_activiti_test01"> <bpmndi:BPMNPlane bpmnElement="activiti_test01" id="BPMNPlane_activiti_test01"> <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end"> <omgdc:Bounds height="28.0" width="28.0" x="510.0" y="194.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start"> <omgdc:Bounds height="30.0" width="30.0" x="90.0" y="193.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="task01" id="BPMNShape_task01"> <omgdc:Bounds height="80.0" width="100.0" x="180.0" y="168.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="task02" id="BPMNShape_task02"> <omgdc:Bounds height="80.0" width="100.0" x="345.0" y="168.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="sid-3BAB9DF2-1381-44E4-9551-E8052BB693FE" id="BPMNEdge_sid-3BAB9DF2-1381-44E4-9551-E8052BB693FE"> <omgdi:waypoint x="280.0" y="208.0"></omgdi:waypoint> <omgdi:waypoint x="345.0" y="208.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-B148A91B-4D70-4B51-BF56-C779E92C573A" id="BPMNEdge_sid-B148A91B-4D70-4B51-BF56-C779E92C573A"> <omgdi:waypoint x="445.0" y="208.0"></omgdi:waypoint> <omgdi:waypoint x="510.0" y="208.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-0BD9BF34-F3E5-4739-BC31-3D8E6625AD06" id="BPMNEdge_sid-0BD9BF34-F3E5-4739-BC31-3D8E6625AD06"> <omgdi:waypoint x="120.0" y="208.0"></omgdi:waypoint> <omgdi:waypoint x="180.0" y="208.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
java客户端代码(ActivitiTest01.java):
package com.bug315; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; public class ActivitiTest01 { public static void main(String[] args) { // 创建流程引擎 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); // 获取流程存储服务组件 RepositoryService repositoryService = processEngine.getRepositoryService(); // 获取运行时服务组件 RuntimeService runtimeService = processEngine.getRuntimeService(); // 获取流程任务组件 TaskService taskService = processEngine.getTaskService(); // 1、部署流程文件 repositoryService.createDeployment() .addClasspathResource("bpmn/activiti_test01.bpmn20.xml").deploy(); // 2、启动流程 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("activiti_test01"); System.out.println("流程实例ID=" + processInstance.getId()); // 3、查询第一个任务 Task task = taskService.createTaskQuery().singleResult(); System.out.println("任务ID=" + task.getId() + " 任务名称=" + task.getName()); taskService.complete(task.getId()); // 完成任务 // 4、查询第二个任务 task = taskService.createTaskQuery().singleResult(); System.out.println("任务ID=" + task.getId() + " 任务名称=" + task.getName()); taskService.complete(task.getId()); // 完成任务 // 5、流程结束 task = taskService.createTaskQuery().singleResult(); System.out.println("任务结束(" + task + ")"); } }
运行结果:
流程实例ID=1705
任务ID=1708 任务名称=任务1
任务ID=1710 任务名称=任务2
任务结束(null)