基础配置
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gavin</groupId>
<artifactId>activiti</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!-- 连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.activiti.cloud</groupId>
<artifactId>activiti-cloud-services-api</artifactId>
<version>7.0.0.Beta1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
</project>
创建数据库表
创建工作表引擎需要的数据库表:
创建方式—使用processEngine 创建
这里需要配置所需的xml文件,该文件位置在resource文件夹下,而且文件名必须为activities.cfg.xml
<?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="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://172.21.114.22:3306/activiti"/>
<property name="jdbcUsername" value="root"/>
<property name="jdbcPassword" value="123456"/>
<!-- 数据库表生成策略,如果存在则直接使用-->
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
生成数据表
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.junit.Test;
public class ActTest {
@Test
public void Test01(){
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
System.out.println(engine);
}
}
表的作用
生成的25张表:
操作数据表
有了这么多表,我们需要去操作这些表,activiti已经封装好了一些操作的api;
上面得到的**service,之后可以通过这些service去操作数据表:
比如
但是在这之前我们要了解这些service是管理哪些表的;
repositoryService—>资源管理服务(带re的表)
runtimeService—>流程运行服务(带ru的表)
taskService—>任务管理服务
historyService—>历史管理服务
identityService—>用户管理服务
managementService—>引擎管理服务
以上是activiti初始化时候的一些知识;
工作流引擎创建
以上在运行时使用了默认的创建方式—>
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
这种方式需要指定 activiti.cfg.xml的文件名以及存放位置;
源码
public synchronized static void init() {
if (!isInitialized()) {
if (processEngines == null) {
// Create new map to store process-engines if current map is
// null
processEngines = new HashMap<String, ProcessEngine>();
}
ClassLoader classLoader = ReflectUtil.getClassLoader();
Enumeration<URL> resources = null;
try {
resources = classLoader.getResources("activiti.cfg.xml");
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("problem retrieving activiti.cfg.xml resources on the classpath: " + System.getProperty("java.class.path"), e);
}
.............................下面的代码略.....................
此时配置文件activiti.cfg.xml中的bean 名称processEngineConfiguration
calss需要指定为org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration
另一种方式是手动指定配置文件的位置,然后创建
processEngine实例
// 配置文件自定义
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
//这里可以对bean进行命名
ProcessEngineConfiguration newBeanName = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml", "newBeanName");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
ProcessEngine processEngine1 = newBeanName.buildProcessEngine();
小结:工作流引擎创建的方式
默认
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
可以自定义配置文件名和bean的名
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();