springboot集成 activiti工作流代码使用示例
时间: 2023-09-11 22:04:10 浏览: 175
以下是一个简单的 Spring Boot 集成 Activiti 工作流的示例代码:
1. 在 pom.xml 中添加依赖:
```xml
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>${activiti.version}</version>
</dependency>
```
2. 创建一个 Activiti 配置类:
```java
@Configuration
public class ActivitiConfig {
@Bean
public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource, PlatformTransactionManager transactionManager) {
SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
configuration.setDataSource(dataSource);
configuration.setTransactionManager(transactionManager);
configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
configuration.setAsyncExecutorActivate(false);
return configuration;
}
@Bean
public ProcessEngineFactoryBean processEngineFactoryBean(ProcessEngineConfiguration processEngineConfiguration) {
ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
factoryBean.setProcessEngineConfiguration(processEngineConfiguration);
return factoryBean;
}
@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();
}
@Bean
public IdentityService identityService(ProcessEngine processEngine) {
return processEngine.getIdentityService();
}
@Bean
public FormService formService(ProcessEngine processEngine) {
return processEngine.getFormService();
}
}
```
3. 创建一个简单的工作流程:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<definitions id="definitions"
targetNamespace="http://www.activiti.org/test"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
<process id="testProcess" name="Test Process">
<startEvent id="start" name="Start"></startEvent>
<userTask id="task1" name="Task 1" assignee="${user}"></userTask>
<exclusiveGateway id="gateway1"></exclusiveGateway>
<userTask id="task2" name="Task 2" assignee="${user}" />
<endEvent id="end" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="start" targetRef="task1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="task1" targetRef="gateway1"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="gateway1" targetRef="task2">
<conditionExpression xsi:type="tFormalExpression">${approved == true}</conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow4" sourceRef="gateway1" targetRef="end">
<conditionExpression xsi:type="tFormalExpression">${approved == false}</conditionExpression>
</sequenceFlow>
</process>
</definitions>
```
4. 创建一个处理器来启动和完成工作流程:
```java
@Service
public class WorkflowService {
private final TaskService taskService;
private final RuntimeService runtimeService;
@Autowired
public WorkflowService(TaskService taskService, RuntimeService runtimeService) {
this.taskService = taskService;
this.runtimeService = runtimeService;
}
public void startWorkflow(String processDefinitionKey, Map<String, Object> variables) {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
}
public void completeTask(String taskId, Map<String, Object> variables) {
taskService.complete(taskId, variables);
}
}
```
5. 在控制器中使用处理器来启动和完成工作流程:
```java
@RestController
@RequestMapping("/workflow")
public class WorkflowController {
private final WorkflowService workflowService;
@Autowired
public WorkflowController(WorkflowService workflowService) {
this.workflowService = workflowService;
}
@PostMapping("/start")
public void startWorkflow(@RequestParam String processDefinitionKey, @RequestParam String user) {
Map<String, Object> variables = new HashMap<>();
variables.put("user", user);
workflowService.startWorkflow(processDefinitionKey, variables);
}
@PostMapping("/complete")
public void completeTask(@RequestParam String taskId, @RequestParam boolean approved) {
Map<String, Object> variables = new HashMap<>();
variables.put("approved", approved);
workflowService.completeTask(taskId, variables);
}
}
```
上述代码中,我们创建了一个 Activiti 配置类来配置 Activiti 引擎,包括数据库配置、事务管理器等。我们还创建了一个简单的工作流程,其中包括一个开始事件、两个用户任务、一个排他网关和一个结束事件。最后,我们创建了一个处理器来启动和完成工作流程,并在控制器中使用处理器来处理具体的请求。
阅读全文