activiti7+springboot2(六)SpringBoot2整合Activiti7 activiti 7 的springboot插件能⾃动创建数据表,⾃动部署设计好的流程,不需要我们像前⾯那样代码创建数据库和部署流程。
(⼀)⾸先 l ⽂件中引⼊相关的依赖,具体依赖如下所⽰:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>amples</groupId>
<artifactId>activiti-3</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>8</java.version>
<mavenpiler.source>${java.version}</mavenpiler.source>
<mavenpiler.target>${java.version}</mavenpiler.target>
<activiti-dependencies.version>7.1.24</activiti-dependencies.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.activiti.dependencies</groupId>
<artifactId>activiti-dependencies</artifactId>
<version>${activiti-dependencies.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>${java.version}</release>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
<!-- 添加私服仓库地址,否则不到包 -->
<repositories>
<repository>
<id>alfresco</id>
<name>Activiti Releases</name>
<url>artifacts.alfresco/nexus/content/repositories/activiti-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
(⼆)springboot 的配置⽂件 l中添加相关的配置:
spring:
datasource:
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/platform_activiti?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
username: root
minimum-idle: 5
maximum-pool-size: 20
connection-test-query: SELECT 1
activiti:
database-schema-update: true
# 开启历史库
db-history-used: true
history-level: audit
参数说明:
databaseSchemaUpdate配置项可以设置流程引擎启动和关闭时数据库执⾏的策略。 databaseSchemaUpdate有以下四个值:
false:false为默认值,设置为该值后,Activiti在启动时,会对⽐数据库表中保存的版本,如果没有表或者版本不匹配时,将在启动时抛出异常。
true:设置为该值后,Activiti会对数据库中所有的表进⾏更新,如果表不存在,则Activiti会⾃动创建。
create-drop:Activiti启动时,会执⾏数据库表的创建操作,在Activiti关闭时,执⾏数据库表的删除操作。
drop-create:Activiti启动时,执⾏数据库表的删除操作在Activiti关闭时,会执⾏数据库表的创建操作。
history-level对于历史数据,保存到何种粒度,Activiti提供了history-level属性对其进⾏配置。history-level属性有点像log4j的⽇志输出级别,该属性有以下四个值:none:不保存任何的历史数据,因此,在流程执⾏过程中,这是最⾼效的。
activity:级别⾼于none,保存流程实例与流程⾏为,其他数据不保存。
audit:除activity级别会保存的数据外,还会保存全部的流程任务及其属性。audit为history的默认值。
full:保存历史数据的最⾼级别,除了会保存audit级别的数据外,还会保存其他全部流程相关的细节数据,包括⼀些流程参数等。
db-history-used为true表⽰使⽤历史表,如果不配置,则⼯程启动后可以检查数据库,只建⽴了17张表,历史表没有建⽴,则流程图及运⾏节点⽆法展⽰
(三)流程资源
资源⽂件放在  resources/processes ⽬录下,activiti程序启动会⾃动发布流程
(四)编写Applocation主程序
package ample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActivitiApplication {
public static void main(String[] args) {
SpringApplication.run(ActivitiApplication.class, args);
createprocessa}
}
运⾏程序,从控制台输出中可以看到,⾃动创建了数据库,并且发布了"请假申请"的流程
(五)编写基于SpringBootTest的测试程序
package ample;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.task.runtime.TaskRuntime;
import ine.*;
import ine.history.HistoricTaskInstance;
import ine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.context.SpringBootTest;
import st.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiTest {
@Autowired
private RepositoryService repositoryService;
@Autowired
private TaskService taskService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private HistoryService historyService;
/**
@Test
public void startProcessInstance() {
System.out.println("Number of process definitions : "+ ateProcessDefinitionQuery().count());        System.out.println("Number of tasks : " + ateTaskQuery().count());
runtimeService.startProcessInstanceByKey("myProcess_1");
}
/**
* 查询⽤户的任务列表
*/
@Test
public void taskQuery() {
//根据流程定义的key,负责⼈assignee来实现当前⽤户的任务列表查询
List<Task> list = ateTaskQuery()
.processDefinitionKey("myProcess_1")
.taskAssignee("john")
.list();
if(list!=null && list.size()>0){
for(Task task:list){
System.out.println("任务ID:"+Id());
System.out.println("任务名称:"+Name());
System.out.println("任务的创建时间:"+CreateTime());
System.out.println("任务的办理⼈:"+Assignee());
System.out.println("流程实例ID:"+ProcessInstanceId());
System.out.println("执⾏对象ID:"+ExecutionId());
System.out.println("流程定义ID:"+ProcessDefinitionId());
System.out.println("getOwner:"+Owner());
System.out.println("getCategory:"+Category());
System.out.println("getDescription:"+Description());
System.out.println("getFormKey:"+FormKey());
Map<String, Object> map = ProcessVariables();
for (Map.Entry<String, Object> m : Set()) {
System.out.println("key:" + m.getKey() + " value:" + m.getValue());
}
for (Map.Entry<String, Object> m : TaskLocalVariables().entrySet()) {
System.out.println("key:" + m.getKey() + " value:" + m.getValue());
}
}
}
}
/**
* 完成任务
*/
@Test
public void completeTask(){
//任务ID
String taskId = "967465fe-3367-11ea-a057-30b49ec7161f";
taskServiceplete(taskId);
System.out.println("完成任务:任务ID:"+taskId);
}
/
**
* 历史活动实例查询
*/
@Test
public void queryHistoryTask() {
List<HistoricTaskInstance> list = ateHistoricTaskInstanceQuery() // 创建历史活动实例查询
.processInstanceId("9671cdea-3367-11ea-a057-30b49ec7161f") // 执⾏流程实例id
.orderByTaskCreateTime()
.asc()
.list();
for (HistoricTaskInstance hai : list) {
System.out.println("活动ID:" + Id());
System.out.println("流程实例ID:" + ProcessInstanceId());
System.out.println("活动名称:" + Name());
System.out.println("办理⼈:" + Assignee());
System.out.println("开始时间:" + StartTime());
System.out.println("结束时间:" + EndTime());
}
}
}
源码下载

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。