下面是详细讲解SpringBoot整合Activiti7的实现代码的完整攻略。
什么是Activiti7
Activiti7是一个轻量级的工作流引擎,它提供了一套流程定义、流程实例、任务管理等服务,可以用来设计和实现复杂的业务流程。
如何在SpringBoot中整合Activiti7
步骤一:添加依赖
在SpringBoot项目的pom.xml文件中添加Activiti7的依赖:
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>7.0.0.M6</version>
</dependency>
步骤二:配置数据库连接
在SpringBoot项目的配置文件(application.yml或application.properties)中添加数据库连接配置:
spring:
datasource:
url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
步骤三:配置Activiti7引擎
在SpringBoot项目的配置类中配置Activiti7引擎:
@Configuration
public class ActivitiConfig {
@Bean
public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource) {
SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
configuration.setDataSource(dataSource);
configuration.setTransactionManager(new DataSourceTransactionManager(dataSource));
configuration.setDatabaseSchemaUpdate("true");
return configuration;
}
@Bean
public ProcessEngine processEngine(ProcessEngineConfiguration processEngineConfiguration) {
return processEngineConfiguration.buildProcessEngine();
}
@Bean
public RepositoryService repositoryService(ProcessEngine processEngine) {
return processEngine.getRepositoryService();
}
@Bean
public RuntimeService runtimeService(ProcessEngine processEngine) {
return processEngine.getRuntimeService();
}
@Bean
public TaskService taskService(ProcessEngine processEngine) {
return processEngine.getTaskService();
}
@Bean
public HistoryService historyService(ProcessEngine processEngine) {
return processEngine.getHistoryService();
}
@Bean
public ManagementService managementService(ProcessEngine processEngine) {
return processEngine.getManagementService();
}
}
步骤四:定义流程
在classpath:/processes目录下创建一个简单的工作流程定义文件(helloworld.bpmn20.xml),内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" 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" targetNamespace="http://www.activiti.org/test">
<process id="helloworld" name="Hello World Process" isExecutable="true">
<startEvent id="start" activiti:initiator="initiator"></startEvent>
<userTask id="task1" name="User Task" activiti:candidateUsers="kermit"></userTask>
<endEvent id="end"></endEvent>
<sequenceFlow id="flow1" sourceRef="start" targetRef="task1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="task1" targetRef="end"></sequenceFlow>
</process>
</definitions>
步骤五:发布流程和启动流程实例
在SpringBoot的控制器中编写发布流程和启动流程实例的代码:
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@PostMapping("/publish")
public String publish(@RequestBody String xml) {
Deployment deployment = repositoryService.createDeployment()
.addString("helloworld.bpmn20.xml", xml).deploy();
return "Deployment ID: " + deployment.getId();
}
@PostMapping("/start")
public String start(@RequestParam String processDefinitionKey, @RequestParam String initiator) {
ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, initiator);
return "Process ID: " + instance.getId();
}
}
访问http://localhost:8080/activiti/publish接口发布流程文件,访问http://localhost:8080/activiti/start接口启动流程实例。
Activiti7整合SpringBoot示例
以下是两个Activiti7整合SpringBoot的具体示例:
示例一:将流程图转成图片并返回给前端
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
@Autowired
private RepositoryService repositoryService;
@Autowired
private ProcessEngine processEngine;
@PostMapping("/publish")
public String publish(@RequestBody String xml) {
Deployment deployment = repositoryService.createDeployment()
.addString("helloworld.bpmn20.xml", xml).deploy();
return "Deployment ID: " + deployment.getId();
}
@GetMapping("/image")
public void getImage(@RequestParam String processDefinitionId, HttpServletResponse response)
throws IOException {
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
ProcessDiagramGenerator diagramGenerator = processEngine.getProcessEngineConfiguration()
.getProcessDiagramGenerator();
InputStream inputStream = diagramGenerator.generateDiagram(bpmnModel,
"png", Collections.emptyList(), Collections.emptyList(), "宋体", "微软雅黑",
"黑体", null, 1.0);
response.setContentType("image/png");
IOUtils.copy(inputStream, response.getOutputStream());
}
}
示例二:查询用户任务
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
@Autowired
private TaskService taskService;
@PostMapping("/publish")
public String publish(@RequestBody String xml) {
Deployment deployment = repositoryService.createDeployment()
.addString("helloworld.bpmn20.xml", xml).deploy();
return "Deployment ID: " + deployment.getId();
}
@PostMapping("/start")
public String start(@RequestParam String processDefinitionKey, @RequestParam String initiator) {
ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, initiator);
return "Process ID: " + instance.getId();
}
@GetMapping("/tasks")
public List<Task> getTasks(@RequestParam String candidateUser) {
return taskService.createTaskQuery().taskCandidateUser(candidateUser).list();
}
}
访问http://localhost:8080/activiti/tasks接口查询候选用户的任务。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot整合Activiti7的实现代码 - Python技术站