Spring Boot 项目集成 Camunda 流程引擎学习记录
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WorkflowService {
private final ProcessEngine processEngine;
private final RepositoryService repositoryService;
private final RuntimeService runtimeService;
@Autowired
public WorkflowService(ProcessEngine processEngine) {
this.processEngine = processEngine;
this.repositoryService = processEngine.getRepositoryService();
this.runtimeService = processEngine.getRuntimeService();
}
public List<String> getDeployedProcessDefinitions() {
return repositoryService.createProcessDefinitionQuery().orderByProcessDefinitionName().asc().list()
.stream().map(pd -> pd.getKey() + " : " + pd.getName()).toList();
}
public void startProcessInstance(String processDefinitionKey) {
ProcessInstance pi = runtimeService.startProcessInstanceByKey(processDefinitionKey);
System.out.println("Process instance started. ID: " + pi.getId());
}
}
这段代码演示了如何在Spring Boot项目中集成Camunda工作流程引擎。首先,通过@Autowired
注解注入ProcessEngine
,然后通过ProcessEngine
获取RepositoryService
和RuntimeService
。getDeployedProcessDefinitions
方法用于获取所有已部署的流程定义,并以流的方式映射键和名称。startProcessInstance
方法用于根据流程定义的键启动一个新的流程实例。
评论已关闭