Flowable API
Jmix BPM 使用了 Flowable API,为 Spring Boot 应用程序与进程引擎之间的交互提供了便利。
Flowable API
Flowable API 支持以编程的方式与不同的服务进行交互。
Flowable FormService 和 IdentityService 没有在 Jmix BPM 项目中使用。 Jmix BPM 提供了 BpmTaskService,继承自 Flowable 的 TaskService。请参考 后面的示例。
|
有两种方式获取 Flowable 服务:
-
使用
ProcessEngines:ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); RepositoryService repositoryService = processEngine.getRepositoryService(); TaskService taskService = processEngine.getTaskService(); -
在 bean 中直接注入服务,因为服务也是作为 Spring bean 注册的:
@Component("sample_MyCustomBean") public class MyCustomBean { @Autowired private RuntimeService runtimeService;
示例
编程式启动流程
下面示例中,流程在常规实体编辑界面中使用 RuntimeService 编程式启动:
@Autowired
private RuntimeService runtimeService;
@Subscribe("commitAndCloseBtn")
public void onCommitAndCloseBtnClick(Button.ClickEvent event) {
Customer customer = getEditedEntity();
String name = customer.getName();
Map<String, Object> params = new HashMap<>();
params.put("customer", customer); (1)
params.put("name", name); (2)
runtimeService.startProcessInstanceByKey( (3)
"new-customer", (4)
params); (5)
}
| 1 | 将编辑实体作为流程变量传入,使用 customer 变量名。 |
| 2 | 将客户的姓名作为字符串类型的流程变量。 |
| 3 | RuntimeService 用于启动流程。 |
| 4 | new-customer 是流程定义键值。 |
| 5 | 传入流程变量。 |
获取用户任务列表
下面示例展示如何获取当前认证用户的激活任务列表:
@Autowired
private TaskService taskService;
@Autowired
private CurrentAuthentication currentAuthentication;
public List<Task> getCurrentUserTasks() {
return taskService.createTaskQuery() (1)
.processDefinitionKey("approval") (2)
.taskAssignee(currentAuthentication.getUser().getUsername()) (3)
.active()
.orderByTaskCreateTime()
.desc()
.list();
}
| 1 | 使用 TaskService 获取任务列表。 |
| 2 | 搜索 approval 流程的任务。 |
| 3 | 搜索分配给当前用户的任务。 |
获取流程实例列表
下面示例展示如何获取与指定 Order 实体相关的 approval 流程实例列表:
@Autowired
private RuntimeService runtimeService;
public List<ProcessInstance> getActiveProcessInstances() {
return runtimeService.createProcessInstanceQuery() (1)
.processDefinitionKey("approval") (2)
.variableValueEquals("orderId", "N-1") (3)
.active()
.list();
}
| 1 | 使用 RuntimeService 获取任务列表。 |
| 2 | 搜索 approval 流程的实例。 |
| 3 | 通过指定的 orderId 流程变量搜索流程实例。 |
使用 BpmTaskService
BpmTaskService 继承自 TaskService 并添加了一个方法,可以完成带有输出的任务:
void completeTaskWithOutcome(String taskId, String outcomeId, Map<String, Object> processVariables);
可以在 Spring 组件中注入该服务:
@Autowired
private BpmTaskService bpmTaskService;
或者使用 ProcessEngines 类:
BpmTaskService bpmTaskService = (BpmTaskService) ProcessEngines
.getDefaultProcessEngine()
.getTaskService();
通过 API 处理流程定义
Flowable 提供了处理流程定义的丰富 API:
-
部署:流程定义通常作为部署单元的一部分进行部署,该部署单元可以包含多个流程定义和相关资源。
Deployment deployment = repositoryService.createDeployment() .addClasspathResource("my-process.bpmn20.xml") .deploy(); -
查询:
RepositoryService支持使用各种条件查询流程定义:ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() .processDefinitionId("myProcessDefinitionId") .singleResult(); -
挂起和激活:可以挂起或激活流程定义,从而控制是否可以根据流程定义启动新的流程实例:
filename.javarepositoryService.suspendProcessDefinitionById("myProcessDefinitionId"); -
生成流程图:Flowable 可以为部署的流程定义生成流程图:
filename.javaInputStream diagramStream = repositoryService.getProcessDiagram(processDefinition.getId());
本页是否有帮助?
感谢您的反馈