Activiti使用步骤

使用介绍

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插件

Activiti使用步骤_第1张图片

三、生成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() {
    // 创建表[默认从resources下读取文件]
    /*ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    System.out.println(processEngine);*/

    //使用自定义方式创建
    ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
}

效果如下:
Activiti使用步骤_第2张图片

四、流程创建

1、定义流程的ID和NAMEActiviti使用步骤_第3张图片
2、定义节点名称和执行人Activiti使用步骤_第4张图片
3、导出PNG图片存放到当前目录
Activiti使用步骤_第5张图片
Activiti使用步骤_第6张图片

4、部署流程

public void testDeployment() {
   //1、创建ProcessEngine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    //2、获取RepositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    //3、对流程进行部署,将bpmn和png部署到数据库中
    Deployment deploy = repositoryService.createDeployment()
            .name("出差审批流程")
            .addClasspathResource("bpmn/evection.bpmn20.xml")
            .addClasspathResource("bpmn/evection.bpmn20.png")
            .deploy();
    //4、输出
    System.out.println("流程部署的ID" + deploy.getId());
    System.out.println("流程部署的NAME" + deploy.getName());
}

5、启动流程

/**
 * 启动流程实例
 * act_hi_actinst  流程实例执行历史
 * act_hi_identitylink 流程参与用户信息历史
 * act_hi_taskins 任务实例历史信息
 * act_ru_execution 流程执行的信息
 * act_ru_identitylink 流程参与者信息
 * act_ru_task 任务信息
 *
 */
@Test
public void startProcess() {
    //1、创建流程ProcessEngine
    ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
    //2、获取流程RuntimeService服务
    RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
    //3、根据流程定义的id启动流程
    ProcessInstance ins = runtimeService.startProcessInstanceByKey("MyEvection");
    //4、输出
    System.out.println("流程定义ID" + ins.getProcessDefinitionId());
    System.out.println("流程实例ID" + ins.getId());
    System.out.println("活动的ID" + ins.getActivityId());
}

五、代办查询

/**
 * 代办查询
 */
@Test
public void getTaskLists() {
    //1、获取ProcessEngine流程引擎
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    //2、获取待办任务
    TaskService taskService = processEngine.getTaskService();

    //3、根据流程key和任务负责人查询任务
    List<Task> taskList = taskService.createTaskQuery()
            .processDefinitionKey("MyEvection")     //流程的id
            .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结果如下:
Activiti使用步骤_第7张图片

六、完成当前任务

/**
 * 完成当前任务
 */
@Test
public void completeTask() {
    //1、获取流程引擎
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    //2、获取service
    TaskService taskService = processEngine.getTaskService();
    //3、根据任务ID完成任务
    //taskService.complete("2505"); // taskId
    //获取
    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方式(批量)部署

/**
 * 使用zip进行批量部署
 */
@Test
public void deployProcessZip() {
    //1、使用自定义文件名称创建ProcessEngine
    ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();

    //2、创建service
    RepositoryService repositoryService = processEngine.getRepositoryService();

    //3、部署
    Deployment deploy = repositoryService.createDeployment()
            .addZipInputStream( //使用zip格式进行部署
                    new ZipInputStream(     //构造InputStream
                            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() {
    //1、使用自定义文件名称创建ProcessEngine
    ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();

    //2、获取Service接口
    RepositoryService repositoryService = processEngine.getRepositoryService();

    //3、获取对象进行查询 list()/singleResult()
    List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
            .processDefinitionKey("MyEvection") //流程定义的id
            .orderByProcessDefinitionVersion()  //根据version进行倒叙
            .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】三张表;②删除所有关于该流程部署的表格

/**
 * 删除部署信息【re_deployment、ge_bytearray、re_procdef】
 *
 */
@Test
public void deleteTask() {
    //1、使用自定义文件名称创建ProcessEngine
    ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    //re_deployment、ge_bytearray、re_procdef】
    //repositoryService.deleteDeployment("1");
    // true表示删除所有的表里面的数据
    repositoryService.deleteDeployment("1", true);
}

十、查询历史流程

/**
 * 历史流程查询
 */
@Test
public void findHistoryInfo() {
    ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();

    HistoryService historyService = processEngine.getHistoryService();

    //获取hi_actinst
    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】

/**
 * 添加业务Key businessKey
 * 执行的表【act_ru_execution】
 */
@Test
public void addBusinessKey() {
    //1、获取流程引擎
    ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();

    //2、获取Service
    RuntimeService runtimeService = processEngine.getRuntimeService();

    //3、添加BusinessKey【在启动流程的过程中】
    //参数一:流程定义的ID  参数二:业务的ID(出差申请单的ID)
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("MyEvection", "1001");

    System.out.println("业务Key" + processInstance.getBusinessKey());
}

十二、【全部】流程实例的挂起和激活

/**
 * 全部流程实例的挂机和激活
 * suspend  暂停
 */
@Test
public void suspendAllProcessInstance() {
    //1、获取流程引擎
    ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();

    //2、获取Service
    RepositoryService repositoryService = processEngine.getRepositoryService();

    //3、查询流程定义
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
            .processDefinitionKey("MyEvection").singleResult();

    //4、判断是否挂起
    if (processDefinition.isSuspended()) {
        //挂起,执行激活操作
        // 参数一:流程定义的ID  参数二:是否激活  参数三: 激活的时间
        repositoryService.activateProcessDefinitionByKey(processDefinition.getId(), true, null);
    } else {
        //激活,执行挂起操作
        // 参数一:流程定义的ID  参数二:是否激活  参数三: 激活的时间
        repositoryService.suspendProcessDefinitionById(processDefinition.getId(), true, null);

    }
}

十三、【单个】流程实例的挂机和激活

/**
 * 挂起或激活单个流程实例
 *
 */
@Test
public void suspendOneProcessInstance() {
    //1、获取流程引擎
    ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();

    //2、获取Service
    RuntimeService runtimeService = processEngine.getRuntimeService();

    //3、查询流程定义
    ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
            .processInstanceId("12501").singleResult();

    //4、判断是否挂起
    if (processInstance.isSuspended()) {
        //挂起,执行激活操作
        // 参数一:流程实例的ID
        runtimeService.activateProcessInstanceById(processInstance.getId());
        System.out.println("流程已经被激活");
    } else {
        //激活,执行挂起操作
        // 参数一:流程实例的ID
        runtimeService.suspendProcessInstanceById(processInstance.getId());
        System.out.println("流程已经被挂起");

    }
}

你可能感兴趣的:(JAVA,java)