Activiti是領先的輕量級的,以Java為中心的開源BPMN(Business Process Modeling Notation)引擎,實現了真正的流程自動化。下面介紹如何在SpringBoot環境下使用Maven集成Activiti6,來實現流程開發。
添加依賴
-
<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-web
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-jdbc
</artifactId>
-
</dependency>
添加Processes目錄
SpringBoot集成activiti默認會從classpath下的processes目錄下讀取流程定義文件,所以需要在src/main/resources目錄下添加processes目錄,並在目錄中創建流程文件,添加目錄后,目錄結構變為:
如果沒有processes目錄,則需要修改配置spring.activiti.process-definition-location-prefix,指定流程文件存放目錄。
Spring集成Activiti6默認支持**.bpmn20.xml和**.bpmn格式的流程定義文件,修改支持的文件格式,通過配置spring.activiti.process-definition-location-suffixes修改
如:
-
spring:
-
activiti:
-
check-process-definitions:
true #自動檢查、部署流程定義文件
-
database-schema-update:
true #自動更新數據庫結構
-
process-definition-location-prefix: classpath:/processes/ #流程定義文件存放目錄
-
#process-definition-location-suffixes: #流程文件格式
-
# - **.bpmn20.xml
-
# - **.bpmn
啟動項目時,如果沒有流程部署,就不能通過自動注入,使用RuntimeService等API,依賴注入時后報錯。
ActivitiProperties中定義了activiti的自動配置項,其他配置請查看ActivitiProperties屬性。
添加數據源
添加數據源,項目中添加數據源,初始化數據庫結構,后續保存流程數據,
-
spring :
-
#data source config
-
datasource :
-
driver : com.mysql.jdbc.Driver
-
url: jdbc:mysql:
//192.168.105.10:3306/test_db?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
-
username : root
-
password : mysql
-
initsize :
10
-
maxActive :
20
-
minIdle :
10
-
maxWait :
120000
-
poolPreparedStatements :
false
-
maxOpenPreparedStatements : -
1
-
validationQuery : select
1
-
testOnborrow :
true
-
testOnReturn :
true
-
testWhileIdle :
true
-
timeBetweenEvictionRunsMillis :
120000
-
filters : log4j,stat
添加流程
在項目中添加流程,創建文件simple.bpmn,添加內容
-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="Examples" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1539757531057" name="" targetNamespace="Examples" typeLanguage="http://www.w3.org/2001/XMLSchema">
-
<process id="oneTaskProcess" isClosed="false" name="The One Task Process" processType="None">
-
<startEvent id="theStart"/>
-
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask"/>
-
<userTask activiti:assignee="${user}" activiti:exclusive="true" id="theTask" name="my task"/>
-
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd"/>
-
<endEvent id="theEnd"/>
-
</process>
-
</definitions>
啟動測試
編寫SpringBoot啟動類,啟動項目,啟動項目。
-
@SpringBootApplication(scanBasePackages =
"com.legao.server")
-
@EnableSwagger2
-
public
class WorkflowServer {
-
-
public static void main(String[] args) {
-
-
SpringApplication.run(WorkflowServer.class, args);
-
}
-
}
啟動時,發現啟動報錯,
-
Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
-
-
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'requestMappingHandlerMapping' defined in
class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
查看activiti-spring-boot-starter-basic-6.0.0.jar發現,org.activiti.spring.boot.SecurityAutoConfiguration編譯報錯,這時候將SecurityAutoConfiguration排除到SpringBoot啟動之外,即@SpringBootApplication注解添加exclude = SecurityAutoConfiguration.class屬性
-
@SpringBootApplication(scanBasePackages =
"com.legao.server", exclude = SecurityAutoConfiguration.class)
-
@EnableSwagger2
-
public
class WorkflowServer {
-
-
public static void main(String[] args) {
-
-
SpringApplication.run(WorkflowServer.class, args);
-
}
-
}
再啟動發現啟動正常,這時候SpringBoot集成activiti已經啟動成功,查看數據庫,Activiti6運行所需的28張表也已經創建成功。
-
ACT_EVT_LOG
-
ACT_GE_BYTEARRAY
-
ACT_GE_PROPERTY
-
ACT_HI_ACTINST
-
ACT_HI_ATTACHMENT
-
ACT_HI_COMMENT
-
ACT_HI_DETAIL
-
ACT_HI_IDENTITYLINK
-
ACT_HI_PROCINST
-
ACT_HI_TASKINST
-
ACT_HI_VARINST
-
ACT_ID_GROUP
-
ACT_ID_INFO
-
ACT_ID_MEMBERSHIP
-
ACT_ID_USER
-
ACT_PROCDEF_INFO
-
ACT_RE_DEPLOYMENT
-
ACT_RE_MODEL
-
ACT_RE_PROCDEF
-
ACT_RU_DEADLETTER_JOB
-
ACT_RU_EVENT_SUBSCR
-
ACT_RU_EXECUTION
-
ACT_RU_IDENTITYLINK
-
ACT_RU_JOB
-
ACT_RU_SUSPENDED_JOB
-
ACT_RU_TASK
-
ACT_RU_TIMER_JOB
-
ACT_RU_VARIABLE
(完)