1.1Eclipse新建一個基本的java項目java
1.2項目結構以下mysql
(注:須要jar包太多本身添加)spring
1.3建立配置文件、建立本身的庫sql
<?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 --> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" /> <property name="jdbcDriver" value="com.mysql.jdbc.Driver" /> <property name="jdbcUsername" value="root" /> <property name="jdbcPassword" value="123456" /> <property name="databaseSchemaUpdate" value="true" /><!-- 相似於hibernate的檢測表是否創建 --> </bean> </beans>
1.4建立流程配置文件express
畫好流程後的文件spa
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="myProcess" name="My process" isExecutable="true"> <endEvent id="endevent1" name="End"></endEvent> <startEvent id="startevent1" name="Start"></startEvent> <userTask id="usertask3" name="manager"></userTask> <userTask id="usertask4" name="worker"></userTask> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask4"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="usertask4" targetRef="usertask3"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess"> <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess"> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="630.0" y="171.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="160.0" y="160.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3"> <omgdc:Bounds height="55.0" width="105.0" x="480.0" y="150.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4"> <omgdc:Bounds height="55.0" width="105.0" x="280.0" y="150.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="195.0" y="177.0"></omgdi:waypoint> <omgdi:waypoint x="280.0" y="177.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="385.0" y="177.0"></omgdi:waypoint> <omgdi:waypoint x="480.0" y="177.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="585.0" y="177.0"></omgdi:waypoint> <omgdi:waypoint x="630.0" y="188.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
1.5寫程序hibernate
package org.crazyit.activiti; 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.task.Task; /** * 第一個流程運行類 * @author yangenxiong * */ public class First { public static void main(String[] args) { // 建立流程引擎 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 獲得流程存儲服務組件 RepositoryService repositoryService = engine.getRepositoryService(); // 獲得運行時服務組件 RuntimeService runtimeService = engine.getRuntimeService(); // 獲取流程任務組件 TaskService taskService = engine.getTaskService(); // 部署流程文件 repositoryService.createDeployment() .addClasspathResource("bpmn/First.bpmn").deploy(); // 啓動流程 runtimeService.startProcessInstanceByKey("process1"); // 查詢第一個任務 Task task = taskService.createTaskQuery().singleResult(); System.out.println("第一個任務完成前,當前任務名稱:" + task.getName()); // 完成第一個任務 taskService.complete(task.getId()); // 查詢第二個任務 task = taskService.createTaskQuery().singleResult(); System.out.println("第二個任務完成前,當前任務名稱:" + task.getName()); // 完成第二個任務(流程結束) taskService.complete(task.getId()); task = taskService.createTaskQuery().singleResult(); System.out.println("流程結束後,查找任務:" + task); // 退出 System.exit(0); } }
程序運行後 會自動建表 走一個最簡單的activiti流程code