Detailed explanation of Activiti7 native integration and workflow related concepts

I. Overview

ActivitiIt is a workflow engine that Activitican extract complex business processes in the business system and BPMN2.0define them with a special modeling language. The business processes are executed according to the pre-defined processes, which realizes the Activitimanagement of the system processes and reduces the business system due to The workload of system upgrade and transformation by process change improves the robustness of the system and reduces the cost of system development and maintenance.

Official website address:

<https://www.activiti.org/>

2. Integrate Activiti based on Maven

This is also a relatively native way to use Activiti, which is helpful for Activitiunderstanding the basics.

2.1 Create a Maven project

insert image description here
insert image description here

After entering the basic information of the project, click below Finishto create a basic Mavenproject.

2.2 Add Maven coordinates in the pom file

<properties>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <activiti.version>7.0.0.Beta1</activiti.version>
</properties>
<dependencies>
    <!-- activiti流程引擎 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-engine</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <!-- activiti和spring的整合包 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <!-- bpmn 模型处理 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-bpmn-model</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <!-- bpmn 转换 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-bpmn-converter</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <!-- bpmn json数据转换 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-json-converter</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <!-- bpmn 布局 -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-bpmn-layout</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <!-- activiti 云支持 -->
    <dependency>
        <groupId>org.activiti.cloud</groupId>
        <artifactId>activiti-cloud-services-api</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <!-- mysql驱动(版本不要过低,要不然连接mysql8 的时候直接会报如下错误:) -->
    <!-- Cannot create PoolableConnectionFactory (Unknown system variable ‘query_cache_size‘) -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.5</version>
    </dependency>
    <!-- 链接池 -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!-- log start -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
</dependencies>

If it is reported com.github.jgraph:jgraphxthat the dependency cannot be found, there are two solutions:

  • You can manually download dependencies locally

  • If you don't want to see the red report, you can exclude the dependency

    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-bpmn-layout</artifactId>
        <version>${activiti.version}</version>
        <exclusions>
            <exclusion>
                <groupId>com.github.jgraph</groupId>
                <artifactId>jgraphx</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    Because in fact, there is no such dependency and it does not affect the use

2.3 Add log4j log configuration file

Because of the introduction log4jof coordinates, it is necessary to add a configuration file Mavenin the project directory.resourceslog4j.properties

In this way, the system will output the log according to the configuration in the configuration file.

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
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
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=F:\activiti\activiti.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

Changes can be made based on project needs or personal preferences.

Notice:

log4j.appender.LOGFILE.FileThe configured value is under the corresponding path F:\activiti\activiti.logon Fthe disk, and activiti.logthe file needs to exist in advance, otherwise an exception will be reported that the file cannot be found.

2.4 Add Activiti configuration file

ActivitiThe provided default way to create MySQLtable requirements resourcesis to create activiti.cfg.xmla file under .

Default mode configuration requirements:

  • The directory and file name cannot be modified, because Activitithe source code has been written to read a file with a fixed file name in a fixed directory.
  • The name in is called , activiti.cfg.xmldo not modify this name.beanprocessEngineConfiguration
<?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/contex
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="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql:///activiti" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
        <property name="maxActive" value="3" />
        <property name="maxIdle" value="1" />
    </bean>

    <bean id="processEngineConfiguration"
          class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!-- 引用数据源 上面已经设置好了-->
        <property name="dataSource" ref="dataSource" />
        <!-- activiti数据库表处理策略 -->
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>
    
</beans>

3. Generate 25 tables in Activiti

3.1 Use the default method

It is the way to use the default configuration file (configuration file name and location cannot be modified) and smooth engine class (bean name cannot be modified).

public class TestActiviti {
    
    

    @Test
    public void testCreateDbTable() {
    
    
        // 使用classpath下的activiti.cfg.xml中的配置创建processEngine
        // 而resources下的文件就是处于classpath下的
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		System.out.println(processEngine);
    }
}

3.2 Using a custom method

The name of the configuration file can be customized, and the name of the bean can also be customized.

But the following code still writes the same configuration file name and bean as the original default.

public class TestActiviti {
    
    

    @Test
    public void testCreateDbTable() {
    
    
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.
                createProcessEngineConfigurationFromResource("activiti.cfg.xml", "processEngineConfiguration");
		// 获取流程引擎对象(此时就会创建数据库)
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
    }
}

Fourth, table structure analysis

After executing the above code, a table will be activiticreated in the local database.25
insert image description here

4.1 Description of table naming rules

  • ACT : ACTIn fact Activiti, it means that these tables are all Activitirelated tables.

  • ACT_RE : RERepresentation repository. Tables with this prefix contain process definitions and process static resources (images, rules, etc.).

  • ACT_RU : RUIndicates runtime. These runtime tables contain running data such as process instances, tasks, variables, asynchronous tasks, and so on.

    Activiti​This data is only

    ​ In this way, the runtime table can always be small and fast.

  • ACT_HI : HIIndicates history. These tables contain historical data such as historical process instances, variables, tasks, etc.

  • ACT_GE : GEIndicates general. Common data, used in different scenarios

4.2 Description of Activiti data table

table classification Table Name explain
general data
[ACT_GE_BYTEARRAY] Generic process definitions and process resources
[ACT_GE_PROPERTY] system related properties
Process History
[ACT_HI_ACTINST] Historical Process Examples
[ACT_HI_ATTACHMENT] Historical Process Attachments
[ACT_HI_COMMENT] historical descriptive information
[ACT_HI_DETAIL] Detailed information on historical process runs
[ACT_HI_IDENTITYLINK] User relationship during historical process running
[ACT_HI_PROCINST] Historical Process Examples
[ACT_HI_TASKINST] Examples of historical tasks
[ACT_HI_VARINST] Variable information in the historical process running
Process definition table
[ACT_RE_DEPLOYMENT] Deployment unit information
[ACT_RE_MODEL] model information
[ACT_RE_PROCDEF] Deployed Process Definition
run instance table
[ACT_RU_EVENT_SUBSCR] runtime events
[ACT_RU_EXECUTION] Runtime process execution instance
[ACT_RU_IDENTITYLINK] Runtime user relationship information, storing information about task nodes and participants
[ACT_RU_JOB] runtime job
[ACT_RU_TASK] runtime task
[ACT_RU_VARIABLE] runtime variable table

Five, Activiti's basic class relationship

In , two classes have been Activiti7deleted . Yes engine profile.IdentityServiceFormServiceSerivce
insert image description here
activiti.cfg.xmlActiviti

Includes ProcessEngineConfigurationdefinitions, data source definitions, transaction managers, etc. Equivalent to a Springconfiguration file.

ProcessEngineConfigurationcan be used to create ProceccEngine.

AndProceccEngine you can get Activitivarious cores in management Service.

5.1 ProcessEngineConfiguration

Process engine configuration class ( ProcessEngineConfiguration), through which ProcessEngineConfigurationworkflow engines can be created ProceccEngine.

There are also two ways to create processEngineConfiguration:

  • Method 1: Fixed name

    activiti.cfg.xmlThere must be one processEngineConfigurationof the code requirements below bean.

    ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml")
    
  • Method 1: Customize the name

    activiti.cfg.xmlThe configuration file name can be changed, and the name processEngineConfigurationof the configuration beanfile can be changed.

    ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(String resource, String beanName);
    

5.2 ProcessEngineConfiguration

Workflow engine ( ProcessEngine), which is equivalent to a unified management interface.

By ProcessEngineConfigurationcreating processEngine, by ProcessEnginegetting each Serviceinterface.

  • Method 1: Fixed name

    activiti.cfg.xmlThere must be one processEngineConfigurationof the code requirements below bean.

    // 直接使用工具类 ProcessEngines,使用classpath下的activiti.cfg.xml中的配置创建processEngine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    System.out.println(processEngine);
    
  • Method 1: Customize the name

    activiti.cfg.xmlThe configuration file name can be changed, and the name processEngineConfigurationof the configuration beanfile can be changed.

    //先构建ProcessEngineConfiguration
    ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    //通过ProcessEngineConfiguration创建ProcessEngine,此时会创建数据库
    ProcessEngine processEngine = configuration.buildProcessEngine();
    

5.3 Servcie service interface

Various Serviceservice interfaces are service interfaces provided by the workflow engine for workflow deployment, execution, and management.

Using these interfaces can be the data table corresponding to the operation service.

1) service overview

service name service role
RepositoryService activiti's resource management class
RuntimeService Activiti's process operation management class
TaskService activiti's task management class
HistoryService Activiti's history management class
ManagerService Activiti's engine management class
  • RepositoryService

    ActivitiThe resource management class provides operations for managing and controlling process release packages and process definitions.

    A business flowchart designed with a workflow modeling tool needs to use this service to deploy the contents of the process definition file to the computer.

    In addition to deploying process definitions, you can also query release packages and process definitions in the engine, and pause or activate release packages, corresponding to all and specific process definitions.

    Suspending means they can no longer perform any operations, activating is the corresponding reverse operation. Access to various resources, such as files included in the distribution package, or flowcharts automatically generated by the engine.

    Gets a version of the process definition pojothat can be used to Javaparse the process without passing through xml.

  • RuntimeService

    ActivitiThe process operation management class. You can get a lot of information about process execution from this service class

  • TaskService

    Activititask management class. Task information can be obtained from this class.

  • HistoryService

    ActivitiThe history management class can query historical information. When executing a process, the engine will save a lot of data (according to the configuration), such as the start time of the process instance, the participants of the task, the time to complete the task, the execution path of each process instance, etc. . This service mainly obtains these data through the query function.

  • ManagementService

    ActivitiThe engine management class provides management and maintenance functions for Activitithe process engine. These functions are not used in workflow-driven applications, but are mainly used for Activitidaily maintenance of the system.

2) Service creation method

Usually by ProcessEnginecreating various types Service, just take as RuntimeServicean example, and the rest Serviceare the same.

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

ce**

ActivitiThe engine management class provides management and maintenance functions for Activitithe process engine. These functions are not used in workflow-driven applications, but are mainly used for Activitidaily maintenance of the system.

2) Service creation method

Usually by ProcessEnginecreating various types Service, just take as RuntimeServicean example, and the rest Serviceare the same.

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

Guess you like

Origin blog.csdn.net/qq_44749491/article/details/130198454