1 // VacationRequest.java 2 3 /** 4 * author : 馮孟活 ^_^ 5 * dates : 2015年9月1日 下午10:32:58 6 * class : 演示簡單的公司請假流程 7 * 8 * 一個簡單的流程分三個步驟: 9 * 1、發布流程(部署流程定義) 10 * 2、啟動流程實例 11 * 3、完成任務(先查詢任務,后完成任務) 12 * 4、掛起、激活一個流程實例(可選) 13 */ 14 public class VacationRequest { 15 public static void main(String[] args) { 16 17 /** 18 * 第一步:發布流程 19 */ 20 ProcessEngine processEngine = ProcessEngineConfiguration // 通過流程引擎配置類來創建流程引擎 21 .createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine(); 22 RepositoryService repositoryService = processEngine.getRepositoryService(); // 通過流程引擎來得到知識庫服務 23 repositoryService.createDeployment().addClasspathResource("VacationRequest.bpmn").deploy(); // 通過只是庫部署流程定義 24 System.out.println("流程定義的個數="+repositoryService.createDeploymentQuery().count()); // 查詢所有發布的流程定義的個數 25 26 /** 27 * 第二步:啟動一個流程實例 28 */ 29 /*定義Map來存放流程變量:流程變量經常會被用到,因為他們賦予來自同一個流程定義的不同流程實例 30 的特別含義,簡單來說,流程變量是區分流程實例的關鍵 31 */ 32 Map<String, Object> variables = new HashMap<>(); // 定義一個Map來存放流程變量 33 variables.put("employeeName","Kermit"); 34 variables.put("numberOfDays",new Integer(4)); 35 variables.put("vacationMotivation","I'm really tired!"); 36 RuntimeService runtimeService = processEngine.getRuntimeService(); // 獲取運行服務 37 runtimeService.startProcessInstanceByKey("vacationRequest",variables); // 通過運行服務來啟動流程實例,並且設置流程變量(通過key 或者 id 部署都可以) 38 System.out.println("流程實例的個數="+runtimeService.createProcessInstanceQuery().count()); // 通過運行服務來查詢所有的流程實例的個數 39 40 /** 41 * 第三部:完成任務 42 */ 43 TaskService taskService = processEngine.getTaskService(); // 通過流程引擎獲取任務服務 44 List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("management").list(); // 通過任務服務來查詢任務候選組(這是通過組來分區) 45 for (Task task : tasks) { // 遍歷打印任務 46 System.err.println("能找到的任務="+task.getName()); 47 } 48 Task task = tasks.get(0); // 獲取第一個任務 49 Map<String,Object> taskVariables = new HashMap<>(); // 定義一個Map來存放任務變量 50 taskVariables.put("vacationApproved","false"); 51 taskVariables.put("managerMotivation","We have a tight deadline!"); 52 taskService.complete(task.getId(),taskVariables); // 根據Id來完成任務 53 54 /** 55 * 掛起,激活一個流程 56 */ 57 /* 58 * 我們可以掛起一個流程定義。當掛起流程定義時, 就不能創建新流程了(會拋出一個異常)。 59 * 可以通過RepositoryService掛起一個流程: 60 */ 61 //repositoryService.suspendProcessDefinitionByKey("vacationRequest"); // 掛起一個流程定義 62 //try{ 63 //runtimeService.startProcessInstanceByKey("vacationRequest"); // 啟動一個流程實例 64 //}catch(ActivitiException e){ // 這里會拋出一個Activiti自定義異常 65 //e.printStackTrace(); 66 //} 67 68 /* 69 * 備注下: 70 * 也可以掛起一個流程實例。掛起時,流程不能繼續執行(比如,完成任務會拋出異常), 71 * 異步操作(比如定時器)也不會執行。 掛起流程實例可以調用 runtimeService.suspendProcessInstance方法。 72 * 激活流程實例可以調用runtimeService.activateProcessInstanceXXX方法。 73 */ 74 } 75 }
1 <!-- activiti.cfg.xml --> 2 3 <?xml version="1.0" encoding="UTF-8"?> 4 <beans xmlns="http://www.springframework.org/schema/beans" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd"> 8 9 <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> 10 <property name="databaseSchemaUpdate" value="update"/> 11 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_activiti?useUnicode=true&characterEncoding=utf-8"/> 12 <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/> 13 <property name="jdbcUsername" value="root"/> 14 <property name="jdbcPassword" value="root"/> 15 <property name="jobExecutorActivate" value="true"/> 16 </bean> 17 18 </beans>
1 <!-- VacationRequest.bpmn --> 2 <?xml version="1.0" encoding="UTF-8" ?> 3 <definitions id="definitions" 4 targetNamespace="http://activiti.org/bpmn20" 5 xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xmlns:activiti="http://activiti.org/bpmn"> 8 9 <process id="vacationRequest" name="Vacation request"> 10 11 <startEvent id="request" activiti:initiator="employeeName"> 12 <extensionElements> 13 <activiti:formProperty id="numberOfDays" name="Number of days" type="long" value="1" required="true"/> 14 <activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" datePattern="dd-MM-yyyy hh:mm" type="date" required="true" /> 15 <activiti:formProperty id="vacationMotivation" name="Motivation" type="string" /> 16 </extensionElements> 17 </startEvent> 18 <sequenceFlow id="flow1" sourceRef="request" targetRef="handleRequest" /> 19 20 <userTask id="handleRequest" name="Handle vacation request" > 21 <documentation> 22 ${employeeName} would like to take ${numberOfDays} day(s) of vacation (Motivation: ${vacationMotivation}). 23 </documentation> 24 <extensionElements> 25 <activiti:formProperty id="vacationApproved" name="Do you approve this vacation" type="enum" required="true"> 26 <activiti:value id="true" name="Approve" /> 27 <activiti:value id="false" name="Reject" /> 28 </activiti:formProperty> 29 <activiti:formProperty id="managerMotivation" name="Motivation" type="string" /> 30 </extensionElements> 31 <potentialOwner> 32 <resourceAssignmentExpression> 33 <formalExpression>management</formalExpression> 34 </resourceAssignmentExpression> 35 </potentialOwner> 36 </userTask> 37 <sequenceFlow id="flow2" sourceRef="handleRequest" targetRef="requestApprovedDecision" /> 38 39 <exclusiveGateway id="requestApprovedDecision" name="Request approved?" /> 40 <sequenceFlow id="flow3" sourceRef="requestApprovedDecision" targetRef="sendApprovalMail"> 41 <conditionExpression xsi:type="tFormalExpression">${vacationApproved == 'true'}</conditionExpression> 42 </sequenceFlow> 43 44 <task id="sendApprovalMail" name="Send confirmation e-mail" /> 45 <sequenceFlow id="flow4" sourceRef="sendApprovalMail" targetRef="theEnd1" /> 46 <endEvent id="theEnd1" /> 47 48 <sequenceFlow id="flow5" sourceRef="requestApprovedDecision" targetRef="adjustVacationRequestTask"> 49 <conditionExpression xsi:type="tFormalExpression">${vacationApproved == 'false'}</conditionExpression> 50 </sequenceFlow> 51 52 <userTask id="adjustVacationRequestTask" name="Adjust vacation request"> 53 <documentation> 54 Your manager has disapproved your vacation request for ${numberOfDays} days. 55 Reason: ${managerMotivation} 56 </documentation> 57 <extensionElements> 58 <activiti:formProperty id="numberOfDays" name="Number of days" value="${numberOfDays}" type="long" required="true"/> 59 <activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" value="${startDate}" datePattern="dd-MM-yyyy hh:mm" type="date" required="true" /> 60 <activiti:formProperty id="vacationMotivation" name="Motivation" value="${vacationMotivation}" type="string" /> 61 <activiti:formProperty id="resendRequest" name="Resend vacation request to manager?" type="enum" required="true"> 62 <activiti:value id="true" name="Yes" /> 63 <activiti:value id="false" name="No" /> 64 </activiti:formProperty> 65 </extensionElements> 66 <humanPerformer> 67 <resourceAssignmentExpression> 68 <formalExpression>${employeeName}</formalExpression> 69 </resourceAssignmentExpression> 70 </humanPerformer> 71 </userTask> 72 <sequenceFlow id="flow6" sourceRef="adjustVacationRequestTask" targetRef="resendRequestDecision" /> 73 74 <exclusiveGateway id="resendRequestDecision" name="Resend request?" /> 75 <sequenceFlow id="flow7" sourceRef="resendRequestDecision" targetRef="handleRequest"> 76 <conditionExpression xsi:type="tFormalExpression">${resendRequest == 'true'}</conditionExpression> 77 </sequenceFlow> 78 79 <sequenceFlow id="flow8" sourceRef="resendRequestDecision" targetRef="theEnd2"> 80 <conditionExpression xsi:type="tFormalExpression">${resendRequest == 'false'}</conditionExpression> 81 </sequenceFlow> 82 <endEvent id="theEnd2" /> 83 84 </process> 85 86 </definitions>