I. Overview
Activiti
It is a workflow engine that Activiti
can extract complex business processes in the business system and BPMN2.0
define them with a special modeling language. The business processes are executed according to the pre-defined processes, which realizes the Activiti
management 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 Activiti
understanding the basics.
2.1 Create a Maven project
After entering the basic information of the project, click below Finish
to create a basic Maven
project.
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:jgraphx
that 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 log4j
of coordinates, it is necessary to add a configuration file Maven
in the project directory.resources
log4j.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.File
The configured value is under the corresponding path F:\activiti\activiti.log
on F
the disk, and activiti.log
the file needs to exist in advance, otherwise an exception will be reported that the file cannot be found.
2.4 Add Activiti configuration file
Activiti
The provided default way to create MySQL
table requirements resources
is to create activiti.cfg.xml
a file under .
Default mode configuration requirements:
- The directory and file name cannot be modified, because
Activiti
the 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.xml
do not modify this name.bean
processEngineConfiguration
<?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 activiti
created in the local database.25
4.1 Description of table naming rules
-
ACT :
ACT
In factActiviti
, it means that these tables are allActiviti
related tables. -
ACT_RE :
RE
Representationrepository
. Tables with this prefix contain process definitions and process static resources (images, rules, etc.). -
ACT_RU :
RU
Indicatesruntime
. 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 :
HI
Indicateshistory
. These tables contain historical data such as historical process instances, variables, tasks, etc. -
ACT_GE :
GE
Indicatesgeneral
. 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 Activiti7
deleted . Yes engine profile.IdentityService
FormService
Serivce
activiti.cfg.xml
Activiti
Includes ProcessEngineConfiguration
definitions, data source definitions, transaction managers, etc. Equivalent to a Spring
configuration file.
ProcessEngineConfiguration
can be used to create ProceccEngine
.
AndProceccEngine
you can get Activiti
various cores in management Service
.
5.1 ProcessEngineConfiguration
Process engine configuration class ( ProcessEngineConfiguration
), through which ProcessEngineConfiguration
workflow engines can be created ProceccEngine
.
There are also two ways to create processEngineConfiguration:
-
Method 1: Fixed name
activiti.cfg.xml
There must be oneprocessEngineConfiguration
of the code requirements belowbean
.ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml")
-
Method 1: Customize the name
activiti.cfg.xml
The configuration file name can be changed, and the nameprocessEngineConfiguration
of the configurationbean
file can be changed.ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(String resource, String beanName);
5.2 ProcessEngineConfiguration
Workflow engine ( ProcessEngine
), which is equivalent to a unified management interface.
By ProcessEngineConfiguration
creating processEngine
, by ProcessEngine
getting each Service
interface.
-
Method 1: Fixed name
activiti.cfg.xml
There must be oneprocessEngineConfiguration
of the code requirements belowbean
.// 直接使用工具类 ProcessEngines,使用classpath下的activiti.cfg.xml中的配置创建processEngine ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); System.out.println(processEngine);
-
Method 1: Customize the name
activiti.cfg.xml
The configuration file name can be changed, and the nameprocessEngineConfiguration
of the configurationbean
file can be changed.//先构建ProcessEngineConfiguration ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml"); //通过ProcessEngineConfiguration创建ProcessEngine,此时会创建数据库 ProcessEngine processEngine = configuration.buildProcessEngine();
5.3 Servcie service interface
Various Service
service 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:
Activiti
The 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
pojo
that can be used toJava
parse the process without passing throughxml
. -
RuntimeService
Activiti
The process operation management class. You can get a lot of information about process execution from this service class -
TaskService
Activiti
task management class. Task information can be obtained from this class. -
HistoryService
Activiti
The 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
Activiti
The engine management class provides management and maintenance functions forActiviti
the process engine. These functions are not used in workflow-driven applications, but are mainly used forActiviti
daily maintenance of the system.
2) Service creation method
Usually by ProcessEngine
creating various types Service
, just take as RuntimeService
an example, and the rest Service
are the same.
RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService();
ce**
Activiti
The engine management class provides management and maintenance functions for Activiti
the process engine. These functions are not used in workflow-driven applications, but are mainly used for Activiti
daily maintenance of the system.
2) Service creation method
Usually by ProcessEngine
creating various types Service
, just take as RuntimeService
an example, and the rest Service
are the same.
RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService();