Flowable 6.3.0 从安装到实战:手把手教你搭建第一个BPMN流程(附MySQL 8.0避坑指南)
Flowable 6.3.0实战指南从零构建企业级流程引擎当企业业务流程复杂度超过CRUD范畴时一套可靠的流程引擎就成为技术架构中的关键基础设施。作为Activiti原班团队打造的新一代开源BPM引擎Flowable 6.3.0在保持轻量级特性的同时提供了更完善的BPMN 2.0规范支持和更优的性能表现。本文将带您从环境搭建到完整流程开发体验如何用Java构建一个可落地的请假审批系统。1. 开发环境准备与避坑指南在开始Flowable之旅前需要特别注意Java 8和MySQL 8.0的版本适配问题。许多开发者首次接触Flowable时90%的安装问题都源于数据库配置不当。基础依赖配置dependencies !-- Flowable核心引擎 -- dependency groupIdorg.flowable/groupId artifactIdflowable-engine/artifactId version6.3.0/version /dependency !-- MySQL连接器注意8.0版本差异 -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.28/version /dependency !-- 日志框架 -- dependency groupIdorg.slf4j/groupId artifactIdslf4j-api/artifactId version1.7.36/version /dependency dependency groupIdch.qos.logback/groupId artifactIdlogback-classic/artifactId version1.2.11/version /dependency /dependenciesMySQL 8.0特殊配置ProcessEngineConfiguration cfg new StandaloneProcessEngineConfiguration() .setJdbcUrl(jdbc:mysql://localhost:3306/flowable_db?useSSLfalseserverTimezoneAsia/ShanghainullCatalogMeansCurrenttrue) .setJdbcUsername(your_username) .setJdbcPassword(your_password) .setJdbcDriver(com.mysql.cj.jdbc.Driver) .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);关键参数说明nullCatalogMeansCurrenttrue解决MySQL 8.0元数据查询异常serverTimezone避免时区问题导致的时间戳错误。初始化成功后引擎会自动创建约60张表主要分为以下几类表前缀功能描述示例表名ACT_RE流程定义存储ACT_RE_DEPLOYMENTACT_RU运行时流程实例数据ACT_RU_TASKACT_HI历史流程数据ACT_HI_PROCINSTACT_GE通用数据及资源存储ACT_GE_BYTEARRAY2. BPMN 2.0流程设计与部署现代流程引擎的核心在于对BPMN 2.0标准的支持。我们以请假审批为例设计包含以下节点的流程开始事件流程触发起点用户任务经理审批环节排他网关根据审批结果分流服务任务对接外部系统如HR系统结束事件流程终止节点完整的holiday-request.bpmn20.xml?xml version1.0 encodingUTF-8? definitions xmlnshttp://www.omg.org/spec/BPMN/20100524/MODEL process idholidayRequest name请假审批流程 startEvent idstartEvent/ userTask idmanagerApproval name经理审批 flowable:assignee${approver}/ exclusiveGateway iddecisionGateway/ sequenceFlow sourceRefstartEvent targetRefmanagerApproval/ sequenceFlow sourceRefmanagerApproval targetRefdecisionGateway/ sequenceFlow sourceRefdecisionGateway targetRefnotifyHR conditionExpression xsi:typetFormalExpression ![CDATA[${approved true}]] /conditionExpression /sequenceFlow serviceTask idnotifyHR name通知HR系统 flowable:classcom.example.HRSystemDelegate/ sequenceFlow sourceRefdecisionGateway targetRefsendRejection conditionExpression xsi:typetFormalExpression ![CDATA[${approved false}]] /conditionExpression /sequenceFlow serviceTask idsendRejection name发送拒绝通知 flowable:classcom.example.RejectionNotifier/ endEvent idendApproved/ endEvent idendRejected/ sequenceFlow sourceRefnotifyHR targetRefendApproved/ sequenceFlow sourceRefsendRejection targetRefendRejected/ /process /definitions部署流程到引擎的Java代码示例RepositoryService repositoryService processEngine.getRepositoryService(); Deployment deployment repositoryService.createDeployment() .addClasspathResource(processes/holiday-request.bpmn20.xml) .name(请假审批流程部署) .deploy(); System.out.println(部署ID: deployment.getId());3. 流程实例运行与任务处理启动一个流程实例需要提供初始变量RuntimeService runtimeService processEngine.getRuntimeService(); MapString, Object variables new HashMap(); variables.put(employee, 张三); variables.put(days, 3); variables.put(reason, 年度休假); variables.put(approver, 李四); // 指定审批人 ProcessInstance instance runtimeService.startProcessInstanceByKey( holidayRequest, variables);审批人查询待办任务TaskService taskService processEngine.getTaskService(); ListTask tasks taskService.createTaskQuery() .taskAssignee(李四) .processDefinitionKey(holidayRequest) .list(); tasks.forEach(task - { System.out.println(任务ID: task.getId()); System.out.println(任务名称: task.getName()); });处理审批任务时更新流程变量taskService.complete(task.getId(), Collections.singletonMap(approved, true));4. 高级功能与性能优化异步执行器配置# 在flowable.cfg.xml中配置 property nameasyncExecutorActivate valuetrue/ property nameasyncExecutorNumberOfRetries value3/ property nameasyncExecutorThreadPoolQueueSize value100/历史数据归档策略ProcessEngineConfiguration cfg new StandaloneProcessEngineConfiguration() .setHistoryLevel(HistoryLevel.FULL) .setEnableDatabaseEventLogging(true) .setHistoryCleaningTimeCycleConfig(0 0 1 * * ?); // 每天1点执行性能监控指标ManagementService managementService processEngine.getManagementService(); MapString, Long metrics managementService.getDatabaseMetrics() .getMetrics(); System.out.println(活动实例数: metrics.get(ACTIVE)); System.out.println(已完成实例数: metrics.get(COMPLETED));在实际项目中我们通常会结合Spring Boot进行集成。以下是一个典型的配置片段Configuration public class FlowableConfig { Bean public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource) { SpringProcessEngineConfiguration config new SpringProcessEngineConfiguration(); config.setDataSource(dataSource); config.setTransactionManager(transactionManager); config.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); config.setAsyncExecutorActivate(true); return config; } }对于需要高并发的场景建议使用RuntimeService.createChangeActivityStateBuilder()进行批量操作对频繁查询的表添加适当索引考虑使用Flowable的分布式执行器模式
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2450058.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!