springboot整合activiti
1.导入依赖
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.0.0.Beta2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
2.yml配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/sakila?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username : root
password : 123456
driver-class-name: com.mysql.jdbc.Driver
activiti:
db-history-used: true
history-level: full
3.生成表
** 4.流程部署 **
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("bpmb/xx.bpmn20.xml")
.name("流程部署测试")
.deploy();
System.out.println("流程部署名称:" + deployment.getName());
5.查看流程部署
public void listDeployments() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
List<Deployment> deployments =repositoryService.createDeploymentQuery().list();
if (!deployments.isEmpty()) {
deployments.forEach(deployment -> {
System.out.println("Id:" + deployment.getId());
System.out.println("Name:" + deployment.getName());
});
}
}
6.启动流程
public void initProcessInstance() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
// 流程定义KEY
String processDefinitionKey = "Part1_Deployment";
// 参数
Map<String, Object> variables = new HashMap<>(16);
variables.put("assignee","admin");
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey(processDefinitionKey, variables);
System.out.println("流程实例ID:" + processInstance.getProcessInstanceId());
}
7.查看代办任务
public void listTasks() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
List<Task> taskList =taskService.createTaskQuery().list();
if (!CollectionUtils.isEmpty(taskList)) {
taskList.forEach(task -> {
System.out.println("Id:" + task.getId());
System.out.println("Name:" + task.getName());
System.out.println("Assignee:" + task.getAssignee());
});
}
}
8.审批任务
@Test
public void completeTask() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
String taskId = "5b4fdda9-4a25-11ee-a5fe-4ac2dd5e2c23";
Map<String, Object> variables = new HashMap<>(16);
variables.put("assignee","admin");
taskService.complete(taskId, variables);
}
9.根据用户名查看历史
public void listHistoricTasksByAssignee() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
HistoryService historyService = processEngine.getHistoryService();
String assignee = "admin";
List<HistoricTaskInstance> historicTasks = historyService.createHistoricTaskInstanceQuery()
.orderByHistoricTaskInstanceEndTime()
.asc()
.taskAssignee(assignee)
.list();
if (!CollectionUtils.isEmpty(historicTasks)) {
historicTasks.forEach(historicTaskInstance -> {
System.out.println("Id:" + historicTaskInstance.getId());
System.out.println("ProcessInstanceId:" + historicTaskInstance.getProcessInstanceId());
System.out.println("Name:" + historicTaskInstance.getName());
});
}
}