使用介绍
1、Activiti的表说明 使用25张表 ACT_ RE 流程定义和流程资源 ACT_ RU 运行时,流程实例、任务、变量 ACT HI 历史表 ACT GE 通用表 2、Activiti的架构、 类关系图 获取流程引擎的工具类 ProcessEngines.使用默认方式获取配置文件,构造流程引擎。配置文件名字activiti.cfg. xml,放在classpath下 ProcessEngineConfiguration.可以自定义配置文件名
使用_上面2个工具类,都可以获得流程引擎 ProcessEngine :流程引擎。获取各种服务的接口。 服务接口:用于流程的部署、执行、管理,使用这些接口就是在操作对应的数据表 RepositoryService 资源管理类 RuntimeService 运行时管理类 TaskService 任务管理类 HistoryService 历史数据管理类 ManagementService 流程引擎管理类
一、安装
引入pom文件
<properties>
<activiti.version>7.0.0.Beta1</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 模型处理 -->
<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>
</dependencies>
二、安装BPMN插件

三、生成act表
1、在resources目录下新建
<?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 -->
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!--直接引用上面配置的链接池-->
<property name="dataSource" ref="dataSource"/>
<!--actviti数据库表在生成时的策略
true - 如果数据库中已经存在相应的表,那么直接使用,
如果不存在,那么会创建-->
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
2、运行代码生成表
@Test
void TestCreateTable() {
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
}
效果如下: 
四、流程创建
1、定义流程的ID和NAME 2、定义节点名称和执行人 3、导出PNG图片存放到当前目录  
4、部署流程
public void testDeployment() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment()
.name("出差审批流程")
.addClasspathResource("bpmn/evection.bpmn20.xml")
.addClasspathResource("bpmn/evection.bpmn20.png")
.deploy();
System.out.println("流程部署的ID" + deploy.getId());
System.out.println("流程部署的NAME" + deploy.getName());
}
5、启动流程
@Test
public void startProcess() {
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
ProcessInstance ins = runtimeService.startProcessInstanceByKey("MyEvection");
System.out.println("流程定义ID" + ins.getProcessDefinitionId());
System.out.println("流程实例ID" + ins.getId());
System.out.println("活动的ID" + ins.getActivityId());
}
五、代办查询
@Test
public void getTaskLists() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("MyEvection")
.taskAssignee("Tom")
.list();
for (Task task : taskList) {
System.out.println("流程实例的ID" + task.getProcessInstanceId());
System.out.println("任务的ID" + task.getId());
System.out.println("任务的负责人" + task.getAssignee());
System.out.println("任务的名称" + task.getName());
}
}
SQL分析如下:
select distinct RES.* from ACT_RU_TASK RES inner join ACT_RE_PROCDEF D on RES.PROC_DEF_ID_ = D.ID_ WHERE RES.ASSIGNEE_ = ? and D.KEY_ = ? order by RES.ID_ asc LIMIT ? OFFSET ?
Parameters: Tom(String), MyEvection(String), 2147483647(Integer), 0(Integer)
SQL结果如下: 
六、完成当前任务
@Test
public void completeTask() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery()
.processDefinitionKey("MyEvection")
.taskAssignee("James")
.singleResult();
taskService.complete(task.getId());
System.out.println("流程实例的ID" + task.getProcessInstanceId());
System.out.println("任务的ID" + task.getId());
System.out.println("任务的负责人" + task.getAssignee());
System.out.println("任务的名称" + task.getName());
}
操作的表有 ACT_ HI_ TASKINST ACT_ HI_ ACTINST ACT_ HI_ IDENTITYLINK ACT_ RU_ TASK ACT RU_ IDENTITYLINK ACT_ HI_ TASKINST UPDATE ACT_ RU_ EXECUTION UPDATE ACT_ HI_ ACTINST UPDATE
七、使用ZIP方式(批量)部署
@Test
public void deployProcessZip() {
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment()
.addZipInputStream(
new ZipInputStream(
Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("bpmn/evection.zip"))
)).deploy();
System.out.println("部署的ID" + deploy.getId());
System.out.println("部署的名字" + deploy.getName());
}
使用Activiti提供的API把流程图的内容写入数据库中 属于资源类操作,使用RepositoryService 单文件部署:把bpmn文件和png文件一个-个处理 压缩包部署:把bpmn文件和png打压缩包来处理 Deployment deploy = repositoryService. createDeployment () 部署操作表: act_ re_ deployment 部署表 act_ re_ procdef 流程定义表 act_ ge_ bytearray 资源表
八、流程查询
@Test
public void getTask() {
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey("MyEvection")
.orderByProcessDefinitionVersion()
.desc().list();
for (ProcessDefinition processDefinition : list) {
System.out.println("流程定义的ID: " + processDefinition.getId());
System.out.println("流程定义的NAME: " + processDefinition.getName());
System.out.println("流程定义的KEY: " + processDefinition.getKey());
System.out.println("流程定义的VERSION: " + processDefinition.getVersion());
System.out.println("流程部署的ID: " + processDefinition.getDeploymentId());
}
}

九、流程删除
删除有两种模式:①只会删除【re_deployment、ge_bytearray、re_procdef】三张表;②删除所有关于该流程部署的表格
@Test
public void deleteTask() {
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.deleteDeployment("1", true);
}
十、查询历史流程
@Test
public void findHistoryInfo() {
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
HistoryService historyService = processEngine.getHistoryService();
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processInstanceId("2501").orderByHistoricActivityInstanceStartTime().asc().list();
for (HistoricActivityInstance historicActivityInstance : list) {
System.out.println("活动ID" + historicActivityInstance.getActivityId());
System.out.println("活动NAME" + historicActivityInstance.getActivityName());
System.out.println("流程定义ID" + historicActivityInstance.getProcessDefinitionId());
System.out.println("流程实例ID" + historicActivityInstance.getProcessInstanceId());
System.out.println("===============================");
}
}
十一、添加业务ID
执行的表【act_ru_execution】
@Test
public void addBusinessKey() {
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("MyEvection", "1001");
System.out.println("业务Key" + processInstance.getBusinessKey());
}
十二、【全部】流程实例的挂起和激活
@Test
public void suspendAllProcessInstance() {
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey("MyEvection").singleResult();
if (processDefinition.isSuspended()) {
repositoryService.activateProcessDefinitionByKey(processDefinition.getId(), true, null);
} else {
repositoryService.suspendProcessDefinitionById(processDefinition.getId(), true, null);
}
}
十三、【单个】流程实例的挂机和激活
@Test
public void suspendOneProcessInstance() {
ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId("12501").singleResult();
if (processInstance.isSuspended()) {
runtimeService.activateProcessInstanceById(processInstance.getId());
System.out.println("流程已经被激活");
} else {
runtimeService.suspendProcessInstanceById(processInstance.getId());
System.out.println("流程已经被挂起");
}
}
|