其他分享
首页 > 其他分享> > Activiti

Activiti

作者:互联网

Activiti7入门手册

一、工作流介绍#

1.1、概念#

工作流(Workflow),指“业务过程的部分或整体在计算机应用环境下的自动化”。是对工作流程及其各操作步骤之间业务规则的抽象、概括描述。在计算机中,工作流属于计算机支持的协同工作(CSCW)的一部分。后者是普遍地研究一个群体如何在计算机的帮助下实现协同工作的。

工作流主要解决的主要问题是:为了实现某个业务目标,利用计算机在多个参与者之间按某种预定规则自动传递文档、信息或者任务。

1.2、工作流系统#

工作流管理系统(Workflow Management System, WfMS)是一个软件系统,它完成工作量的定义和管理,按照在系统中预先定义好的工作流逻辑进行工作流实例的执行。

工作流管理系统不是企业的业务系统,而是为企业的业务系统的运行提供了一个软件的支撑环境。

工作流管理联盟(WfMC,Workflow Management Coalition)给出的关于工作流管理系统的定义是:工作流管理系统是一个软件系统,它完成工作流的定义和管理,并按照在计算机中预先定义好的工作流逻辑推进工作流实例的执行。

1.3、使用行业#

1.4、具体应用#

1.5、工作流实现方式#

二、Activiti7介绍#

2.1、概述#

2.2、BPM#

2.3、BPM软件#

2.4、BPMN#

diagram-5808583

<?xml version="1.0" encoding="utf-8"?>

<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="sample-diagram" targetNamespace="http://bpmn.io/schema/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
  <bpmn2:process id="process1596086637078" name="流程1596086637078">
    <bpmn2:startEvent id="StartEvent_01ydzqe" name="开始">
      <bpmn2:outgoing>SequenceFlow_13g4uaz</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:userTask id="UserTask_1lf9x0v" name="填写请假单">
      <bpmn2:incoming>SequenceFlow_13g4uaz</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_0yljdna</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="SequenceFlow_13g4uaz" sourceRef="StartEvent_01ydzqe" targetRef="UserTask_1lf9x0v"/>
    <bpmn2:userTask id="UserTask_0xa95fp" name="部门经理审批">
      <bpmn2:incoming>SequenceFlow_0yljdna</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_0wc7wvy</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="SequenceFlow_0yljdna" sourceRef="UserTask_1lf9x0v" targetRef="UserTask_0xa95fp"/>
    <bpmn2:userTask id="UserTask_1p57lka" name="人事复核">
      <bpmn2:incoming>SequenceFlow_0wc7wvy</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_0o4nsg1</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="SequenceFlow_0wc7wvy" sourceRef="UserTask_0xa95fp" targetRef="UserTask_1p57lka"/>
    <bpmn2:endEvent id="EndEvent_05yb7io" name="结束">
      <bpmn2:incoming>SequenceFlow_0o4nsg1</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_0o4nsg1" sourceRef="UserTask_1p57lka" targetRef="EndEvent_05yb7io"/>
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="process1596086637078">
      <bpmndi:BPMNShape id="StartEvent_01ydzqe_di" bpmnElement="StartEvent_01ydzqe">
        <dc:Bounds x="382" y="82" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="389" y="58" width="22" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_1lf9x0v_di" bpmnElement="UserTask_1lf9x0v">
        <dc:Bounds x="490" y="60" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_13g4uaz_di" bpmnElement="SequenceFlow_13g4uaz">
        <di:waypoint x="418" y="100"/>
        <di:waypoint x="490" y="100"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="UserTask_0xa95fp_di" bpmnElement="UserTask_0xa95fp">
        <dc:Bounds x="700" y="60" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0yljdna_di" bpmnElement="SequenceFlow_0yljdna">
        <di:waypoint x="590" y="100"/>
        <di:waypoint x="700" y="100"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="UserTask_1p57lka_di" bpmnElement="UserTask_1p57lka">
        <dc:Bounds x="910" y="60" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0wc7wvy_di" bpmnElement="SequenceFlow_0wc7wvy">
        <di:waypoint x="800" y="100"/>
        <di:waypoint x="910" y="100"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="EndEvent_05yb7io_di" bpmnElement="EndEvent_05yb7io">
        <dc:Bounds x="1152" y="82" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1159" y="125" width="22" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0o4nsg1_di" bpmnElement="SequenceFlow_0o4nsg1">
        <di:waypoint x="1010" y="100"/>
        <di:waypoint x="1152" y="100"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>

在线BPMN网址https://bpmn.io

三、Activiti如何使用#

3.1、整合Activiti#

3.2、实现业务流程建模,使用BPMN实现业务流程图#

3.3、部署业务流程到Activiti#

3.4、启动流程实例#

3.5、查询待办任务(Task)#

3.6、处理待办任务#

3.7、结束流程#

四、Activiti环境#

4.1、数据库支持#

CREATE DATABASE activiti DEFAULT CHARACTER SET utf8mb4;

4.2、创建Java工程#

4.3、加入相关jar包的Maven坐标#

<?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>org.example</groupId>
    <artifactId>activiti_project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <activiti.version>7.1.0.M6</activiti.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.activiti/activiti-bpmn-model -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-model</artifactId>
            <version>7.1.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-converter</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.activiti/activiti-json-converter -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-json-converter</artifactId>
            <version>7.1.0.M6</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-layout</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.activiti.cloud/activiti-cloud-services-api -->
        <dependency>
            <groupId>org.activiti.cloud</groupId>
            <artifactId>activiti-cloud-services-api</artifactId>
            <version>7-201802-EA</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- log start -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.13.3</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
        </dependency>


        <!-- log end -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.8.0</version>
        </dependency>



    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.activiti.cloud.dependencies</groupId>
                <artifactId>activiti-cloud-dependencies</artifactId>
                <version>7.0.0.GA</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

4.4、log4j2.xml#

<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration后面的status用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,可以看到log4j2内部各种详细输出-->
<configuration status="INFO">
    <!--先定义所有的appender-->
    <appenders>
        <!--输出日志信息到控制台-->
        <console name="Console" target="SYSTEM_OUT">
            <!--控制日志输出的格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </console>
    </appenders>
    <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
    <!--root:用于指定项目的根日志,如果没有单独指定Logger,则会使用root作为默认的日志输出-->
    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>

4.5、activiti.cfg.xml#

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

    <!--  配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;useSSL=false&amp;serverTimezone=GMT%2B8&amp;allowPublicKeyRetrieval=true"/>
        <property name="password" value="root"/>
        <property name="maxIdle" value="1"/>
    </bean>

    <!-- Activiti单独运行的ProcessEngine配置 -->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!--
            activiti数据库表处理策略
                false(默认值):检查数据库的版本和依赖库的版本,如果不匹配就抛出异常
                true:构建流程引擎时,执行检查,如果需要就执行更新。如果表不存在,就创建。
                create-drop:构建流程引擎时创建数据库报表,关闭流程引擎时就删除这些表。
                drop-create:先删除表再创建表。
                create:构建流程引擎时创建数据库表,关闭流程引擎时不删除这些表
        -->
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>
</beans>

4.5、测试#

package com.sunxiaping;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-07-31 6:41
 */
public class ActivitiTest {

    public static void main(String[] args) {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        //创建ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println("processEngine = " + processEngine);
    }

}

也可以这样

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-07-31 6:41
 */

public class ActivitiTest {

    public static void main(String[] args) {
        ProcessEngineConfiguration config = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        config.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        config.setJdbcUrl("jdbc:mysql://localhost:3306/activiti?createDatabaseIfNotExist=true");
        config.setJdbcUsername("root");
        config.setJdbcPassword("root");
        config.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        ProcessEngine engine = config.buildProcessEngine();
        System.out.println(engine);
    }

}

此时我们查看数据库,发现25张表

五、Activiti服务架构#

5.1、概述#

5.2、 Service#

5.2.1、Service的创建方式#

package com.sunxiaping;

import org.activiti.engine.*;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-07-31 11:55
 */
public class ActivitiTest {

    public static void main(String[] args) {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        //创建ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        TaskService taskService = processEngine.getTaskService();
        HistoryService historyService = processEngine.getHistoryService();
        ManagementService managementService = processEngine.getManagementService();

        System.out.println("repositoryService = " + repositoryService);
        System.out.println("runtimeService = " + runtimeService);
        System.out.println("taskService = " + taskService);
        System.out.println("historyService = " + historyService);
        System.out.println("managementService = " + managementService);

    }
}

5.2.2、Service总览#

Service接口 说明
RepositoryService Activiti的资源管理接口
RuntimeService Activiti的流程运行管理接口
TaskService Activiti的任务管理接口
HistoryService Activiti的历史管理接口
ManagementService Activiti的引擎管理接口

5.2.3、RepositoryService#

5.2.4、RuntimeService#

5.2.5、TaskService#

5.2.6、HistoryService#

5.2.7、ManagementService#

六、流程定义#

6.1、画板#

在IDEA中安装对应的Activiti-Designer已停止更新了。

https://gitee.com/jjjxxx/activiti-designer

6.1.1、介绍#

基于bpmn.js 的 activiti 模型设计器

6.1.2、安装教程#

  1. 切换到项目目录下运行 npm install 安装依赖
  2. 运行 npm run dev 启动

6.1.3、使用说明#

  1. 可在页面进行拖拽设计activiti的流程图,支持导出bpmn流程图和svg图片
  2. 运行截图

6.2、绘制流程#

image-20210317131433207

选中可执行文件

image-20210317131528861

把请假申请任务分配给张三

image-20210317131604581

导出diagram.bpmn

6.3、部署流程定义#

将刚才生成的holiday.bpmn和holiday.png拷贝到项目的resources目录下。

image-20210317131919691

测试

    @Test
    public void test() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //进行部署

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram.bpmn") //添加bpmn资源
                .addClasspathResource("diagram.svg")
                .name("请假申请程")
                .deploy(); //部署
        //输出部署的一些信息
        String id = deployment.getId();
        System.out.println("流程部署id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署名称 = " + name);
    }

运行结果:

image-20210317132125717

6.4、启动一个流程实例#

关系: 流程定义(BPMN文件)-->流程部署(Activiti的三张表)。
流程实例-->启动流程实例。

类比:
流程定义类似于Java中的类,流程实例类似于Java中的一个实例(对象),所以一个流程定义key对应多个不同的流程实例。

  @Test
    public void test2() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RuntimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //根据流程定义的key启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday");
        //获取流程实例的相关信息
        String processDefinitionId = processInstance.getProcessDefinitionId();
        System.out.println("流程定义的id = " + processDefinitionId);
        String deploymentId = processInstance.getDeploymentId();
        System.out.println("流程部署的id = " + deploymentId);
        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
        String activityId = processInstance.getActivityId();
        System.out.println("当前活动的id = " + activityId);

    }

运行结果:

image-20210317132729509

6.5、任务查询#


    @Test
    public void test3() {
        //任务负责人
        String assignee = "张三";
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取TaskService对象
        TaskService taskService = processEngine.getTaskService();
        //查询任务列表,根据流程定义的key和任务负责人
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey("holiday").taskAssignee(assignee).list();
        //遍历任务列表
        for (Task task : taskList) {
            String processDefinitionId = task.getProcessDefinitionId();
            System.out.println("流程定义id = " + processDefinitionId);
            String processInstanceId = task.getProcessInstanceId();
            System.out.println("流程实例id = " + processInstanceId);
            String assignee1 = task.getAssignee();
            System.out.println("任务负责人 = " + assignee1);
            String id = task.getId();
            System.out.println("任务id = " + id);
            String name = task.getName();
            System.out.println("任务名称 = " + name);
        }
    }

image-20210317132906653

6.6、任务处理#

@Test
    public void test4() {
        //任务负责人
        String assignee = "张三";
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取TaskService对象
        TaskService taskService = processEngine.getTaskService();
        //查询任务列表,根据流程定义的key和任务负责人
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey("holiday").taskAssignee(assignee).list();
        //遍历任务列表
        for (Task task : taskList) {
            String id = task.getId();
            //完成任务
            taskService.complete(id);
            System.out.println("完成任务"+task.getName());
        }
    }

image-20210317133535923

七、流程定义#

7.1 流程定义概述#

7.1.1、什么是流程定义?#

7.1.2、bpmn文件#

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:activiti="http://activiti.org/bpmn" id="sample-diagram" targetNamespace="http://activiti.org/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
  <bpmn2:process id="holiday" isExecutable="true">
    <bpmn2:startEvent id="StartEvent_1">
      <bpmn2:outgoing>SequenceFlow_0rn9i9a</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:userTask id="UserTask_0ch0apa" name="填写请假申请" activiti:assignee="张三">
      <bpmn2:incoming>SequenceFlow_0rn9i9a</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_12ujysh</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="SequenceFlow_0rn9i9a" sourceRef="StartEvent_1" targetRef="UserTask_0ch0apa" />
    <bpmn2:userTask id="UserTask_0vnu4us" name="部门经理审批">
      <bpmn2:incoming>SequenceFlow_12ujysh</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_002nzo9</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="SequenceFlow_12ujysh" sourceRef="UserTask_0ch0apa" targetRef="UserTask_0vnu4us" />
    <bpmn2:userTask id="UserTask_1jz9b0z" name="总经理审批">
      <bpmn2:incoming>SequenceFlow_002nzo9</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_0uv53v4</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="SequenceFlow_002nzo9" sourceRef="UserTask_0vnu4us" targetRef="UserTask_1jz9b0z" />
    <bpmn2:endEvent id="EndEvent_0ampl6l">
      <bpmn2:incoming>SequenceFlow_0uv53v4</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_0uv53v4" sourceRef="UserTask_1jz9b0z" targetRef="EndEvent_0ampl6l" />
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="holiday">
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="402" y="392" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_0ch0apa_di" bpmnElement="UserTask_0ch0apa">
        <dc:Bounds x="490" y="370" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0rn9i9a_di" bpmnElement="SequenceFlow_0rn9i9a">
        <di:waypoint x="438" y="410" />
        <di:waypoint x="490" y="410" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="UserTask_0vnu4us_di" bpmnElement="UserTask_0vnu4us">
        <dc:Bounds x="650" y="370" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_12ujysh_di" bpmnElement="SequenceFlow_12ujysh">
        <di:waypoint x="590" y="410" />
        <di:waypoint x="650" y="410" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="UserTask_1jz9b0z_di" bpmnElement="UserTask_1jz9b0z">
        <dc:Bounds x="810" y="370" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_002nzo9_di" bpmnElement="SequenceFlow_002nzo9">
        <di:waypoint x="750" y="410" />
        <di:waypoint x="810" y="410" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="EndEvent_0ampl6l_di" bpmnElement="EndEvent_0ampl6l">
        <dc:Bounds x="972" y="392" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0uv53v4_di" bpmnElement="SequenceFlow_0uv53v4">
        <di:waypoint x="910" y="410" />
        <di:waypoint x="972" y="410" />
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>

7.2 流程定义部署#

7.2.1、什么是流程定义部署#

7.2.2、单个文件部署方式#

    @Test
    public void test() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn") //添加bpmn资源
                .addClasspathResource("diagram/holiday.svg")
                .name("请假申请流程")
                .deploy(); //部署
        //输出部署的一些信息
        String id = deployment.getId();
        System.out.println("流程部署id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署名称 = " + name);
    }

7.2.3、压缩包部署方式#

将.bpmn文件和.png图片压缩成一个zip包。

image-20210318005121149

@Test
    public void test() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();

        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("diagram/holiday.zip");

        ZipInputStream zipInputStream = new ZipInputStream(inputStream);

        //进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("请假申请流程")
                .deploy(); //部署
        //输出部署的一些信息
        String id = deployment.getId();
        System.out.println("流程部署id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署名称 = " + name);
    }

7.2.4、操作数据表#

说明:
act_re_deploymentact_re_procdef是一对多的关系,一次部署在流程部署表生成一条记录,但一次流程部署可以部署多个流程定义,每个流程定义在流程定义表生成一条记录。每一个流程定义在act_ge_bytearray会存在两个资源记录.bpmn文件和.png图片。

建议:
一次部署一个流程,这样部署表和流程定义表示一对一的关系,方便读取流程部署和流程定义信息。

7.3、流程定义查询#

    public void test5() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //得到ProcessDefinitionQuery对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //设置条件,并查询出当前的所有流程定义
        List<ProcessDefinition> processDefinitionList = processDefinitionQuery.processDefinitionKey("holiday")
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
        //输出流程定义信息
        for (ProcessDefinition processDefinition : processDefinitionList) {
            System.out.println("流程定义的id = " + processDefinition.getId());
            System.out.println("流程定义的name = " + processDefinition.getName());
            System.out.println("流程定义的key = " + processDefinition.getKey());
            System.out.println("流程定义的version = " + processDefinition.getVersion());
        }
    }

结果显示为:

image-20210317221454265

7.4、流程定义删除#

@Test
    public void test6() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //删除已经部署的流程定义

        //得到ProcessDefinitionQuery对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //设置条件,并查询出当前的所有流程定义
        List<ProcessDefinition> processDefinitionList = processDefinitionQuery.processDefinitionKey("holiday")
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
        for (ProcessDefinition processDefinition : processDefinitionList) {
            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());
        }
        String deploymentId = "22501";
        repositoryService.deleteDeployment(deploymentId,true);
    }

image-20210318000806159

在数据库中访问DEPLOYMENT_ID表可以发现22501的部署数据已经消失。

7.5、流程定义资源查询#

   @Test
    public void test7() throws IOException {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //获取ProcessDefinitionQuery对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //设置查询条件,执行查询操作
        List<ProcessDefinition> processDefinitionList = processDefinitionQuery.processDefinitionKey("holiday")
                .orderByProcessDefinitionVersion()
                .desc()
                .list();


        //遍历查询结果
        for (ProcessDefinition processDefinition : processDefinitionList) {
            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());
            //获取资源名称,即png图片的名称
            String resourceName = processDefinition.getResourceName();
            //获取图表资源,即bpmn图片的名称
            String diagramResourceName = processDefinition.getDiagramResourceName();
            //获取资源的输入流,即png图片的输入流
            InputStream resourceNameInputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
            InputStream diagramResourceNameInputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);

            String resourcePath =  "/Users/wzx/Downloads/"+resourceName;
            File file = new File(resourcePath);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
            }

            String diagramResourcePath =  "/Users/wzx/Downloads/"+diagramResourceName;
            file = new File(diagramResourcePath);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
            }

            //复制文件
            FileCopyUtils.copy(resourceNameInputStream, new FileOutputStream(resourcePath));
            FileCopyUtils.copy(diagramResourceNameInputStream, new FileOutputStream(diagramResourcePath));
        }
    }

7.6、流程历史信息查询#

    @Test
    public void test() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        //获取ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取HistoryService对象
        HistoryService historyService = processEngine.getHistoryService();
        //获取HistoricActivityInstanceQuery查询对象
        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
        List<HistoricActivityInstance> historicActivityInstanceList = historicActivityInstanceQuery.list();
        for (HistoricActivityInstance historicActivityInstance : historicActivityInstanceList) {
            String activityId = historicActivityInstance.getActivityId();
            System.out.println("activityId = " + activityId);
            String activityName = historicActivityInstance.getActivityName();
            System.out.println("activityName = " + activityName);
            String processDefinitionId = historicActivityInstance.getProcessDefinitionId();
            System.out.println("processDefinitionId = " + processDefinitionId);
            String processInstanceId = historicActivityInstance.getProcessInstanceId();
            System.out.println("processInstanceId = " + processInstanceId);
        }

    }

参考转自

Activiti7的基本原理和使用

Activiti7工作流的使用

标签:ProcessEngineConfiguration,定义,activiti,流程,Activiti,System
来源: https://www.cnblogs.com/maBK/p/16653615.html