1.通过以下地址创建基于Maven的SpringBoot Web项目
https://start.spring.io/
2.添加项目的依赖文件
pom.xml文件如下
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3.添加业务流程文件
在src/main/java/resource目录下创建processes目录
创建process.bpmn文件,内容如下
4.配置application.properties文件
配置内容如下
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activiti?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
server.port=8082
server.context-path=/
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8
5.代码实现
a.定义接口
package com.springboot.demo.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/activityConsumerService")
public interface ActivityConsumerService {
@RequestMapping(value = "/startActivityDemo",method = RequestMethod.GET)
public boolean startActivityDemo();
}
b.实现接口
package com.springboot.demo.service.impl;
import com.springboot.demo.service.ActivityConsumerService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName ActivityConsumerServiceImpl
* @Description TODO
* @Author yunshuodeng
* @Date 2019-04-29 13:28
* @Version 1.0
**/
@Service("activityService")
public class ActivityConsumerServiceImpl implements ActivityConsumerService {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private RepositoryService repositoryService;
@Override
public boolean startActivityDemo() {
// 获取当前方法名
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
// 定义任务办理人
String name1 = "zhangsan";
String name2 = "lisi";
System.out.println("method " + methodName + " begin ...");
// 输出流程部署数量
System.out.println("调用流程存储服务,查询部署数量" + repositoryService.createDeploymentQuery().count());
Map
map.put("apply",name1);
map.put("approve",name2);
// 流程启动
ExecutionEntity executionEntity = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave",map);
// 查询name1的任务列表
List
// 输出任务数量
System.out.println(taskList.size());
if (taskList != null && taskList.size() > 0){
for (Task task : taskList){
System.out.println("任务ID:"+task.getId());
System.out.println("任务办理人:"+task.getAssignee());
System.out.println("任务名称:"+task.getName());
System.out.println("任务创建时间:"+task.getCreateTime());
System.out.println("流程实例ID:"+task.getProcessDefinitionId());
System.out.println("-------------------------------------------");
}
}
System.out.println("method " + methodName + " end ...");
return false;
}
}
6.启动服务并通过以下地址进行访问
http://localhost:8082/activityConsumerService/startActivityDemo
7.查看控制台输出内容并不报错表示已整合完成