一:WorkFlow簡介
1:什么是工作流工作流(Workflow),指“業務過程的部分或整體在計算機應用環境下的自動化”。是對工作流程及其各操作步驟之間業務規則的抽象、概括描述。在計算機中,工作流屬於計算機支持的協同工作(CSCW)的一部分。后者是普遍地研究一個群體如何在計算機的幫助下實現協同工作的。
二:ssm整合activiti框架
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-engine -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.22.0</version>
</dependency>
<!--activiti整合ssm需要用到的jar-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.22.0</version>
</dependency>
2:spring_activiti.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置數據源連接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 數據庫連接屬性 -->-->
<property name="url" value="jdbc:mysql://localhost:3306/pmsdb?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<!-- 啟用系統監控過濾器 -->
<property name="filters" value="stat"/>
<!-- 最大連接池數量 -->
<property name="maxActive" value="200"/>
<!-- 初始化時建立物理連接的個數-->
<property name="initialSize" value="5"/>
<!-- 最小連接池數量-->
<property name="minIdle" value="5"/>
<!-- 獲取連接時最大等待時間,單位毫秒-->
<property name="maxWait" value="60000"/>
<!-- 銷毀線程會檢測連接的間隔時間,(單位毫秒) 如果連接空閑時間大於等於minEvictableIdleTimeMillis則關閉物理連接-->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 連接保持空閑而不被驅逐的最小時間 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<!--申請連接的時候檢測,如果空閑時間大於timeBetweenEvictionRunsMillis, 執行validationQuery檢測連接是否有效-->
<property name="testWhileIdle" value="true"/>
<!-- 申請連接時執行validationQuery檢測連接是否有效 -->
<property name="testOnBorrow" value="false"/>
<!-- 歸還連接時執行validationQuery檢測連接是否有效-->
<property name="testOnReturn" value="false"/>
<!-- 是否緩存preparedStatement, mysql建議關閉-->
<property name="poolPreparedStatements" value="false"/>
<!-- 偽SQL,用於檢查連接是否可用 -->
<property name="validationQuery" value="select 1"/>
</bean>
<!-- 裝配DataSource的事務管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 需要注入一個DataSource -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--創建一個流程引擎的配置對象 這里我們使用的是Spring提供的流程引擎對象-->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="txManager"/>
<!-- 是否自動創建25張表 -->
<property name="databaseSchemaUpdate" value="true"/>
<!--是否啟動jobExecutor-->
<property name="jobExecutorActivate" value="false"/>
<property name="databaseType" value="mysql"/>
<property name="activityFontName" value="宋體"/>
<property name="labelFontName" value="黑體"/>
</bean>
<!--<property name="xmlEncoding" value="utf-8"/>-->
<!-- 創建一個流程引擎bean -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"/>
</bean>
<!--配置服務Bean,定義Service服務-->
<!--工作流倉儲服務,對所有atc_re開頭的表進行操作-->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
<!--工作流運行服務 對所有act_ru開頭的表進行操作 -->
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
<!--工作流任務服務-->
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
<!--工作流歷史數據服務 對所有的act_hi開頭的表進行操作-->
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
<!--工作流管理服務-->
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>
<!--工作流唯一服務, 對所有以act_id開頭的表進行增刪改查-->
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>
<!--工作流管理服務-->
<bean id="formService" factory-bean="processEngine" factory-method="getFormService"/>
</beans>
3:別忘了將spring.activiti.xml注入到spring容器中,交給spring容器管理
<!--引入工作流配置文件-->
<import resource="classpath*:spring-activiti.xml"/>
4.啟動項目時,將生成25張框架自帶的表
4.1:部分表的含義
序號 |
表名 |
說明 |
1 |
act_ge_bytearray |
二進制數據表 |
2 |
act_ge_property |
屬性數據表存儲整個流程引擎級別的數據,初始化表結構時,會默認插入三條記錄, |
3 |
act_hi_actinst |
歷史節點表 |
4 |
act_hi_attachment |
歷史附件表 |
5 |
act_hi_comment |
歷史意見表 |
6 |
act_hi_identitylink |
歷史流程人員表 |
7 |
act_hi_detail |
歷史詳情表,提供歷史變量的查詢 |
8 |
act_hi_procinst |
歷史流程實例表 |
9 |
act_hi_taskinst |
歷史任務實例表 |
10 |
act_hi_varinst |
歷史變量表 |
11 |
act_id_group |
用戶組信息表 |
12 |
act_id_info |
用戶擴展信息表 |
13 |
act_id_membership |
用戶與用戶組對應信息表 |
14 |
act_id_user |
用戶信息表 |
15. |
act_re_deployment |
部署信息表 |
16. |
act_re_model |
流程設計模型部署表 |
17 |
act_re_procdef |
流程定義數據表 |
18 |
act_ru_event_subscr |
throwEvent、catchEvent時間監聽信息表 |
19 |
act_ru_execution |
運行時流程執行實例表 |
20 |
act_ru_identitylink |
運行時流程人員表,主要存儲任務節點與參與者的相關信息 |
21 |
act_ru_job |
運行時定時任務數據表 |
22 |
act_ru_task |
運行時任務節點表 |
23 |
act_ru_variable |
運行時流程變量數據表 |
5:測試流程,首先使用idea下載activiti插件actiBPM,下載后重啟idea,畫出流程圖,並生成png圖片
apply.bpmn
apply.png圖片
5:WorkFlowTest
package com.htzs.pms.service.address.impl; import org.activiti.engine.*; import org.activiti.engine.repository.Deployment; 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.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; import java.util.List; /** * @author : ywb * @createdDate : 2019/9/18 * @updatedDate */ @RunWith(SpringRunner.class) @ContextConfiguration(locations = {"classpath:dispatcher-dao.xml", "classpath:dispatcher-service.xml"}) @WebAppConfiguration public class WorkFlowTest { /** * 測試activiti是否與spring整合成功,成功標志如下 * 控制台輸出org.activiti.engine.impl.ProcessEngineImpl@4721ee77說明已經整合成功 */ @Test public void test() { // 1.創建Activiti配置對象的實例
ProcessEngineConfiguration configuration = ProcessEngineConfiguration .createStandaloneProcessEngineConfiguration(); // 2.設置數據庫連接信息 // 設置數據庫的類型
configuration.setDatabaseType("mysql"); // 設置數據庫驅動
configuration.setJdbcDriver("com.mysql.jdbc.Driver"); // 設置jdbcURL
configuration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false"); // 設置用戶名
configuration.setJdbcUsername("root"); // 設置密碼
configuration.setJdbcPassword("root"); // 設置數據庫建表策略
/** * DB_SCHEMA_UPDATE_TRUE:如果不存在表就創建表,存在就直接使用 * DB_SCHEMA_UPDATE_FALSE:如果不存在表就拋出異常 * DB_SCHEMA_UPDATE_CREATE_DROP:每次都先刪除表,再創建新的表 */ configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP); // 3.使用配置對象創建流程引擎實例(檢查數據庫連接等環境信息是否正確)
ProcessEngine processEngine = configuration.buildProcessEngine(); System.out.println(processEngine); } /** * 第二種創建方式 一般使用最多的就是這種創建方式 */ @Test public void initActiviti() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); System.out.println(processEngine); } @Autowired private RepositoryService repositoryService; @Autowired private RuntimeService runtimeService; @Autowired private TaskService taskService; /** * 部署流程定義 */ @Test public void deployProcess(){ //得到流程部署的service
Deployment deploy = repositoryService.createDeployment().name("請假流程001") .addClasspathResource("apply.bpmn") .addClasspathResource("apply.png").deploy(); System.out.println("部署成功,流程id:"+deploy.getId()); } /** * 啟動流程 */ @Test public void startProcess(){ //使用key啟動流程
runtimeService.startProcessInstanceByKey("myProcess_1"); //使用id啟動流程 // runtimeService.startProcessInstanceById("myProcess_1:1:4");
System.out.println("流程啟動成功"); } /** * 查詢流程 */ @Test public void findProcess(){ List<Task> tasks = taskService.createTaskQuery().taskAssignee("王五").list(); for (Task task : tasks) { System.out.println("任務id"+task.getId()); System.out.println("流程實例id"+task.getProcessInstanceId()); System.out.println("執行實例id"+task.getExecutionId()); System.out.println("流程定義id"+task.getProcessDefinitionId()); System.out.println("任務名稱"+task.getName()); System.out.println("任務辦理人"+task.getAssignee()); } } @Test public void completeProcess(){ taskService.complete("10002"); System.out.println("任務完成"); } }
三:使用SpringBoot 整合activiti框架並整合activiti流程設計器
1:需要使用的maven
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<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>
<!--activiti begin-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>${activiti.version}</version>
</dependency>
<!--activiti end-->
</dependencies>
2.application.yml文件
server: port: 8085 security: basic: enabled: false spring: datasource: url: jdbc:mysql://localhost:3306/activiti??useUnicode=true&characterEncoding=utf8&autoReconnect=true username: root password: root thymeleaf: mode: LEGACYHTML5 cache: false
3:java注解配置類
package edu.nf.project.config;
import org.activiti.engine.*;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.io.IOException;
/**
* @author : ywb
* @createdDate : 2019/9/20
* @updatedDate
*/
@Configuration
public class ActivitiConfiguration {
@Autowired
private DataSource dataSource;
@Autowired
private PlatformTransactionManager platformTransactionManager;
/**
* 創建一個流程引擎的配置對象 這里我們使用的是Spring提供的流程引擎對象
* 創建一個流程引擎的配置對象 這里我摸嗯使用的事Spring提供的流程引擎對象
* @return
*/
@Bean
public SpringProcessEngineConfiguration springProcessEngineConfiguration(){
SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
spec.setDataSource(dataSource);
spec.setTransactionManager(platformTransactionManager);
spec.setDatabaseSchemaUpdate("true");
Resource[] resources = null;
// 啟動自動部署流程
try {
resources = new PathMatchingResourcePatternResolver().getResources("classpath*:bpmn/*.bpmn");
} catch (IOException e) {
e.printStackTrace();
}
spec.setDeploymentResources(resources);
return spec;
}
/**
* 創建一個流程引擎bean
* @return
*/
@Bean
public ProcessEngineFactoryBean processEngine(){
ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration());
return processEngineFactoryBean;
}
/**
* 工作流倉儲服務,對所有atc_re開頭的表進行操作-
* @return
* @throws Exception
*/
@Bean
public RepositoryService repositoryService() throws Exception{
return processEngine().getObject().getRepositoryService();
}
/**
* 工作流運行服務 對所有act_ru開頭的表進行操作
* @return
* @throws Exception
*/
@Bean
public RuntimeService runtimeService() throws Exception{
return processEngine().getObject().getRuntimeService();
}
/**
* 工作流任務服務
* @return
* @throws Exception
*/
@Bean
public TaskService taskService() throws Exception{
return processEngine().getObject().getTaskService();
}
/**
* 工作流歷史數據服務 對所有的act_hi開頭的表進行操作
* @return
* @throws Exception
*/
@Bean
public HistoryService historyService() throws Exception{
return processEngine().getObject().getHistoryService();
}
/**
* 工作流唯一服務,對所有以act_id開頭的表進行增刪改查
* @return
* @throws Exception
*/
public IdentityService identityService() throws Exception{
return processEngine().getObject().getIdentityService();
}
/**
* 工作流管理服務
* @return
* @throws Exception
*/
public FormService formService() throws Exception{
return processEngine().getObject().getFormService();
}
/**
* 工作流管理服務
* @return
* @throws Exception
*/
public ManagementService managementService() throws Exception{
return processEngine().getObject().getManagementService();
}
}