logo头像

生而无畏,战至终章

activiti集成Springboot

最近项目中用;到的开源流程引擎框架activiti,于是乎就有了下面的这篇文章

准备

版本信息如下:

  1. activiti 5.22.0
  2. Springboot 1.5.14
  3. Maven

maven配置

Springboot相关的依赖,我这里就直接省略,如果不清楚的同学google下吧

在pom文件中引入activiti与Springboot集成的依赖

1
2
3
4
5
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.22.0</version>
</dependency>

数据源的配置

先要知道的是activiti流程引擎默认可以通过resource目录下的activiti.cfg.xml文件来配置数据源,这里也贴一下里面的配置文件信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- 配置 ProcessEngineConfiguration -->
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!-- 配置数据库连接 -->
<property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/openwebflow?useUnicode=true&amp;characterEncoding=utf8"></property>
<property name="jdbcUsername" value="root"></property>
<property name="jdbcPassword" value="root1"></property>
<property name="jdbcUrl" value="${spring.datasource.url}"></property>
<property name="jdbcUsername" value="${spring.datasource.username}"></property>
<property name="jdbcPassword" value="${spring.datasource.password}"></property>

<!-- 配置创建表策略 :没有表时,自动创建 -->
<property name="databaseSchemaUpdate" value="true"></property>
<!--设置字体-->
<property name="activityFontName" value="宋体"/>
<property name="labelFontName" value="宋体"/>
<property name="annotationFontName" value="宋体"/>
</bean>
</beans>

以上的xml配置不是我们要说的重点,既然用了Springboot,就不会存在xml文件,那我们如何配置数据源了

首先在applicant.properties中配置数据库的信息,

1
2
3
4
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3066/openwebflow?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root

然后实现ProcessEngineConfigurationConfigurer接口,配置数据库连接信息,将其注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@Component
public class ActivitiDataBaseConfig implements ProcessEngineConfigurationConfigurer {

private static final Logger logger = LoggerFactory.getLogger(ActivitiDataBaseConfig.class);

@Value("${spring.datasource.driver-class-name}")
private String driverClassName;

@Value("${spring.datasource.url}")
private String url;

@Value("${spring.datasource.username}")
private String username;

@Value("${spring.datasource.password}")
private String password;

@Override
public void configure(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
springProcessEngineConfiguration.setJdbcDriver(driverClassName);
springProcessEngineConfiguration.setJdbcUrl(url);
springProcessEngineConfiguration.setJdbcUsername(username);
springProcessEngineConfiguration.setJdbcPassword(password);
springProcessEngineConfiguration.setActivityFontName("宋体");
springProcessEngineConfiguration.setLabelFontName("宋体");
springProcessEngineConfiguration.setAnnotationFontName("宋体");
//设定数据库连接大小,默认为10
springProcessEngineConfiguration.setJdbcMaxActiveConnections(300);
//设置不启动定时任务,这里也可以解决启动后里面有很多定时任务的日志
springProcessEngineConfiguration.setAsyncExecutorEnabled(false);
springProcessEngineConfiguration.setAsyncExecutorActivate(false);
}

}

遇到的坑

本以为这样就完美解决了,结果还是启动失败了,提示找不到classpath下的process文件夹,google后才知道,集成了Springboot的activiti流程引擎是会加载classpath下的bpmn文件,也就是流程配置的文件,那么如何在启动的时候不自动检查了,需要在application.properties属性文件中加入如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# spring-activiti
# 自动部署验证设置:true-开启(默认)、false-关闭
spring.activiti.check-process-definitions=false

# activiti会对数据库中所有表进行更新操作。如果表不存在,则自动创建,true-表示创建或更新、false-关闭
spring.activiti.database-schema-update=false

#还有下面的几个配置也顺便的提下,虽然没用到
# asyncExecutorEnabled属性设置设置true后将代替那些老的Job executor
spring.activiti.async-executor-enabled=false
spring.activiti.job-executor-activate=false

# asyncExecutorActivate是指activiti在流程引擎启动就激活AsyncExecutor,异步:true-开启(默认)、false-关闭
spring.activiti.async-executor-activate=true

# 使用自定义的mybatis-mapper
spring.activiti.custom-mybatis-mappers=
spring.activiti.custom-mybatis-xmlmappers=

最后在啰嗦一下,需要用到流程引擎的相关服务,直接注入进来就可以调用相关的方法了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Resource
private ProcessEngine processEngine;

@Resource
private HistoryService historyService;

@Resource
private TaskService taskService;

@Resource
private RuntimeService runtimeService;

@Resource
private RepositoryService repositoryService;

以上,就是activiti与Springboot集成的全过程,有任何问题,欢迎留言,感谢阅读!!!