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/
二. 流程例項 來源:http://www.fhadmin.org/webnewsdetail1.html
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.寫測試介面,進行測試 來源:http://www.fhadmin.org/webnewsdetail1.html
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); } }
啟動測試類,測試即可。
三. 問題 來源:http://www.fhadmin.org/webnewsdetail1.html
問題一: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/laboratory?useUnicode=true&characterEncoding=utf8 即可解決問題。暫時還不知道原理,猜測是與activiti的版本有關。