Article Directory
1. Prepare the development environment
1.1. Understand the activiti development library
1.2. Create a WEB project and import the Activiti dependency package
Right-click New in the work bar on the left side of IDEA and select Create Maven Project, create a project named ActivitiDemo, and
click Finish to complete.
Then add the following dependencies in the pom.xml file
<dependencies>
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-engine -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.22.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.6</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<version>3.1</version>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
1.3. Initialize the table structure
1.3.1. Use the table creation statement provided by the activiti framework
Step 1 : Obtain the table creation statement
Step 2 : Create the database
Step 3 : Execute SQL statement
1.3.2. Use the automatic table creation function of the activiti framework
The framework provides automatic table
creation function similar to hibernate Do not use configuration files (not recommended)
/*
* 使用框架提供自动建表(不提供配置文件)
*/
@Test
public void createTable(){
//创建一个流程引擎配置对象
ProcessEngineConfiguration conf = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
//设置数据源信息
conf.setJdbcDriver("com.mysql.jdbc.Driver");
conf.setJdbcUrl("jdbc:mysql:///activitidb?characterEncoding=utf-8");
conf.setJdbcUsername("root");
conf.setJdbcPassword("123");
//设置自动建表
conf.setDatabaseSchemaUpdate("true");
//创建一个流程引擎对象,在创建流程引擎对象过程中会自动建表
ProcessEngine processEngine = conf.buildProcessEngine();
}
1.3.3. Using configuration files
The name of the configuration file must be activiti-context.xml
or activiti.cfg.xml
, and the configuration information must be
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置流程引擎配置对象 -->
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:///activitidb?characterEncoding=utf-8" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="123" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>
@Test
public void createTable1(){
ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-context.xml","processEngineConfiguration").buildProcessEngine();
System.out.println("------processEngine----:" + processEngine);
}
1.3.4. Use the default configuration file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置流程引擎配置对象 -->
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:///activitidb?characterEncoding=utf-8" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="123" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
<!-- 配置一个流程引擎工厂bean,用于创建流程引擎对象 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<!-- 通过set方法注入流程引擎配置对象 -->
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
</beans>
@Test
public void createTable2(){
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println(processEngine);
}
Then run the test method, if it runs successfully, 25 Activiti related data tables should be generated in the database
1.3.5. Activiti table description
The following is a summary of several commonly used data tables
The backend of Activiti is supported by a database, and all tables start with ACT_ . The second part is a two-letter logo that indicates the purpose of the table. The usage also corresponds to the API of the service.
ACT_RE_*
:'RE' stands for repository. The prefix table contains process definitions and process static resources (pictures, rules, etc.).
ACT_RU_*
:'RU' means runtime. These runtime tables contain process instances, tasks, variables, asynchronous tasks, and other running data. Activiti only saves these data during the execution of the process instance, and deletes these records at the end of the process. This way the runtime table can always be small and fast.
ACT_ID_*
:'ID' means identity. These tables contain identity information, such as users, groups, and so on.
ACT_HI_*
:'HI' means history. These tables contain historical data, such as historical process instances, variables, tasks, and so on.
ACT_GE_*
: General data, used in different scenarios.
Process deployment related tables
act_re_deployement 部署对象表
act_rep_procdef 流程定义表
act_ge_bytearray 资源文件表
act_ge_prperty 主键生成策略表(对于部署对象表的主键ID)
Process instance related table
act_ru_execution 正在执行的执行对象表(包含执行对象ID和流程实例ID,如果有多个线程可能流程实例ID不一样)
act_hi_procinst 流程实例历史表
act_hi_actinst 存放历史所有完成的任务
Task related table
act_ru_task 代办任务表 (只对应节点是UserTask的)
act_hi_taskinst 代办任务历史表 (只对应节点是UserTask的)
act_hi_actinst 所有节点活动历史表 (对应流程的所有节点的活动历史,从开始节点一直到结束节点中间的所有节点的活动都会被记录)
Process variable table
act_ru_variable 正在执行的流程变量表
act_hi_variable 流程变量历史表
2. Prepare the core API of the development environment
2.1.ProcessEngine object process engine object
Description:
-
The core class in Activiti, all other classes are derived from him.
-
production methods
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
- Can generate RepositoryService
RepositoryService repositoryService =processEngine.getRepositoryService();
- RuntimeService can be generated
RuntimeService runtimeService = processEngine.getRuntimeService();
- Can generate TaskService
TaskService taskService =processEngine.getTaskService();
The role of each Service
Xxxservice | effect |
---|---|
RepositoryService | Management process definition |
RuntimeService | Perform management, including operations such as starting, advancing, and deleting process instances |
TaskService | Task management |
2.2.RepositoryService
Activiti's warehouse service class. The so-called warehouse refers to the two files of the process definition document: the bpmn file and the process picture.
The service can be used to delete the deployed process definition.
2.3.RuntimeService
It is the process execution service class of Activiti. From this service class, you can get a lot of information about process execution.
2.4.TaskService
It is the task service class of Activiti. You can get task-related information from this class, such as personal to-do tasks and user group to-do tasks that are currently being performed.
2.5.HistoryService
It is Activiti's class for querying historical information. After a process is executed, this object provides us with query historical information, which can track the running status of all to-do nodes corresponding to the process instance.
2.6.ProcessDefinition
Process definition class, resource files etc. can be obtained from here.
2.7.ProcessInstance
Represents the execution instance of the process definition. When a deployed flowchart is started, the process has only one process instance data, but it can have multiple process tasks, and each task corresponds to the corresponding process node in the flowchart.