创建一个普通Maven项目
一、依赖、配置
pom.xml
<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.example</groupId> <artifactId>clowable_0811</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>clowable_0811</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- flowable--> <dependency> <groupId>org.flowable</groupId> <artifactId>flowable-engine</artifactId> <version>6.8.0</version> </dependency> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <!-- 测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <!-- 日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.36</version> </dependency> </dependencies> </project>
log4j.properties
log4j.rootLogger=DEBUG,CA log4j.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
二、定义的流程
<?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="同意或拒绝请假" flowable:assignee="zhangsan"/> <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"/> <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>
三、流程操作
SendRejectionMail.java
package org.flowable; import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.delegate.JavaDelegate; public class SendRejectionMail implements JavaDelegate { /** * flowable触发器 * * @param execution */ @Override public void execute(DelegateExecution execution) { System.out.println("拒绝了任务"); } }
package org.minos; import junit.framework.TestCase; import junit.framework.TestSuite; import org.flowable.bpmn.model.BpmnModel; import org.flowable.engine.*; 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.Execution; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.image.ProcessDiagramGenerator; import org.flowable.task.api.Task; import org.junit.Before; import org.junit.Test; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Unit test for simple App. */ public class AppTest { private ProcessEngineConfiguration configuration; /** * 获取流程引擎对象 */ @Test public void testProcessEngine() { //获取ProcessEngineConfiguration对象 ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration(); //配置相关数据库的连接信息 configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver"); configuration.setJdbcUsername("root"); configuration.setJdbcPassword("123456"); configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?serverTimezone=UTC"); //如果书库中的表结构不存在就新建 configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); //通过ProcessEngineConfiguration构建ProcessEngine对象 ProcessEngine processEngine = configuration.buildProcessEngine(); } ProcessEngineConfiguration configurati = null; @Before public void before() { //获取ProcessEngineConfiguration对象 configuration = new StandaloneProcessEngineConfiguration(); //配置相关数据库的连接信息 configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver"); configuration.setJdbcUsername("root"); configuration.setJdbcPassword("123456"); configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?serverTimezone=UTC"); configuration.setActivityFontName("宋体"); configuration.setLabelFontName("宋体"); configuration.setAnnotationFontName("宋体"); //如果书库中的表结构不存在就新建 configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); } /** * 流程部署 */ @Test public void testDeploy() { //1.获取ProcessEngine对象 ProcessEngine processEngine = configuration.buildProcessEngine(); //2、获取RepositoryService RepositoryService repositoryService = processEngine.getRepositoryService(); //3.完成流程部署操作 Deployment deployed = repositoryService.createDeployment() .addClasspathResource("demo.bpmn20.xml") .name("请假流程") .deploy(); System.out.println("deployed.getId()=" + deployed.getId()); System.out.println("deployed.getName()=" + deployed.getName()); } /** * 查询流程定义信息 */ @Test public void testDeployQuery() { ProcessEngine processEngine = configuration.buildProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() .deploymentId("1") .singleResult(); System.out.println("processDefinition.getId() = " + processDefinition.getId()); System.out.println("processDefinition.getName() = " + processDefinition.getName()); System.out.println("processDefinition.getDescription() = " + processDefinition.getDescription()); } /** * 删除部署流程 */ @Test public void testDeleteDeploy() { ProcessEngine processEngine = configuration.buildProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); //删除部署ID为1的流程,如果已经启动,删除失败 //repositoryService.deleteDeployment("1"); //删除部署ID为1的流程,如果已经启动,启动实例也删除 repositoryService.deleteDeployment("2501", true); } /** * 启动流程实例 */ @Test public void testRunProcess() { ProcessEngine processEngine = configuration.buildProcessEngine(); //获取RunTimeService RuntimeService runtimeService = processEngine.getRuntimeService(); //构建流程变量 Map<String, Object> variables = new HashMap<String, Object>(); variables.put("employee", "zhangsan"); variables.put("nrOfHolidays", 3); variables.put("desciption", "work hard"); //启动流程实例 ProcessInstance holidayRequest = runtimeService.startProcessInstanceByKey("holidayRequest", variables); System.out.println("holidayRequest.getProcessDefinitionId() = " + holidayRequest.getProcessDefinitionId()); System.out.println("holidayRequest.getActivityId() = " + holidayRequest.getActivityId()); System.out.println("holidayRequest.getId() = " + holidayRequest.getId()); } /** * 查询任务 */ @Test public void testQueryProcess() { ProcessEngine processEngine = configuration.buildProcessEngine(); TaskService taskService = processEngine.getTaskService(); List<Task> list = taskService.createTaskQuery() .processDefinitionKey("holidayRequest")//指定查询的流程key .taskAssignee("zhangsan")//查询任务的处理人 .list(); for (Task task : list) { System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId()); System.out.println("task.getName() = " + task.getName()); System.out.println("task.getAssignee() = " + task.getAssignee()); System.out.println("task.getDescription() = " + task.getDescription()); System.out.println("task.getId() = " + task.getId()); } } /** * 完成当前任务 */ @Test public void testCompleteTask() { ProcessEngine processEngine = configuration.buildProcessEngine(); TaskService taskService = processEngine.getTaskService(); List<Task> list = taskService.createTaskQuery() .processDefinitionKey("holidayRequest") .taskAssignee("zhangsan") .list(); //创建流程变量 Map<String, Object> variables = new HashMap<String, Object>(); variables.put("approved", false); for (Task task : list) { taskService.complete(task.getId(), variables); } } /** * 获取流程任务的历史数据 */ @Test public void testHistroy() { ProcessEngine processEngine = configuration.buildProcessEngine(); HistoryService historyService = processEngine.getHistoryService(); List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery() .processDefinitionId("holidayRequest:1:7503") .finished() //查询的历史记录的状态已完成 .orderByHistoricActivityInstanceEndTime().asc() //指定排序字段和顺序 .list(); for (HistoricActivityInstance item : list) { System.out.print("item.getActivityName() = " + item.getActivityName()); System.out.print("item.getAssignee() = " + item.getAssignee()); System.out.print("item.getActivityId() = " + item.getActivityId()); System.out.println("item.getDurationInMillis() = " + item.getDurationInMillis() + "毫秒"); } } }