Activiti is the most detailed series of articles on the web.

Activiti for starters

Basic use of 1Activiti

1.1 Creating a Maven project

Create a normal Maven project and add related dependencies

    <properties>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <activiti.version>7.0.0. Walk</activiti.version>
    </properties>
    <dependencies>
        <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>
        <! -- BPMN model processing -->
        <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 data conversion -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-json-converter</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <! -- BPMN layout -->
        <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>
        <! -- Activiti Cloud support -->
        <dependency>
            <groupId>org.activiti.cloud</groupId>
            <artifactId>activiti-cloud-services-api</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <! -- Mysql driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <! -- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <! -- Link pool -->
        <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>
Copy the code

1.2 the log4j

Add a log file log4j.properties

# 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=d:\log\act\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
Copy the code

1.3 Adding an Activiti Profile

The database we use in this case is mysql8.0.

The default way to use Activiti requires you to create an activiti.cfg. XML file under Resources. The default way name cannot be changed.

There are two ways to configure the data source in the configuration file: one is to configure the data source separately, and the other is not to configure the data source separately


      
<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 class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration" id="processEngineConfiguration">
        <property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="jdbcUrl" value="jdbc:mysql:///activiti2? characterEncoding=utf-8&amp;nullCatalogMeansCurrent=true&amp;serverTimezone=UTC" />

        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="123456" />
        <property name="databaseSchemaUpdate" value="true" />
        <! --<property name="dataSource" ref="dataSource" />-->
    </bean>
    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
                <property name="url" value="jdbc:mysql:///activiti2? characterEncoding=utf-8&amp;nullCatalogMeansCurrent=true&amp;serverTimezone=UTC" />

        <property name="username" value="root"/>
        <property name="password" value="123456"/>
        <property name="maxActive" value="3" />
        <property name="maxIdle" value="2" />
    </bean>
</beans>
Copy the code

1.4 Java program generates table structure

Create a utility class that calls Activiti’s utility class to generate the table structure Activiti needs

public class Test01 {

    /** * Generate Activiti related table structures */
    @Test
    public void test01(a){
        // Use the configuration in activiti.cfg.xml under CLASspath to create the ProcessEngine objectProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); System.out.println(engine); }}Copy the code

Effect of executionAfter executing, we look at the database and create 25 tables. The result is as follows:

2 Table structure

2.1 Naming rules and functions of tables

Looking at the tables we just created, we see that Activiti’s tables all start with ACT_.

The second part is a two-letter identifier that indicates the purpose of the table. The purpose also corresponds to the service API. ACT_RE: ‘RE’ stands for repository. This prefixed table contains process definitions and process static resources (images, rules, and so on). ACT_RU: ‘RU’ stands for runtime. These run-time tables contain running data for process instances, tasks, variables, asynchronous tasks, and more. Activiti saves this data only during the process instance execution and deletes the records at the end of the process. This way the table can stay very small and very fast at runtime. ACT_HI: ‘HI’ stands for history. These tables contain historical data, such as historical process instances, variables, tasks, and so on. ACT_GE: GE stands for general. Common data used in different scenarios

2.2 Activiti data table Introduction

Classification table The name of the table explain
General data
[ACT_GE_BYTEARRAY] Common process definitions and process resources
[ACT_GE_PROPERTY] System related Attributes
Process history
[ACT_HI_ACTINST] Historical process instances
[ACT_HI_ATTACHMENT] Historical process attachments
[ACT_HI_COMMENT] Historical illustrative information
[ACT_HI_DETAIL] Details of the process execution in history
[ACT_HI_IDENTITYLINK] History of user relationships during process runs
[ACT_HI_PROCINST] Historical process instances
[ACT_HI_TASKINST] Historical task instances
[ACT_HI_VARINST] Historical information about variables in a process run
Process definition table
[ACT_RE_DEPLOYMENT] Deployment Unit Information
[ACT_RE_MODEL] Model information
[ACT_RE_PROCDEF] Deployed process definitions
Run instance table
[ACT_RU_EVENT_SUBSCR] Run time event
[ACT_RU_EXECUTION] Runtime process execution instance
[ACT_RU_IDENTITYLINK] Runtime user relationship information, which stores information about task nodes and participants
[ACT_RU_JOB] Run time job
[ACT_RU_TASK] Runtime task
[ACT_RU_VARIABLE] Run time varying meter

3 ProcessEngine creation method

The getDefaultProcessEngine() method was used to load the activiti.cfg.xml file in the classpath. What should we do if we don’t use the default method?

    /** * Custom way to load configuration file */
    @Test
    public void test02(a){
        / / create ProcessEngineConfiguration object in the first place
        ProcessEngineConfiguration configuration =
                ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        / / to create ProcessEngine object by ProcessEngineConfiguration object
        ProcessEngine processEngine = configuration.buildProcessEngine();
    }
Copy the code

4 Servcie service interface

Service is the Service interface provided by the workflow engine for workflow deployment, execution and management. We use these interfaces to operate the corresponding data table of the Service

4.1 Service Creation Mode

Create the Service from ProcessEngine

The method is as follows:

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

4.2 the Service overview

The name of the service The service function
RepositoryService Activiti’s resource management class
RuntimeService Activiti’s process run management class
TaskService Activiti’s task management class
HistoryService Activiti’s history management class
ManagerService Activiti’s engine management class

Brief introduction:

RepositoryService

Activiti resource Management class provides management and control of process distribution packages and process-defined operations. A business flowchart designed using a workflow modeling tool requires the service to deploy the contents of the process definition file to a computer.

In addition to deploying process definitions, you can: query the release packages and process definitions in the engine.

Suspend or activate the distribution, corresponding to all and specific process definitions. Pause means they can’t do anything anymore, and activate is the reverse. Get multiple resources, such as files included in a release package, or flow charts automatically generated by the engine.

Get a POJO version of the process definition that can be used to parse the process through Java rather than XML.

RuntimeService

Activiti’s process run management class. You can get a lot of information about process execution from this service class

TaskService

Activiti’s task management class. You can get information about tasks from this class.

HistoryService

Activiti’s history management class allows you to query historical information. When a process is executed, the engine stores a lot of data (depending on the configuration), such as when the process instance started, who participated in the task, when the task was completed, the execution path of each process instance, and so on. The service obtains this data mainly through the query function.

ManagementService

Activiti’s Engine Management class provides management and maintenance capabilities for the Activiti process engine, which are not used in workflow-driven applications and are primarily used for routine maintenance of the Activiti system.

5 Process Drawing

5.1 Drawing Plug-ins

Since Idea has not updated the design tools that maintain Activiti after 2019, we cannot use the actiBPM plugin to draw in higher versions of Idea. We can either use the lower version or use Eclipse to design the process.The Eclipse we offer you is already integrated with the Activiti plug-in.

Create an Activiti project

5.2 Drawing Process

Using a slide board to draw the flow, drag the icon from the right to the left panel for the final effect

Specify the primary key of the processAssign the person in charge of the task

Specify the owner of each task node in the Properties view:Manager approval: Lisi

Approved by general manager: wangwu

Financial approval: Xiaoming

When we save the file after setting up, a PNG image will be generated

Then copy these two files into the IDEA project

5.3 Icon Introduction

The process symbol

BPMN 2.0 stands for Business Process Modeling Notation 2.0.

It was created and developed by the Business Process Management Initiative, a non-profit association. As an identifier, BPMN 2.0 is a set of symbolic specifications that use notations to clarify business process design flowcharts to improve communication efficiency during business modeling.

Currently BPMN2.0 is the latest version and is used for layout and visual communication in a BPM context.

Let’s start by looking at symbols that are common in process design.

The basic conformance of BPMN2.0 mainly includes:

Events in the Event

Activities in the Activity

Activity is a generic term for a job or task. An activity can be a task or a subprocess of the current process; Second, you can specify different types of activities. Common activities are as follows:

The GateWay of GateWay

Gateways are used to process decisions. There are several common gateways to know:

Exclusive gateway (x)**

Only one path will be selected. When the flow reaches the gateway, the flow is calculated one by one according to the sequence of output streams. When the calculation result of the condition is true, the flow of the current gateway is continued to be executed.

If multiple lines are evaluated to true, the first line with the value true is executed. The engine throws an exception if none of the gateways evaluates to true.

The exclusive gateway needs to be used in conjunction with conditional order flow. The default attribute specifies the default order flow that will be executed when all conditions are not met.

Parallel gateway (+)

All paths are selected at the same time

Split – Executes all output sequential streams in parallel, creating a parallel execution line for each sequential stream.

Merge – All lines split and executed from the parallel gateway wait here until all lines are executed.

Inclusive Gateway (+)

You can execute multiple lines at the same time, or you can set conditions on the gateway

Split – Evaluates the expression on each line, and when the expression evaluates to true, creates a parallel line and continues execution

Merge – All lines split and executed from the parallel gateway wait here until all lines are executed.

Event Gateway (+)

Specifically set for intermediate capture events, allowing multiple output streams to be set to point to multiple different intermediate capture events. When the process executes to the event gateway, the process is in a wait state and needs to wait for events to be thrown to change the wait state to an active state.

Flow to the Flow

A flow is a wire connecting two process nodes. Common flows include the following:

Process designer use

Palette

Connection – the Connection

The Event – events

Task – Task

Gateway – a Gateway

The Container, the Container

Boundary event – Boundary event

Intermediate event- – An Intermediate event

The basic application of ~ Activiti is introduced here, the next chapter begins to explain how to use it in detail. Welcome to focus on zan plus collection oh V_V