SpringBoot入门十四,添加Activiti5.22工作流
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot入门十四,添加Activiti5.22工作流相关的知识,希望对你有一定的参考价值。
项目基本配置参考SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可。现在来给项目添加一个Activiti5.22支持,Springboot采用2.0.7的版本(后来试着升级到2.1.1和2.2.1,都是失败),具体内容如下:
- pom.xml添加以下配置信息
<!-- 1.设置Springboot版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath />
</parent>
<!-- 2.设置公共参数 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- Maven install 时,测试环境@Test中如果有中文输出是乱码,加上这句话试试 -->
<argLine>-Dfile.encoding=UTF-8</argLine>
<!-- 设置要整合的Activiti版本 -->
<activiti.version>5.22.0</activiti.version>
</properties>
<!-- 3.引入Activiti支持 -->
<dependencies>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>${activiti.version}</version>
</dependency>
</dependencies>
- 配置文件中添加以下配置信息
# 设置系统启动时自动建立Activiti表(Springboot1.x的版本中不需要做此设置)
# always为始终执行初始化,embedded只初始化内存数据库(默认值),如h2等,never为不执行初始化.
spring.datasource.initialization-mode=always
# Activiti配置
## 自动构建Activiti数据库表,设置value="true",启动时检测,无表创建,有表跳过;设置value="drop-create"
## 每次启动时建新表(Springboot1.x中生效,Springboot2.x中已经不生效,因此关闭)
#spring.activiti.database-schema-update=true
## Activiti自动部署验证设置,:true-开启(默认)、false-关闭
spring.activiti.check-process-definitions=false
## 设置true后将代替那些老的Job executor
spring.activiti.async-executor-enabled=false
## Activiti定时扫描任务,默认:true(开启),不使用的话建议关掉,需要和spring.activiti.async-executor-enabled=false配合使用,否则不起作用
spring.activiti.job-executor-activate=false
3.创建流程文件
将创建好的文件放置在src/main/resources目录下
4.创建一个测试service
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ActivitiTestService {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
/**
* @功能描述: 流程测试
*
* @param flowType
*/
public void test(String flowType){
//部署流程,只要是符合BPMN2规范的XML文件,理论上都可以被ACTIVITI部署
Deployment deployment = repositoryService.createDeployment().name("测试流程一").addClasspathResource("testProcess.bpmn").addClasspathResource("testProcess.png").deploy();
//获取流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//启动流程定义,返回流程实例
ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId());
String processId = pi.getId();
System.out.println("流程创建成功,当前流程实例ID:"+processId);
Task task=taskService.createTaskQuery().processInstanceId(processId).singleResult();
System.out.println("第一次执行前,任务名称:"+task.getName());
taskService.complete(task.getId());
int count = 2;
while(true){
task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
if (null == task) {
System.out.println("task为null,任务执行完毕:"+task);
break;
} else {
System.out.println("第"+count+++"次执行前,任务名称:"+task.getName());
taskService.complete(task.getId());
}
}
}
}
5.创建一个测试controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qfx.common.bean.MessageBean;
import com.qfx.common.controller.BaseController;
import com.qfx.workflow.service.ActivitiTestService;
import com.qfx.workflow.service.ModelService;
@Controller
@RequestMapping("activiti")
public class ActivitiController extends BaseController {
@Autowired
ActivitiTestService activitiService;
@Autowired
ModelService modelService;
@RequestMapping("test")
public String test(String flowType){
// 流程测试
activitiService.test(flowType);
MessageBean messageBean = new MessageBean();
messageBean.setMessage("测试流程执行完毕了,请看后台输出!");
request.setAttribute("messageBean", messageBean);
return "../index";
}
}
6.测试调用,控制台显示调用成功
以上是关于SpringBoot入门十四,添加Activiti5.22工作流的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot从入门到精通(三十四)如何集成JWT实现Token验证
springboot整合activiti报错[processes/]不存在解决方案