在 Flowable 7.x 中,ProcessEngine
和 RepositoryService
是核心组件之一,负责管理流程引擎和流程定义。由于 Flowable 7.x 进行了重构,ProcessEngine
不再自动注入为 Spring Bean,需要手动配置或通过服务进行访问。下面我将详细介绍这两个组件,并展示如何在 Flowable 7.x 中正确使用它们。
1. ProcessEngine
ProcessEngine
是 Flowable 中的核心引擎组件,它负责执行流程定义、管理流程实例、任务和历史记录等。
创建 ProcessEngine
在 Flowable 7.x 中,需要通过 ProcessEngineConfiguration
来创建 ProcessEngine
,并且可以通过 Spring Bean 进行配置。
配置 ProcessEngine
:
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FlowableConfig {
@Bean
public ProcessEngine processEngine() {
return ProcessEngineConfiguration
.createStandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://localhost:3306/flowable_demo?serverTimezone=UTC&useSSL=false")
.setJdbcUsername("root")
.setJdbcPassword("rootpassword")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
.buildProcessEngine();
}
}
2. RepositoryService
RepositoryService
负责与流程定义相关的操作,例如部署、查询流程定义等。它是 Flowable 引擎的一部分,并通过 ProcessEngine
提供。
使用 RepositoryService
你可以通过 ProcessEngine
获取 RepositoryService
,然后进行流程定义的部署和查询。
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FlowableService {
private final RepositoryService repositoryService;
@Autowired
public FlowableService(ProcessEngine processEngine) {
this.repositoryService = processEngine.getRepositoryService();
}
// 部署流程定义
public Deployment deployProcess(String processFile) {
return repositoryService.createDeployment()
.addClasspathResource(processFile) // 添加 BPMN 文件
.name("Spring Boot 流程部署")
.deploy();
}
// 查询流程定义
public long countDeployments() {
return repositoryService.createDeploymentQuery().count();
}
}
3. 结合 Spring 使用 ProcessEngine
和 RepositoryService
为了在 Spring Boot 项目中更好地使用 ProcessEngine
和 RepositoryService
,你可以通过依赖注入来使用它们。下面是一个完整的控制器示例,展示了如何在 Spring Boot 中部署和查询流程。
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FlowableController {
private final RepositoryService repositoryService;
private final RuntimeService runtimeService;
@Autowired
public FlowableController(RepositoryService repositoryService, RuntimeService runtimeService) {
this.repositoryService = repositoryService;
this.runtimeService = runtimeService;
}
// 部署流程定义
@GetMapping("/deploy")
public String deployProcess() {
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("processes/01/flow1.bpmn20.xml") // 你的 BPMN 文件
.name("Spring 流程部署")
.deploy();
return "流程部署成功,部署ID:" + deployment.getId();
}
// 启动流程实例
@GetMapping("/startProcess")
public String startProcess() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
return "流程启动成功,实例ID:" + processInstance.getId();
}
}
4. 主要组件和它们的作用
ProcessEngine
-
ProcessEngine
是 Flowable 中的核心引擎对象。它负责流程引擎的启动和管理,包含了与数据库的连接和配置。 - 可以通过
ProcessEngineConfiguration
来创建ProcessEngine
,并通过它获取各种服务,比如RepositoryService
、RuntimeService
等。
RepositoryService
-
RepositoryService
是 Flowable 的服务之一,主要用于与流程定义(BPMN 文件)相关的操作。 - 主要功能包括:
- 部署流程定义:通过
createDeployment()
方法部署 BPMN 文件。 - 查询流程定义:可以使用
createDeploymentQuery()
方法查询已经部署的流程定义。
RuntimeService
-
RuntimeService
是 Flowable 的服务之一,主要用于启动和管理流程实例。 - 主要功能包括:
- 启动流程实例:使用
startProcessInstanceByKey()
方法启动一个流程实例。 - 查询流程实例:可以查询正在运行的流程实例。
5. 注意事项
- 数据库表:Flowable 需要一组数据库表来存储流程数据,确保在应用启动时 Flowable 能够自动创建这些表(如果数据库为空),或者手动执行 Flowable 提供的 SQL 脚本来创建表。
- 事务管理:Flowable 中的操作通常是事务性的,Spring 会自动管理这些事务,但你也可以根据需要进行自定义事务配置。
-
BPMN 文件:确保你的 BPMN 文件位于
src/main/resources
目录下,并且路径正确。
总结
-
ProcessEngine
是 Flowable 引擎的核心对象,负责启动和管理整个流程引擎。 -
RepositoryService
主要用于与流程定义(BPMN 文件)相关的操作,例如部署和查询流程定义。 - 可以通过 Spring 的依赖注入来获取
ProcessEngine
和RepositoryService
,从而实现流程定义的部署和流程实例的管理。