Flowable查询历史任务
效果图
历史任务主要是为了统计使用
代码部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * 查询历史任务 */ @Test public void test007(){ ProcessEngine engine = cfg.buildProcessEngine(); HistoryService service = engine.getHistoryService(); List<HistoricActivityInstance> list = service.createHistoricActivityInstanceQuery() .processDefinitionId( "holidayRequest:3:30003" ) .finished() .list(); for (HistoricActivityInstance instance : list) { System.out.println( "instance.getActivityId() = " + instance.getActivityId()); System.out.println( "instance.getAssignee() = " + instance.getAssignee()); System.out.println( "instance.getActivityName() = " + instance.getActivityName()); System.out.println( "instance.getDurationInMillis() = " + instance.getDurationInMillis()); } } |
pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?xml version= "1.0" encoding= "UTF-8" ?> <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>com.java</groupId> <artifactId>flowable -service</artifactId> <packaging>pom</packaging> <version> 1.0 -SNAPSHOT</version> <modules> <module>flowable-test</module> </modules> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.flowable</groupId> <artifactId>flowable-engine</artifactId> <version> 6.7 . 2 </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> <!--mysql驱动 5.6 . 17 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version> 5.1 . 6 </version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.13 . 2 </version> <scope>test</scope> </dependency> </dependencies> </project> |
流程文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?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" />--> <userTask id= "approveTask" name= "Approve or reject request" flowable:assignee= "${employee}" /> <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" />--> <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> |
完整测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | package com.java; import org.flowable.engine.*; import org.flowable.engine.history.HistoricActivityInstance; import org.flowable.engine.history.HistoricActivityInstanceQuery; 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.task.api.Task; import org.junit.Before; import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Description: * @Author: 喵星人 * @Create: 2024/3/11 9:50 */ public class HolidayRequest { ProcessEngineConfiguration cfg= null ; @Before public void processTest(){ cfg = new StandaloneProcessEngineConfiguration() .setJdbcUrl( "jdbc:mysql://127.0.0.1:3307/public_workflow?useUnicode=true&characterEncoding=utf-8&useSSL=false" ) .setJdbcUsername( "root" ) .setJdbcPassword( "qweiop1992" ) .setJdbcDriver( "com.mysql.jdbc.Driver" ) //表结构不存在就创建 .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); } /** * 部署流程 */ @Test public void test001(){ /** * 获取对象 */ ProcessEngine processEngine = cfg.buildProcessEngine(); RepositoryService service = processEngine.getRepositoryService(); /** * 完成流程部署 */ Deployment deploy = service.createDeployment() .addClasspathResource( "holiday-request.bpmn20.xml" ) .deploy(); System.out.println( "deploy.getId() = " + deploy.getId()); System.out.println( "deploy.getName() = " + deploy.getName()); } /** * 查询流程定义的信息 */ @Test public void test002(){ ProcessEngine processEngine = cfg.buildProcessEngine(); RepositoryService service = processEngine.getRepositoryService(); ProcessDefinition definition = service.createProcessDefinitionQuery() .deploymentId( "1" ) .singleResult(); System.out.println( "definition.getDeploymentId() = " + definition.getDeploymentId()); System.out.println( "definition.getName() = " + definition.getName()); System.out.println( "definition.getDescription() = " + definition.getDescription()); } /** * 删除定义流程 */ @Test public void test003(){ ProcessEngine processEngine = cfg.buildProcessEngine(); RepositoryService service = processEngine.getRepositoryService(); //service.deleteDeployment("2501"); service.deleteDeployment( "2501" , true ); } /** * 启动流程实例 */ @Test public void test004() { ProcessEngine processEngine = cfg.buildProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); /** * 构建流程变量 */ Map<String,Object> variable= new HashMap<String, Object>( 16 ); variable.put( "employee" , "李欣" ); variable.put( "nrOfHolidays" , 3 ); variable.put( "description" , "世界那么大,我想去浪浪..." ); /** * 启动流程实例 */ ProcessInstance request = runtimeService.startProcessInstanceByKey( "holidayRequest" , variable); System.out.println( "request.getProcessDefinitionId() = " + request.getProcessDefinitionId()); System.out.println( "request.getActivityId() = " + request.getActivityId()); System.out.println( "request.getId() = " + request.getId()); } /** * 任务查询 */ @Test public void test005(){ ProcessEngine processEngine = cfg.buildProcessEngine(); TaskService taskService = processEngine.getTaskService(); List<Task> tasks = taskService.createTaskQuery() .processDefinitionKey( "holidayRequest" ) .taskAssignee( "李欣" ) .list(); for (Task task : tasks) { 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 test006(){ ProcessEngine engine = cfg.buildProcessEngine(); TaskService taskService = engine.getTaskService(); Task task = taskService.createTaskQuery() .processDefinitionKey( "holidayRequest" ) .taskAssignee( "李欣" ) .singleResult(); Map<String,Object> variable= new HashMap<String, Object>( 16 ); variable.put( "approved" , false ); taskService.complete(task.getId(),variable); } /** * 查询历史任务 */ @Test public void test007(){ ProcessEngine engine = cfg.buildProcessEngine(); HistoryService service = engine.getHistoryService(); List<HistoricActivityInstance> list = service.createHistoricActivityInstanceQuery() .processDefinitionId( "holidayRequest:3:30003" ) .finished() .list(); for (HistoricActivityInstance instance : list) { System.out.println( "instance.getActivityId() = " + instance.getActivityId()); System.out.println( "instance.getAssignee() = " + instance.getAssignee()); System.out.println( "instance.getActivityName() = " + instance.getActivityName()); System.out.println( "instance.getDurationInMillis() = " + instance.getDurationInMillis()); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2023-03-11 更换yum源地址
2023-03-11 jvm参数介绍以及参数优化