前言
Activiti项目是一项新的基于Apache许可的开源BPM平台,从基础开始构建,旨在提供支持新的BPMN 2.0标准,包括支持对象管理组(OMG),面对新技术的机遇,诸如互操作性和云架构,提供技术实现。
创始人Tom Baeyens是JBoss jBPM的项目架构师,以及另一位架构师Joram Barrez,一起加入到创建 Alfresco这项首次实现Apache开源许可的BPMN 2.0引擎开发中来。
Activiti是一个独立运作和经营的开源项目品牌,并将独立于Alfresco开源ECM系统运行。 Activiti将是一种轻量级,可嵌入的BPM引擎,而且还设计适用于可扩展的云架构。 Activiti将提供宽松的Apache许可2.0,以便这个项目可以广泛被使用,同时促进Activiti BPM引擎和BPMN 2.0的匹配,该项目现正由OMG通过标准审定。 加入Alfresco Activiti项目的是VMware的SpringSource分支,Alfresco的计划把该项目提交给Apache基础架构,希望吸引更多方面的BPM专家和促进BPM的创新。
代码实现
- 流程部署、查询流程定义
- 启动流程、查询流程
- 待办任务、完成任务
- 已结束流程、已完成任务
xml
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.1.0.M6</version>
</dependency>
yaml
# 服务配置
server:
port: 8088
# spring配置
spring:
# 数据源配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/boot_activiti?useUnicode=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF8&nullCatalogMeansCurrent=true
username: root
password: root
# activiti7配置
activiti:
# 建表策略,可选值:true,false,create-drop,drop-create
database-schema-update: true
# 自动部署检查,默认校验resources下的processes文件夹里的流程文件
check-process-definitions: false
# 保存历史数据等级,可选值:none,activity,audit,full
history-level: full
# 检测历史表是否存在
db-history-used: true
java
package com.qiangesoft.activiti.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public UserDetailsService userDetailsService() {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("admin")
.password(encoder.encode("123456"))
.roles("USER")
.build());
return manager;
}
}
java
package com.qiangesoft.activiti.controller;
import com.qiangesoft.activiti.constant.ProcessConstant;
import com.qiangesoft.activiti.constant.R;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 流程定义 控制器
*
* @author lq
* @date 2024-02-27
*/
@RestController
@RequestMapping("/definition")
public class ProcessDefinitionController {
@Autowired
private RepositoryService repositoryService;
@GetMapping("/deploy")
public R deploy() {
// 部署流程
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmn/test.bpmn20.xml")
.deploy();
return R.ok(deployment);
}
@GetMapping("/list")
public R list() {
// 查询部署流程
List<Deployment> deploymentList = repositoryService.createDeploymentQuery()
.deploymentKey(ProcessConstant.PROCESS_KEY)
// .deploymentName("")
.orderByDeploymenTime().desc()
.list();
return R.ok(deploymentList);
}
@GetMapping("/get")
public R get() {
Deployment deployment = repositoryService.createDeploymentQuery()
.deploymentKey(ProcessConstant.PROCESS_KEY)
// .deploymentName("")
.latest()
.singleResult();
return R.ok(deployment);
}
}
java
package com.qiangesoft.activiti.controller;
import com.qiangesoft.activiti.constant.ProcessConstant;
import com.qiangesoft.activiti.constant.R;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 流程实例 控制器
*
* @author lq
* @date 2024-02-27
*/
@RestController
@RequestMapping("/instance")
public class ProcessInstanceController {
@Autowired
private RuntimeService runtimeService;
@GetMapping("/start")
public R start() {
// 启动流程:提供流程key,业务key,主题
String businessId = "101";
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
.processDefinitionKey(ProcessConstant.PROCESS_KEY)
.businessKey(ProcessConstant.BUSINESS_KEY_PREFIX + businessId)
.name("请假流程")
.start();
return R.ok(processInstance.toString());
}
@GetMapping("/list")
public R list() {
// 查询进行中的流程实例
List<ProcessInstance> processInstanceList = runtimeService.createProcessInstanceQuery()
.processDefinitionKey(ProcessConstant.PROCESS_KEY)
// .deploymentId("")
// .processInstanceId("")
// .processInstanceBusinessKey("")
// .processInstanceNameLike("请假流程")
.active()
.orderByProcessInstanceId().desc()
.list();
return R.ok(processInstanceList.toString());
}
@GetMapping("/get")
public R get(String instanceId) {
// 某个实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(instanceId)
.singleResult();
return R.ok(processInstance == null ? null : processInstance.toString());
}
}
java
package com.qiangesoft.activiti.controller;
import com.qiangesoft.activiti.constant.R;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 流程待办 控制器
*
* @author lq
* @date 2024-02-27
*/
@RestController
@RequestMapping("/task")
public class TaskController {
@Autowired
private TaskService taskService;
@GetMapping("/mine")
public R mine() {
// 某人的待办任务:按照时间倒序
String userId = "zhangsan";
List<Task> taskList = taskService.createTaskQuery()
// .processDefinitionKey("")
// .taskCandidateOrAssigned("")
.taskAssignee(userId)
.active()
.orderByTaskCreateTime().desc()
.list();
return R.ok(taskList.toString());
}
@GetMapping("/handle")
public R handle(String taskId) {
// 完成任务
taskService.complete(taskId);
return R.ok();
}
}
java
package com.qiangesoft.activiti.controller;
import com.qiangesoft.activiti.constant.ProcessConstant;
import com.qiangesoft.activiti.constant.R;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 历史 控制器
*
* @author lq
* @date 2024-02-27
*/
@RestController
@RequestMapping("/history")
public class HistoryController {
@Autowired
private HistoryService historyService;
@GetMapping("/finished")
public R finished() {
// 已结束的流程:按照结束时间倒序
List<HistoricProcessInstance> processInstanceList = historyService.createHistoricProcessInstanceQuery()
// .processInstanceId("")
// .deploymentId("")
// .processDefinitionName("")
// .processInstanceBusinessKey("")
.processDefinitionKey(ProcessConstant.PROCESS_KEY)
.finished()
.orderByProcessInstanceEndTime().desc()
.list();
return R.ok(processInstanceList.toString());
}
@GetMapping("/completed")
public R completed() {
// 某人已完成的任务:按照完成时间倒序
String userId = "zhangsan";
List<HistoricTaskInstance> processInstanceList = historyService.createHistoricTaskInstanceQuery()
// .processInstanceId("")
// .deploymentId("")
// .processDefinitionName("")
// .processInstanceBusinessKey("")
.taskAssignee(userId)
.processDefinitionKey(ProcessConstant.PROCESS_KEY)
.finished()
.orderByHistoricTaskInstanceEndTime().desc()
.list();
return R.ok(processInstanceList.toString());
}
}