Flowable核心ProcessEngine详解

Flowable核心ProcessEngine详解
在这里插入图片描述




点点关注不迷路

Flowable核心ProcessEngine详解


Flowable核心内容

1.表结构讲解

  工作流程的相关操作都是操作存储在对应的表结构中,为了能更好的弄清楚Flowable的实现原理和细节,我们有必要先弄清楚Flowable的相关表结构及其作用。在Flowable中的表结构在初始化的时候会创建五类表结构,具体如下:

  • ACT_RE :’RE’表示 repository。这个前缀的表包含了流程定义和流程静态资源 (图片,规则,等等)。
  • ACT_RU:’RU’表示 runtime。这些运行时的表,包含流程实例,任务,变量,异步任务,等运行中的数据。Flowable只在流程实例执行过程中保存这些数据, 在流程结束时就会删除这些记录。这样运行时表可以一直很小速度很快。
  • ACT_HI:’HI’表示 history。这些表包含历史数据,比如历史流程实例, 变量,任务等等。
  • ACT_GE:GE 表示 general。通用数据, 用于不同场景下
  • ACT_ID:   ’ID’表示identity(组织机构)。这些表包含标识的信息,如用户,用户组,等等。

具体的表结构的含义:

表分类 表名 解释
一般数据


[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_RE_DEPLOYMENT] 部署单元信息

[ACT_RE_MODEL] 模型信息

[ACT_RE_PROCDEF] 已部署的流程定义
运行实例表


[ACT_RU_EVENT_SUBSCR] 运行时事件

[ACT_RU_EXECUTION] 运行时流程执行实例

[ACT_RU_IDENTITYLINK] 运行时用户关系信息,存储任务节点与参与者的相关信息

[ACT_RU_JOB] 运行时作业

[ACT_RU_TASK] 运行时任务

[ACT_RU_VARIABLE] 运行时变量表
用户用户组表


[ACT_ID_BYTEARRAY] 二进制数据表

[ACT_ID_GROUP] 用户组信息表

[ACT_ID_INFO] 用户信息详情表

[ACT_ID_MEMBERSHIP] 人与组关系表

[ACT_ID_PRIV] 权限表

[ACT_ID_PRIV_MAPPING] 用户或组权限关系表

[ACT_ID_PROPERTY] 属性表

[ACT_ID_TOKEN] 记录用户的token信息

[ACT_ID_USER] 用户表

2.ProcessEngine讲解

2.1 硬编码的方式

  我们前面讲解案例的时候是通过ProcessEngineConfiguration这个配置类来加载的。

// 配置数据库相关信息 获取 ProcessEngineConfiguration
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
    .setJdbcUrl("jdbc:mysql://localhost:3306/flowable-learn2?serverTimezone=UTC&nullCatalogMeansCurrent=true")
    .setJdbcUsername("root")
    .setJdbcPassword("123456")
    .setJdbcDriver("com.mysql.cj.jdbc.Driver")
    .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
// 获取流程引擎对象
ProcessEngine processEngine = cfg.buildProcessEngine();

  这种方式会调用buildProcessEngine()方法,里面的核心代码为:

Flowable核心ProcessEngine详解
在这里插入图片描述
Flowable核心ProcessEngine详解
在这里插入图片描述

2.2 配置文件

  除了上面的硬编码的方式外,我们还可以在resources目录下创建一个flowable.cfg.xml文件,注意这个名称是固定的哦。内容如下:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="processEngineConfiguration"
          class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration">

        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/flow1?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=UTC&amp;nullCatalogMeansCurrent=true" /><property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="123456" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="asyncExecutorActivate" value="false" />
    </bean>
</beans>

  在上面的配置文件中配置相关的信息。我们在Java代码中就可以简化为:

    @Test
    public void test01(){
        // 获取流程引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println("processEngine = " + processEngine);
    }

  可以看下getDefaultProcessEngine的源码,在里面最终还是执行了和硬编码一样的代码

    public static ProcessEngine getProcessEngine(String processEngineName) {
        if (!isInitialized()) {
            init(); // 完成初始化操作
        }
        return processEngines.get(processEngineName);
    }

  进入init方法

    public static synchronized void init() {
        if (!isInitialized()) {
            if (processEngines == null) {
                // Create new map to store process-engines if current map is null
                processEngines = new HashMap<>();
            }
            ClassLoader classLoader = ReflectUtil.getClassLoader();
            Enumeration<URL> resources = null;
            try {
                resources = classLoader.getResources("flowable.cfg.xml"); // 加载flowable.cfg.xml配置文件
            } catch (IOException e) {
                throw new FlowableIllegalArgumentException("problem retrieving flowable.cfg.xml resources on the classpath: " + System.getProperty("java.class.path"), e);
            }

            // Remove duplicated configuration URL's using set. Some
            // classloaders may return identical URL's twice, causing duplicate
            // startups
            Set<URL> configUrls = new HashSet<>();
            while (resources.hasMoreElements()) {
                configUrls.add(resources.nextElement());
            }
            for (Iterator<URL> iterator = configUrls.iterator(); iterator.hasNext();) {
                URL resource = iterator.next();
                LOGGER.info("Initializing process engine using configuration '{}'", resource.toString());
                initProcessEngineFromResource(resource); // 初始化ProcessEngine
            }

            try {
                resources = classLoader.getResources("flowable-context.xml"); // 在整合Spring的情况下加载该文件
            } catch (IOException e) {
                throw new FlowableIllegalArgumentException("problem retrieving flowable-context.xml resources on the classpath: " + System.getProperty("java.class.path"), e);
            }
            while (resources.hasMoreElements()) {
                URL resource = resources.nextElement();
                LOGGER.info("Initializing process engine using Spring configuration '{}'", resource.toString());
                initProcessEngineFromSpringResource(resource); // 从Spring的资源文件中完成ProcessEngine的初始化
            }

            setInitialized(true);
        } else {
            LOGGER.info("Process engines already initialized");
        }
    }

  在源码中提供了单独使用好整合Spring的配置加载方式。再进入到initProcessEngineFromResource(resource)方法中:

Flowable核心ProcessEngine详解
在这里插入图片描述
Flowable核心ProcessEngine详解
在这里插入图片描述
Flowable核心ProcessEngine详解
在这里插入图片描述

而且我们也可以看到ProcessEngine最终的实现是 ProcessEngineImpl对象。

2.3 自定义配置文件

  最后我们如果要加载自定义名称的配置文件可以通过ProcessEngineConfiguration中的对应构造方法来实现

    @Test
    public void test2() throws Exception{
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("flowable.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println("processEngine = " + processEngine);
    }

3. Servcie服务接口

Service是工作流引擎提供用于进行工作流部署、执行、管理的服务接口,我们使用这些接口可以就是操作服务对应的数据表

Flowable核心ProcessEngine详解
在这里插入图片描述

3.1 Service创建方式

通过ProcessEngine创建Service

方式如下:

RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService();
// ...

3.2 Service总览

service名称 service作用
RepositoryService Flowable的资源管理类
RuntimeService Flowable的流程运行管理类
TaskService Flowable的任务管理类
HistoryService Flowable的历史管理类
ManagerService Flowable的引擎管理类

简单介绍:

ProcessEngines.getDefaultProcessEngine()第一次被调用时,将初始化并构建流程引擎,之后的重复调用都会返回同一个流程引擎。可以通过ProcessEngines.init()创建流程引擎,并由ProcessEngines.destroy()关闭流程引擎。

RepositoryService

是activiti的资源管理类,提供了管理和控制流程发布包和流程定义的操作。使用工作流建模工具设计的业务流程图需要使用此service将流程定义文件的内容部署到计算机。

除了部署流程定义以外还可以:查询引擎中的发布包和流程定义。

暂停或激活发布包,对应全部和特定流程定义。暂停意味着它们不能再执行任何操作了,激活是对应的反向操作。获得多种资源,像是包含在发布包里的文件, 或引擎自动生成的流程图。

获得流程定义的pojo版本, 可以用来通过java解析流程,而不必通过xml。

RuntimeService

Activiti的流程运行管理类。可以从这个服务类中获取很多关于流程执行相关的信息

TaskService

Activiti的任务管理类。可以从这个类中获取任务的信息。

HistoryService

Flowable的历史管理类,可以查询历史信息,执行流程时,引擎会保存很多数据(根据配置),比如流程实例启动时间,任务的参与者, 完成任务的时间,每个流程实例的执行路径,等等。这个服务主要通过查询功能来获得这些数据。

ManagementService

Activiti的引擎管理类,提供了对Flowable 流程引擎的管理和维护功能,这些功能不在工作流驱动的应用程序中使用,主要用于 Flowable 系统的日常维护。



原文始发于微信公众号(波哥带你学编程):Flowable核心ProcessEngine详解

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/58709.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!