Flowable初体验

news/2025/1/25 2:49:30/

创建一个普通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*/@Overridepublic 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;/*** 获取流程引擎对象*/@Testpublic 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;@Beforepublic 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);}/*** 流程部署*/@Testpublic void testDeploy() {//1.获取ProcessEngine对象ProcessEngine processEngine = configuration.buildProcessEngine();//2、获取RepositoryServiceRepositoryService 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());}/*** 查询流程定义信息*/@Testpublic 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());}/*** 删除部署流程*/@Testpublic void testDeleteDeploy() {ProcessEngine processEngine = configuration.buildProcessEngine();RepositoryService repositoryService = processEngine.getRepositoryService();//删除部署ID为1的流程,如果已经启动,删除失败//repositoryService.deleteDeployment("1");//删除部署ID为1的流程,如果已经启动,启动实例也删除repositoryService.deleteDeployment("2501", true);}/*** 启动流程实例*/@Testpublic void testRunProcess() {ProcessEngine processEngine = configuration.buildProcessEngine();//获取RunTimeServiceRuntimeService 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());}/*** 查询任务*/@Testpublic 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());}}/*** 完成当前任务*/@Testpublic 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);}}/*** 获取流程任务的历史数据*/@Testpublic 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() + "毫秒");}}}

文章来源:https://blog.csdn.net/qq_29752857/article/details/132270442
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ppmy.cn/news/1030538.html

相关文章

windows配置git公钥,读写远程git项目

首先Windows电脑需要下载并安装git&#xff1a; 从官网直接下载然后安装即可&#xff1a;https://git-scm.com/download/win 添加公钥 ssh-keygen -t rsa -C "xxxxxxx.com"注意&#xff1a;这个xxxxxxx.com与github注册的邮箱一致 然后一路回车&#xff0c;生成完…

Mysql 建索引规范

索引规范 今天在建线上表的时候&#xff0c;做了一个varchar的索引&#xff0c;运维说varchar的索引会占用很大的内存。 于是 上网搜了一下建索引规范 一、建表规约 【强制】&#xff08;1&#xff09; 存储引擎必须使用InnoDB 解读&#xff1a;InnoDB支持事物、行级锁、并发…

ssm+JSP的乡镇自来水收费系统源码和论文PPT

ssmJSP的乡镇自来水收费系统源码和论文PPT014 开发工具&#xff1a;idea 数据库mysql5.7(mysql5.7最佳) 数据库链接工具&#xff1a;navcat,小海豚等 开发技术&#xff1a;java ssm tomcat8.5 一、课题背景与意义 随着我国经济建设迅速发展&#xff0c;乡镇规模日益扩大&am…

统计学补充概念01-基本高斯模拟

概念 基本高斯模拟是指使用高斯分布&#xff08;也称为正态分布&#xff09;来模拟随机变量的分布或生成随机样本的过程。高斯分布在统计学和概率论中具有重要作用&#xff0c;它是连续概率分布中最常见的一种&#xff0c;具有钟形曲线的特点。 基本高斯模拟的一般步骤&#…

visual studio 2022安装本地的nuget包

工具->选项->Nuget包管理器->程序包源 然后右击解决方案->管理解决方案的Nuget包

CRM 系统控制实施风险

全面了解 CRM 系统的优势和特点。CRM 系统应有两种优势&#xff1a;产品功能和租用模 式。从产品功能来说&#xff1a; CRM 系统设计理念应结合企业的具体情况&#xff0c;提出一整套符 合企业需求的 CRM 设计理念&#xff0c;并且在 CRM 系统上付诸实现&#xff1b;从服务模式…

web会话跟踪以及JWT响应拦截机制

目录 JWT 会话跟踪 token 响应拦截器 http是无状态的&#xff0c;登录成功后&#xff0c;客户端就与服务器断开连接&#xff0c;之后再向后端发送请求时&#xff0c;后端需要知道前端是哪个用户在进行操作。 JWT Json web token (JWT), 是为了在网络应用环境间传递声明而…

题目:2558.从数量最多的堆中取走礼物

​题目来源&#xff1a; leetcode题目&#xff0c;网址&#xff1a;2558. 从数量最多的堆取走礼物 - 力扣&#xff08;LeetCode&#xff09; 解题思路&#xff1a; 按要求模拟即可。 解题代码&#xff1a; class Solution {public long pickGifts(int[] gifts, int k) {long…