本文主要是介绍flowable 6.1.2 命令行完成请假审批流程的例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、创建 eclipse maven新项目
1、设置项目选项
其中,Create a simple project 要选中。
2、填写项目包名和项目名称
这里的Group id:必须是 org.flowable (后面的流程配置文件需要用到这个包路径)
二、添加项目maven依赖
<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>org.flowable</groupId><artifactId>HolidayRequest</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.flowable</groupId><artifactId>flowable-engine</artifactId><version>6.1.2</version></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>1.3.176</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.21</version></dependency></dependencies>
</project>
三、创建Java运行入口
package org.flowable;public class HolidayRequest {public static void main(String[] args) {}}
四、创建流程配置文件
src/main/resources/holiday-request.bpmn20.xml
<?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: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:flowable="http://flowable.org/bpmn"typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"targetNamespace="http://www.flowable.org/processdef"><process id="holidayRequest" name="Holiday Request"isExecutable="true"><startEvent id="startEvent" /><sequenceFlow sourceRef="startEvent" targetRef="approveTask" /><userTask id="approveTask" name="Approve or reject request" flowable:candidateGroups="managers"/><sequenceFlow sourceRef="approveTask" targetRef="decision" /><exclusiveGateway id="decision" /><sequenceFlow sourceRef="decision" targetRef="externalSystemCall"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${approved}]]></conditionExpression></sequenceFlow><sequenceFlow sourceRef="decision" targetRef="sendRejectionMail"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${!approved}]]></conditionExpression></sequenceFlow><serviceTask id="externalSystemCall" name="Enter holidays in external system"flowable:class="org.flowable.CallExternalSystemDelegate" /><sequenceFlow sourceRef="externalSystemCall" targetRef="holidayApprovedTask" /><userTask id="holidayApprovedTask" name="Holiday approved" flowable:assignee="${employee}"/><sequenceFlow sourceRef="holidayApprovedTask" targetRef="approveEnd" /><serviceTask id="sendRejectionMail" name="Send out rejection email"flowable:class="org.flowable.SendRejectionMail" /><sequenceFlow sourceRef="sendRejectionMail" targetRef="rejectEnd" /><endEvent id="approveEnd" /><endEvent id="rejectEnd" /></process></definitions>
五、创建log配置文件
src/main/resources/log4j.properties
log4j.rootLogger=DEBUG, CAlog4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n
六、创建Java代理类
这个类在前面的流程配置中被设置为id = externalSystemCall 的 serviceTask 的执行类。
package org.flowable;import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;public class CallExternalSystemDelegate implements JavaDelegate {public void execute(DelegateExecution execution) {System.out.println("Calling the external system for employee "+ execution.getVariable("employee"));}}
七、完成代码逻辑
package holidy;import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;import org.flowable.engine.HistoryService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.task.Task;public class HolidayRequest {public static void main(String[] args) {// 创建流程引擎相关数据库表ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration().setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1").setJdbcUsername("sa").setJdbcPassword("").setJdbcDriver("org.h2.Driver").setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);ProcessEngine processEngine = cfg.buildProcessEngine();// 设计好的流程部署到流程引擎中(数据库)RepositoryService repositoryService = processEngine.getRepositoryService();Deployment deployment = repositoryService.createDeployment().addClasspathResource("holiday-request.bpmn20.xml").deploy();// 根据流程ID查找已经部署的流程ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();System.out.println(" 发现定义的流程 : " + processDefinition.getName());// 模拟请假流程,输入相关的参数Scanner scanner = new Scanner(System.in);// 请假人System.out.println("你是谁?");String employee = scanner.nextLine();// 请假天数System.out.println("你要请几天假?");Integer nrOfHolidays = Integer.valueOf(scanner.nextLine());// 请假理由System.out.println("请假理由?");String description = scanner.nextLine();// 流程引擎服务RuntimeService runtimeService = processEngine.getRuntimeService();Map<String, Object> variables = new HashMap<String, Object>();variables.put("employee", employee);variables.put("nrOfHolidays", nrOfHolidays);variables.put("description", description);// 构建流程运行参数,启动流程ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);// 获取待办任务列表TaskService taskService = processEngine.getTaskService();List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();System.out.println("你有 " + tasks.size() + " 个待办任务:");for (int i = 0; i < tasks.size(); i++) {System.out.println((i + 1) + ") " + tasks.get(i).getName());}// 任务选择System.out.println("请选择你要处理的任务ID?");int taskIndex = Integer.valueOf(scanner.nextLine());Task task = tasks.get(taskIndex - 1);Map<String, Object> processVariables = taskService.getVariables(task.getId());// 流程审批System.out.println(processVariables.get("employee") + " 要请 " + processVariables.get("nrOfHolidays")+ " 天假. 是否同意? 同意回复 y ");boolean approved = scanner.nextLine().toLowerCase().equals("y");variables = new HashMap<String, Object>();variables.put("approved", approved);taskService.complete(task.getId(), variables);// 审批历史数据HistoryService historyService = processEngine.getHistoryService();List<HistoricActivityInstance> activities = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstance.getId()).finished().orderByHistoricActivityInstanceEndTime().asc().list();for (HistoricActivityInstance activity : activities) {System.out.println(activity.getActivityId() + " took " + activity.getDurationInMillis() + " milliseconds");}}}
这篇关于flowable 6.1.2 命令行完成请假审批流程的例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!