Activiti7 Workflow 1 [Workflow introduction, what is Activiti7? , Activiti7 environment, integrated Activiti7, process engine API]


Activiti7 workflow

1. Introduction to workflow

1.1 Concept

Workflow refers to the automated execution and management of business processes through computers. It mainly solves the problem of "automating the process of transferring documents, information or tasks between multiple participants according to certain predefined rules, so as to achieve an expected business goal, or to promote the realization of this goal."

Case: Business trip expense reimbursement

Goal: The company transfers business trip expenses to its own account.

Participants: Applicant, department head, general manager, finance

Insert image description here

Pass files/execute tasks

Insert image description here

What is a workflow system ?

A software system that has workflow functions is called a workflow system.

1.2 Applicable industries

Consumer goods industry, manufacturing, telecommunications services, financial services such as banking, securities and insurance, logistics services, property services, property management, large and medium-sized import and export trading companies, government institutions, research institutes and education services, etc., especially It is a large multinational enterprise and group company.

1.3 Application areas

Enterprise: Procurement process, contract review process

Customer: Customer Telephone Complaint Handling Process

In life: housing loan approval process, applying for ID card, and handling childbirth procedures

Administration: business trip approval, reimbursement process, leave approval, car use process, conference room application

Banking industry: credit approval, credit card issuance approval

Personnel: employee training, performance appraisal, job changes

1.4 Traditional implementation methods

We can think about it, if we need to implement the [ Business Travel Expense Reimbursement ] workflow, how should the code be implemented?
Insert image description here

1. When initiating the application, you need to select which person will review and approve each stage (database needs to record)

2. We need to design a status field in the database [supervisor approval 0, general manager approval 1, financial approval 2]. After the supervisor approval is passed, the status is changed to general manager approval. (hard-coded method)

3. When the process is under supervisor approval, only users with the supervisor role can see this audit data.

Through the above description, students will find that if we want to implement this process control, we need a lot of code to complete it. [Heavy workload]

And if the process changes, the code we write also needs to be adjusted [inflexible]

1.5 What is a workflow engine

It is deployed according to predefined rules [need to comply with BPMN specifications], and separates the processes of business and nodes [associated in specific forms].

A workflow framework that implements automatic node transfer.

Insert image description here

1. The scheduled process file BPMN needs to be deployed into the workflow engine, and the node and path information will be stored in the database.

2. The workflow engine provides a large number of APIs to query and process the process, and the details are shielded from the application, greatly improving development efficiency.

3. Business logic processing and process flow are separated and related through BusinessKey.

2. What is Activiti7?

2.1 Overview

  • Alfresco Software announced the official launch of the Activiti business process management (BPM) open source project on May 17, 2010. Its chief architect is Tom Baeyens, an expert in
    business process management BPM. Tom Baeyens is the architect of the original jbpm,
    and jbpm is A very famous workflow engine, of course activiti is also a workflow engine.
  • Activiti is a workflow engine. Activiti can extract complex business processes from the business system and define them using a special
    modeling language (BPMN2.0). The business system executes according to the pre-defined process, realizing the business of the business system.
    The process is managed by activiti, which reduces the workload of system upgrades and transformations due to process changes in the business system, thus improving
    the robustness of the system and reducing system development and maintenance costs.

Official website: ·https://www.activiti.org·

2.2 Activiti7 internal core mechanism

  • 1️⃣Business process diagrams must be standardized and must comply with a set of standards.
  • 2️⃣A business process diagram is essentially an XML file, and XML can store the required data.
  • 3️⃣The process of reading the business flow chart is the process of parsing the XML file.
  • 4️⃣Reading a node of a business flow chart is equivalent to parsing an XML node, and further inserting the data into the MySQL table to form a record.
  • 5️⃣Read and store all nodes of a business flow chart into the MySQL table.
  • 6️⃣As long as you read the records in the MySQL table later, it is equivalent to reading a node in the business flow chart.
  • 7️⃣As the business process progresses, it will be converted to reading the data in the table and processing the data. At the end, this row of data can be deleted.

2.3 BPMN

  • BPMN (Business Process Model And Notation), business process model and notation, is a set of business process modeling symbols developed by BPMI (Business Process Management Initiative). Business processes can be created using the symbols provided by BPMN. The BPMN1.0 specification was released in May 2004. BPMI was merged into the OMG (The Object Management Group) organization in September 2005. OMG released the final version of BPMN2.0 in January 2011.

  • Activit uses BPMN2.0 for process modeling and process execution management. It includes many modeling symbols.

  • An example of BPMN:

    1. The party concerned fills out the leave request form, and after starting the process, the leave request form ID is bound to the process;
    2. Department managers review leave requests;
    3. Then the HR manager will review and file;
    4. Finally, the leave application process ends.

Insert image description here

Thinking: What we see is a picture (png), but how does the application know where the next node should flow? Does it need to parse the png file?

  • BPMN actually uses XML to represent business processes. The above .bpmn file is opened with a text editor:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1620716847764" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
      <process id="leave" isClosed="false" isExecutable="true" processType="None">
        <startEvent id="_2" name="StartEvent"/>
        <userTask activiti:candidateUsers="lisi,wangwu" activiti:exclusive="true" id="_4" name="部门经理审批"/>
        <userTask activiti:assignee="rose" activiti:exclusive="true" id="_6" name="人事复批"/>
        <endEvent id="_7" name="EndEvent"/>
        <sequenceFlow id="_12" sourceRef="_6" targetRef="_7"/>
        <sequenceFlow id="_13" sourceRef="_2" targetRef="_4"/>
        <sequenceFlow id="_14" sourceRef="_4" targetRef="_6"/>
      </process>
      <bpmndi:BPMNDiagram documentation="background=#FFFFFF;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
        <bpmndi:BPMNPlane bpmnElement="leave">
          <bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">
            <omgdc:Bounds height="32.0" width="32.0" x="175.0" y="185.0"/>
            <bpmndi:BPMNLabel>
              <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4">
            <omgdc:Bounds height="55.0" width="85.0" x="245.0" y="175.0"/>
            <bpmndi:BPMNLabel>
              <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNShape bpmnElement="_6" id="Shape-_6">
            <omgdc:Bounds height="55.0" width="85.0" x="380.0" y="175.0"/>
            <bpmndi:BPMNLabel>
              <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNShape bpmnElement="_7" id="Shape-_7">
            <omgdc:Bounds height="32.0" width="32.0" x="505.0" y="185.0"/>
            <bpmndi:BPMNLabel>
              <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNEdge bpmnElement="_13" id="BPMNEdge__13" sourceElement="_2" targetElement="_4">
            <omgdi:waypoint x="207.0" y="201.0"/>
            <omgdi:waypoint x="245.0" y="202.5"/>
            <bpmndi:BPMNLabel>
              <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNEdge>
          <bpmndi:BPMNEdge bpmnElement="_12" id="BPMNEdge__12" sourceElement="_6" targetElement="_7">
            <omgdi:waypoint x="465.0" y="202.5"/>
            <omgdi:waypoint x="505.0" y="201.0"/>
            <bpmndi:BPMNLabel>
              <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNEdge>
          <bpmndi:BPMNEdge bpmnElement="_14" id="BPMNEdge__14" sourceElement="_4" targetElement="_6">
            <omgdi:waypoint x="330.0" y="202.5"/>
            <omgdi:waypoint x="380.0" y="202.5"/>
            <bpmndi:BPMNLabel>
              <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
      </bpmndi:BPMNDiagram>
    </definitions>
    

2.4 How to use Activiti

2.4.1 Integrate Activiti

  • Activiti is a workflow engine. Business systems use Activiti to automatically manage the system's business processes. In order to facilitate the business system to access (operate) Activiti's interfaces or functions, Activiti is usually integrated with the business system environment.

2.4.2 Business process modeling

  • Use the Activiti process modeling tool (Activity-designer) to define the business process (.bpmn file).
  • The .bpmn file is a business process definition file, which defines business processes through xml.
  • If you use work engines developed by other companies, they generally provide a visual modeling tool (Process Designer) for generating process definition files. The modeling tools are intuitive to operate and generally support graphical drag-and-drop methods, multi-window user interfaces, and rich Process graphic elements, process elements copy, paste, delete and other functions.

2.4.3 Deploy business process

  • Deploy the business process definition (.bpmn file) to Activiti.
  • Use the API provided by Activiti to deploy the .bpmn file to Activiti (generally, you also need to deploy the picture .png of the business process together).

2.4.4 Start process instance

  • Starting a process instance means starting a business process. For example, the employee leave process deployment is completed. If Zhang San wants to ask for leave, he can start a process instance. If Li Si wants to ask for leave, he also needs to start a process instance. The execution of the two processes are independent of each other. The impact is like defining a Java class and instantiating two Java objects. The deployment process is like a Java class, and starting a process instance is like new a Java object.

2.4.5 Query to-do tasks

  • Because the business process of the system has been handed over to Activiti for management, through Activiti we can query where the current process is executed and what tasks the current user needs to handle. Activiti helps us manage these, unlike the traditional method that requires us to use SQL statements. The WHERE condition in specifies the status value of the current query.

2.4.6 Process to-do tasks

  • After the user queries the to-do tasks, he can handle a certain task. If the task is completed, other users need to handle it. For example, after the purchase order is created, it will be reviewed by the department manager. This process is also completed by Activiti for us, and we do not need to add code to it. The next task handler is hard-coded.

2.4.7 End process

  • When the task processing is completed and there is no next task/node, the process instance is completed.

3. Activiti7 environment

3.1 Development environment

Jdk1.8 or above version

Mysql 5 and above versions

Tomcat8.5

IDEA

Activate 7.0.0.SR1

3.2 Install Activiti process designer

3.2.2 Local installation

  1. Download the package from the IDEA official website actiBPM.jar, download address: https://plugins.jetbrains.com/plugin/7429-actibpm/versions

  2. Install actiBPM locally

    The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

    Click Install plugin from diskand select the downloaded one actiBPM.jar, then restart IDEA.

3.3 Solve actiBPM Chinese garbled characters

  • Change File–>Settings–>Editor–>File Encodings to UTF-8 in IDEA

Insert image description here

  • Add at the end of IDEA's Help–>Edit Custom VM Options-Dfile.encoding=UTF-8

Insert image description here

Insert image description here

  • Add the two files idea.exe.vmoptions and idea64.exe.vmoptions to the end of the bin directory of the IDEA installation directory.-Dfile.encoding=UTF-8

    Insert image description here

  • Just restart IDEA.

3.4 Databases supported by Activiti

  • The operation of Activiti requires database support, which is supported as follows:

    • h2
    • MySQL
    • Oracle
    • Db2
    • postgres
    • mssql
  • The database created in the Navicat tool activitiis used for subsequent experiments.

4. Integrate Activiti7

4.1 Create Maven project

  • Create a Maven Java project through IDEA.

4.2 Add Maven dependencies

Add the jar packages required by ProcessEngine to the Java project, including:

  • activities-engine-7.0.0.GA.jar
  • Jar packages that activiti depends on: mybatis, slf4j, log4j, etc.
  • The spring jar package that activiti depends on
  • Database driven
  • Third-party database connection pool dbcp
  • unit test junit

pom.xmldocument

    <properties>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <activiti.version>7.0.0.SR1</activiti.version>
    </properties>
    <dependencies>
        <!-- activiti引擎 -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <!-- 整合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>
        <!-- mysql驱动 -->
        <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>
        <!-- 链接池 -->
        <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>
        <!-- log end -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>
    </dependencies>

4.3 Add log4j log configuration

We use the log4j log package to configure the log and create it resourcesbelow log4j.properties. The content is as follows:

# 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=./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

4.4 Add core configuration file

  • resourceA configuration file needs to be added to the directory activiti.cfg.xmlwith the following content:
<?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">
</beans>
  • The following information needs to be configured in the configuration file
    • Database connection pool
    • ProcessEngine configuration for Activiti to run alone
<?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">
    <!--数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql:///activiti" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>
    <!-- 默认id对应的值 为processEngineConfiguration -->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource"/>
        <!--
            activiti数据库表处理策略
                false(默认值):检查数据库的版本和依赖库的版本,如果不匹配就抛出异常
                true:构建流程引擎时,执行检查,如果需要就执行更新。如果表不存在,就创建。
                create-drop:构建流程引擎时创建数据库报表,关闭流程引擎时就删除这些表。
                drop-create:先删除表再创建表。
                create:构建流程引擎时创建数据库表,关闭流程引擎时不删除这些表
        -->
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>
</beans>

4.5 Testing

  • Create a test class, call Activiti's tool class, and generate the database tables required by Activiti. code show as below:
package cn.wolfcode.demo;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.junit.Test;

public class _01TestInit {
    
    
    @Test
    public void testInit(){
    
    
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);
    }
}

4.6 Naming rules for database tables

  • At this point we check the database and find 25 tables. The results are as follows:

Insert image description here

Activiti's tables all ACT_start with. The second part is a two-letter designation that indicates the purpose of the table. The usage also corresponds to the API of the service.

  • ACT_RE_*: 'RE' means Repository. The table with this prefix contains the process definition and process static resources (pictures, rules, etc.).
  • ACT_RU_*: 'RU' means Runtime. These runtime tables contain running data such as process instances, tasks, variables, asynchronous tasks, etc. Activiti only saves this data during the execution of the process instance, and deletes these records when the process ends. These runtime tables can remain small and fast.
  • ACT_HI_*: 'HI' means History. These tables contain historical data, such as historical process instances, variables, tasks, etc.
  • ACT_GE_*: 'GE' means General. General data, used in different scenarios.

5. Process Engine API

Insert image description here

ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

This line of code reads the file resourcein the directory by default activiti.cfg.xml. This file is essentially the spring configuration file. After loading, processEngineConfigurationthe bean with the id name in the configuration file will be read. Through this configuration object, the process engine object can be obtained.ProcessEngine

5.1 Service Overview

Service interface illustrate
RepositoryService Activiti’s resource management interface
RuntimeService Activiti’s process operation management interface
TaskService Activiti’s task management interface
HistoryService Activiti’s history management interface
ManagementService Activiti’s engine management interface
  • RepositoryService is Activiti's resource management interface, which provides operations for managing and controlling process release packages and process definitions. Business process diagrams designed using workflow modeling tools need to use this Service to deploy the contents of the process definition file to the computer.

  • RuntimeService is Activiti's process operation management interface. You can obtain a lot of information about process execution from this interface.

  • TaskService is the task management interface of Activiti. You can obtain task information from this interface.

  • HistoryService is Activiti's history management class. It can query historical information. When executing a process, the engine will contain a lot of data (according to configuration), such as process instance startup time, task participants, task completion time, and execution of each process instance. path, etc.

  • ManagementService is the engine management interface of Activiti. It provides management and maintenance functions for the Activiti process engine. These functions are not used in workflow-driven applications and are mainly used for the daily maintenance of the Activiti system.

Guess you like

Origin blog.csdn.net/m0_52896752/article/details/133205022