SpringBoot 集成 Activiti 一路踩得坑
由于项目需要,本人开始在项目Spring boot 中集成工作流引擎Activiti。由于第一次集成,一路上步步都是坑,怪我没有先去看官方文档。现将一路上遇到的问题一一记录。
一. 环境配置
1.项目环境:Springboot2.0 + Maven + activiti6.0 + Eclipse
2.maven依赖:
<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-basic</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 单元测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
3.安装eclipse插件:Activiti Designer插件;地址:http://www.activiti.org/designer/update/
二. 流程实例
1.Spring boot项目启动类
import org.activiti.spring.boot.SecurityAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@ComponentScan(value = {"com.river", "com.river.learn" })
public class App {
public static void main( String[] args ){
SpringApplication.run(App.class, args);
}
}
2.在 src/main/resources 下新建目录: processes/MyProcesses.bpmn 。
注意:任务图标有好几种,如上就有两种,内涵是不同的。Properties是视图,可以在window -> show view中打开
3.写service接口,用于bpmn文件调用
@Service public class TestService { public void activiti() { System.out.println("任务已经执行....................................."); } public List<String> user() { return Arrays.asList("xiaoming","xiaohong"); } }
4.写测试接口,进行测试
package com.river.learn; import java.util.HashMap; import java.util.List; import java.util.Map; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.river.learn.web.service.TestService; import com.river.main.App; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ActivitiTest { @Autowired TestService testService; @Autowired RuntimeService runtimeService; @Autowired private TaskService taskService; @Test public void TestStartProcess() { System.out.println("Start........."); ProcessInstance pi = runtimeService.startProcessInstanceByKey("myProcess", "1"); System.out.println("流程启动成功,流程id:"+pi.getId()); } @Test public void findTasksByUserId() { String userId ="dulingjiang"; List<Task> resultTask = taskService.createTaskQuery().processDefinitionKey("myProcess").taskCandidateOrAssigned(userId).list(); System.out.println("任务列表:"+resultTask); } }
启动测试类,测试即可。
三. 问题
问题一:Caused by: java.lang.ArrayStoreException:sun.reflect.annotation.TypeNotPresentExceptionProxy
解决办法:项目一启动就报这个错误,折腾了很久都没有解决这个问题,最后在一篇博客上找到解决办法,但是遗憾的是忘记了作者的博客链接。解决办法是直接在启动类上加上 @SpringBootApplication(exclude = SecurityAutoConfiguration.class) 即可。SecurityAutoConfiguration.class来自于类 org.activiti.spring.boot.SecurityAutoConfiguration。第一次我引错了类,导致该问题无法解决。我还认为我的错误不是用这个方法解决。哎,小心仔细啊。
问题二:org.activiti.engine.ActivitiObjectNotFoundException: no processes deployed with key 'MyProcess'
解决办法:控制台提示 runtimeService.startProcessInstanceByKey("MyProcess", map) 中没有“MyProcess”,但是我在resource中的bpmn文件就是“MyProcess.bpmn”
改成小写:runtimeService.startProcessInstanceByKey("myProcess", map) 成功,runtimeService.startProcessInstanceByKey("MyProcess", map)中的"MyProcess"是指bpmn文件的id,有一个默认id为“myProcess”;
问题三:org.activiti.engine.ActivitiException: Could not execute service task expression
解决办法:在MyProcess.bpmn文件中配置的任务表达式错误,导致无法解析:在表达式中首字母应该小写,否则无法识别。
问题四:org.activiti.engine.ActivitiException: Expression did not resolve to a string or collection of strings
解决办法:我在第一次书写接口方法时,没有返回值,所以报错。审批节点设置的人返回的应该是数组,否则是错误的。
问题五:Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName。
问题描述:为activiti配置独立的数据源时,遇到了该问题。以下为配置:
application.properties文件
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=123 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
ActivitiConfig.java文件
import java.io.IOException; import javax.sql.DataSource; import org.activiti.spring.SpringAsyncExecutor; import org.activiti.spring.SpringProcessEngineConfiguration; import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.transaction.PlatformTransactionManager; import com.river.datasource.DBIdentifier; import com.river.datasource.DDSHolder; import com.river.datasource.DynamicDataSource; @Configuration//声名为配置类,继承Activiti抽象配置类 public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration { @Bean @Primary @ConfigurationProperties(prefix = "spring.datasource") public DataSource activitiDataSource() { return DataSourceBuilder.create().build(); } @Bean public SpringProcessEngineConfiguration springProcessEngineConfiguration( PlatformTransactionManager transactionManager, SpringAsyncExecutor springAsyncExecutor) throws IOException { return baseSpringProcessEngineConfiguration( activitiDataSource(), transactionManager, springAsyncExecutor); } }
解决办法:修改application.properties文件中 spring.datasource.url 为 spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 即可解决问题。暂时还不知道原理,猜测是与activiti的版本有关。
待续......