Activiti7 基础知识

本文其实主要是观看教程的一些个人笔记。主要介绍基础知识和一些使用 activiti 之前要做的准备工作

activiti 介绍

activiti 是目前非常流行的工作流引擎,用于实现项目中工作流的自动化控制。
没有使用工作流引擎的项目,一般使用状态码来标识区分各流程。这样开发的话,如果后期工作流程发生变化,加一个流程步骤或减少一个流程步骤,都要重新修改代码;这样就不太好扩展、维护。
如果使用了工作流引擎,这些问题就几乎不存在了,可以省了很多事。

BPM (Business Process Management) 业务流程管理
BPMN (Business Process Model And Notation) 业务流程模型和符号

该工作流引擎本身有25张表,有比较大的成本,适用于规模比较大的项目

activiti 原理

1)画好工作流程图——实际上是一个xml文件
2)activiti 将流程图中每个节点的数据读取到表中
3)读取表中的第一条记录,处理后删除

activiti 使用步骤

1)使用BPMN设计业务流程图
2)部署业务流程到Activiti
3)启动流程实例
4)查询待办业务
5)处理待办业务
6)结束流程

安装绘制流程图的插件

IDEA

1)在线安装(要翻墙)
settings >> plugins 搜索 actiBPM。

2)离线下载安装包(推荐)
https://plugins.jetbrains.com/plugin/7429-actibpm

eclipse

1)在线安装
help >> install new software
Name: 自定义一个名称(Activiti BPMN 2.0 designer)
Location: 输入网址 https://www.activiti.org/designer/update/

2)离线下载插件包(推荐)
把插件包中的 features 和 plugins 中的文件分别全部复制到 eclipse 安装目录中的 features 和 plugins

如果安装成功,是可以创建 BpmnFile 的
在这里插入图片描述

创建 activiti 的表结构

创建数据库 test_activiti
使用 java 代码创建25张表结构

1)创建 maven 项目,引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>activitiDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-model</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-converter</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-json-converter</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-layout</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.activiti.cloud</groupId>
            <artifactId>activiti-cloud-services-api</artifactId>
            <version>7.0.0.Beta1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.26</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.26</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>alfresco</id>
            <name>activiti releases</name>
            <url>https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

</project>

2)配置文件
log4j 配置文件:

log4j.rootCategory=debug, CONSOLE, LOGFILE
log4j.logger.ort.apache.axis.enterprise=FATAL, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=g:\\temp\\activitiDemo.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

activiti 默认配置文件:activiti.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--数据源配置dbcp-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test_activiti?serverTimezone=GMT%2B8&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;allowPublicKeyRetrieval=true"/>
        <property name="username" value="root"/>
        <property name="password" value="godXiao"/>
     </bean>
    <!--activiti单独运行的 ProcessEngine 配置,使用单独启动方式-->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource"/>
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>
    
</beans>
需要注意的是,在xml配置文件中,url中的 & 符号需要转义成 &amp;

3)测试类

public class ActivitiTest {
    
    
    //测试 activiti 的25张表的生成
    @Test
    public void createTable() {
    
    
        //1、创建 ProcessEngineConfiguration 对象
        //ProcessEngineConfiguration configuration = ProcessEngineConfiguration
        //        .createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        //后面的参数是配置文件 bean 的id,默认是 processEngineConfiguration,可以自定义其他名称
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("activiti.cfg.xml","processEngineConfiguration");
        //2、创建 ProcessEngine 对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //然后就可以通过 processEngine 获取到不同的 service 接口去操作那 25 张表了

        System.out.println(processEngine);
    }

    @Test
    public void createTableDefault() {
    
    
        //前提:1、activiti 的配置文件是 activiti.cfg.xml;2、activiti 的配置文件中 bean 的 id 必须是 processEngineConfiguration
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);
    }
}

首先创建 ProcessEngineConfiguration对象,通过 ProcessEngineConfiguration对象创建 ProcessEngine 对象,在创建 ProcessEngine 对象时会自动创建表结构

执行这个测试方法,可以看到控制台有很多创建表结构的 sql 语句
在这里插入图片描述
查看数据库 test_activiti,发现 25 张表创建成功
在这里插入图片描述

25张表命名规则
表名 作用描述
act_re_* “re”表示 repository 。这些表包含了流程定义和流程静态资源(图片、规则等)
act_ru_* “ru”表示 runtime 。这些运行时的表,包含流程的实例、任务、变量、异步任务等运行中的数据。Activiti 只在流程实例执行过程中保存这些数据,在流程结束时就会删除这些记录。这样运行时表可以一直很小,速度很快
act_hi_* “hi”表示 history 。这些表包含历史数据,比如历史流程实例、变量、任务等
act_ge_* “ge”表示 general 。通用数据,用于不同的场景下
service 接口作用
接口 作用
RepositoryService activiti 的资源管理类
RuntimeService activiti 的流程运行管理类
TaskService activiti 的任务管理类
HistoryService activiti 的历史管理类
ManagerService activiti 的引擎管理类

注意:在新版本中,IdentityService, FormService 两个 Service 都被删除

猜你喜欢

转载自blog.csdn.net/Alias_fa/article/details/107288581
今日推荐