一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

一、Activiti环境

1、idea安装插件
* Bpmn图形其实是通过xml表示业务流程(.bpmn文件使用文本编辑器打开)
* File | Settings | Plugins
    - 搜索actiBPM(Activiti BPMN visualizer)
2、数据库支持
* 阿里云PolarDB
    - 集群白名单
        ~ 新增ip白名单分组(www.ip138.com)
        ~ 选择安全组
    - 账号管理
        ~ 创建账号
    - 基本信息
        ~ 链接地址(主地址申请公网)
        ~ 登录数据库
* 创建数据库(名字任意)
    - CREATE DATABASE activiti DEFAULT CHARACTER SET utf8;
3、创建maven工程
  • pom.xml
<properties>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <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>
    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.5</version>
    </dependency>
    <!-- 链接池 -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!-- log start -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
</dependencies>
  • src/main/resources/log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r[%15.15t] %-5p %30.30c %x - %m%n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=F:\\act\\activiti.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r[%15.15t] %-5p %30.30c %x - %m%n
  • src/main/resources/activiti.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">
    <!-- 默认id对应的值 为processEngineConfiguration -->
    <!-- processEngine Activiti的流程引擎 -->
    <bean id="processEngineConfiguration"
          class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///activiti"/>
        <property name="jdbcUsername" value="root"/>
        <property name="jdbcPassword" value="123456"/>
        <!-- activiti数据库表处理策略 -->
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>
</beans>
  • 测试
public class TestDemo {
    @Test
    public void testCreateDbTable() {
        // 生成activiti的数据库表
        // 使用classpath下的activiti.cfg.xml中的配置创建processEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);
    }
}
表分类 表名 解释
一般数据
[ACT_GE_BYTEARRAY] 通用的流程定义和流程资源
[ACT_GE_PROPERTY] 系统相关属性
流程历史记录
[ACT_HI_ACTINST] 历史的流程实例
[ACT_HI_ATTACHMENT] 历史的流程附件
[ACT_HI_COMMENT] 历史的说明性信息
[ACT_HI_DETAIL] 历史的流程运行中的细节信息
[ACT_HI_IDENTITYLINK] 历史的流程运行过程中用户关系
[ACT_HI_PROCINST] 历史的流程实例
[ACT_HI_TASKINST] 历史的任务实例
[ACT_HI_VARINST] 历史的流程运行中的变量信息
流程定义表
[ACT_RE_DEPLOYMENT] 部署单元信息
[ACT_RE_MODEL] 模型信息
[ACT_RE_PROCDEF] 已部署的流程定义
运行实例表
[ACT_RU_EVENT_SUBSCR] 运行时事件
[ACT_RU_EXECUTION] 运行时流程执行实例
[ACT_RU_IDENTITYLINK] 运行时用户关系信息,存储任务节点与参与者的相关信息
[ACT_RU_JOB] 运行时作业
[ACT_RU_TASK] 运行时任务
[ACT_RU_VARIABLE] 运行时变量表
service名称 service作用
RepositoryService activiti的资源管理类
RuntimeService activiti的流程运行管理类
TaskService activiti的任务管理类
HistoryService activiti的历史管理类
ManagerService activiti的引擎管理类
4、activiti入门
  • 流程符号
* 事件Event
    - 开始事件
    - 中间事件
    - 结束事件
* 活动Activity
    - 用户任务
    - 服务任务
    - 子流程
* 网关GateWay
    - 排他网关(x)
    - 并行网关(+)
    - 包容网关(+)
    - 综合网关
    - 事件网关(+)
* 流向Flow
    - 顺序流
    - 消息流
    - 关联
    - 数据关联
  • 流程设计器使用
* 右击目录 -> new -> New Activiti 6.x BPMN 2.0 file
* 右击xml编辑面板 -> View BPMN (Activiti) Diagram
* 右击画板
    - Connection:连接
    - Event:事件
    - Task:任务
    - Gateway:网关
    - Container:容器
    - Boundary event:边界事件
    - Intermediate event:中间事件
5、流程定义部署
public class ActivitiDemo {
  @Test
  public void testDeployment() {
    // 改动四张表:act_re_deployment、act_re_procdef、act_ge_bytearray、act_ge_property
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deploy = repositoryService
            .createDeployment()
            .name("出差申请流程")
            .addClasspathResource("bpmn/evection.bpmn20.xml")
            .addClasspathResource("bpmn/evection.png")
            .deploy();
    System.out.println("流程部署id=" + deploy.getId());
    System.out.println("流程部署名字=" + deploy.getName());
  }
}
6、启动流程-任务查询-完成任务
public class ActivitiDemo {
  @Test
  public void testStartProcess() {
    // 启动流程
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    ProcessInstance instance = runtimeService.startProcessInstanceByKey("evection");
    System.out.println("流程定义ID:" + instance.getProcessDefinitionId());
    System.out.println("流程实例ID:" + instance.getId());
    System.out.println("当前活动的ID:" + instance.getActivityId());
  }

  @Test
  public void testFindPersonalTaskList() {
    // 任务查询
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    List<Task> taskList = taskService.createTaskQuery().processDefinitionKey("evection").taskAssignee("zhangsan").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());
    }
  }

  @Test
  public void completTask() {
    // 完成任务
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    // taskService.complete("7505");
    // Task task = taskService.createTaskQuery().processDefinitionKey("evection").taskAssignee("jerry").singleResult();
    // Task task = taskService.createTaskQuery().processDefinitionKey("evection").taskAssignee("jack").singleResult();
    Task task = taskService.createTaskQuery().processDefinitionKey("evection").taskAssignee("rose").singleResult();
    System.out.println("流程实例id=" + task.getProcessInstanceId());
    System.out.println("任务Id=" + task.getId());
    System.out.println("任务负责人=" + task.getAssignee());
    System.out.println("任务名称=" + task.getName());
    taskService.complete(task.getId());
  }
}
7、zip方式进行流程部署
public class ActivitiDemo {
  @Test
  public void deployProcessByZip() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("bpmn/evection.zip");
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    Deployment deploy = repositoryService.createDeployment().addZipInputStream(zipInputStream).deploy();
    System.out.println("流程部署id=" + deploy.getId());
    System.out.println("流程部署的名称=" + deploy.getName());
  }
}
8、流程的查询和删除
public class ActivitiDemo {
  @Test
  public void queryProcessDefinition() {
    // 流程查询
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    ProcessDefinitionQuery definitionQuery = repositoryService.createProcessDefinitionQuery();
    List<ProcessDefinition> definitionList = definitionQuery.processDefinitionKey("myEvection").orderByProcessDefinitionVersion().desc().list();
    for (ProcessDefinition processDefinition : definitionList) {
      System.out.println("流程定义ID:" + processDefinition.getId());
      System.out.println("流程定义名称:" + processDefinition.getName());
      System.out.println("流程定义Key:" + processDefinition.getKey());
      System.out.println("流程定义版本:" + processDefinition.getVersion());
      System.out.println("流程部署ID:" + processDefinition.getDeploymentId());
    }
  }

  @Test
  public void deleteDeployMent() {
    // 流程删除
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    String deploymentId = "2501";
    // 当前流程如果没有完成,想要删除的话需要使用特殊方式,原理就是:级联删除
    repositoryService.deleteDeployment(deploymentId, true);
  }
}
9、流程资源下载
public class ActivitiDemo {
  @Test
  public void getDeployment() throws IOException {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("myEvection").singleResult();
    String deploymentId = processDefinition.getDeploymentId();
    // 从数据库获取流
    String pngName = processDefinition.getDiagramResourceName();
    InputStream pngInput = repositoryService.getResourceAsStream(deploymentId, pngName);
    String bpmnName = processDefinition.getResourceName();
    InputStream bpmnInput = repositoryService.getResourceAsStream(deploymentId, bpmnName);
    // 传输流
    File pngFile = new File("E:\\study\\ll-activiti\\evection.png");
    File bpmnFile = new File("E:\\study\\ll-activiti\\evection.bpmn20.xml");
    FileOutputStream pngOutputStream = new FileOutputStream(pngFile);
    FileOutputStream bpmnOutputStream = new FileOutputStream(bpmnFile);
    IOUtils.copy(pngInput, pngOutputStream);
    IOUtils.copy(bpmnInput, bpmnOutputStream);
    pngOutputStream.close();
    bpmnOutputStream.close();
    pngInput.close();
    bpmnInput.close();
  }
}
10、查看历史信息
public class ActivitiDemo {
  @Test
  public void findHistoryInfo() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    HistoryService historyService = processEngine.getHistoryService();
    HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery();
    // instanceQuery.processInstanceId("12501");
    instanceQuery.processDefinitionId("myEvection:1:10004");
    instanceQuery.orderByHistoricActivityInstanceStartTime().asc();
    List<HistoricActivityInstance> activityInstanceList = instanceQuery.list();
    for (HistoricActivityInstance hi : activityInstanceList) {
      System.out.println(hi.getActivityId());
      System.out.println(hi.getActivityName());
      System.out.println(hi.getProcessDefinitionId());
      System.out.println(hi.getProcessInstanceId());
      System.out.println("<==========================>");
    }
  }
}
11、任务挂起与激活
public class ActivtiBusinessDemo {
  @Test
  public void addBusinessKey() {
    // 实现businessKey的关联
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    ProcessInstance instance = runtimeService.startProcessInstanceByKey("myEvection", "1001");
    System.out.println("businessKey==" + instance.getBusinessKey());
  }

  @Test
  public void suspendAllProcessInstance() {
    // 实现流程定义的全部挂起与激活
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("myEvection").singleResult();
    boolean suspended = processDefinition.isSuspended();
    String definitionId = processDefinition.getId();
    if (suspended) {
      repositoryService.activateProcessDefinitionById(definitionId, true, null);
      System.out.println("流程定义id:" + definitionId + ",已激活");
    } else {
      repositoryService.suspendProcessDefinitionById(definitionId, true, null);
      System.out.println("流程定义id:" + definitionId + ",已挂起");
    }
  }

  @Test
  public void suspendSingleProcessInstance() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId("15001").singleResult();
    boolean suspended = instance.isSuspended();
    String instanceId = instance.getId();
    if (suspended) {
      runtimeService.activateProcessInstanceById(instanceId);
      System.out.println("流程实例id:" + instanceId + "已经激活");
    } else {
      runtimeService.suspendProcessInstanceById(instanceId);
      System.out.println("流程实例id:" + instanceId + "已经暂停");
    }
  }

  @Test
  public void completTask() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    Task task = taskService.createTaskQuery().processInstanceId("15001").taskAssignee("zhangsan").singleResult();
    System.out.println("流程实例id==" + task.getProcessInstanceId());
    System.out.println("流程任务id==" + task.getId());
    System.out.println("负责人==" + task.getAssignee());
    System.out.println("任务名称==" + task.getName());
    taskService.complete(task.getId());
  }
}
12、使用uel表达式设置负责人
<process id="myEvection1" name="出差申请-uel" isExecutable="true">
  <startEvent id="sid-34c8a888-0fca-4ed3-a310-7c7e323e3f97"/>
  <userTask id="sid-37addbf9-3c71-454e-ac96-bda52ca7d357" name="创建出差申请" activiti:assignee="${assignee0}"/>
  <userTask id="sid-30af0221-fd06-463f-a12a-b5e88a2d4055" name="经理审批" activiti:assignee="${assignee1}"/>
  <userTask id="sid-b78692a2-2a06-4ce4-994e-f20c242d99d0" name="总经理审批" activiti:assignee="${assignee2}"/>
  <userTask id="sid-c85642c4-a46c-494b-a32d-6806ae070fa3" name="财务审批" activiti:assignee="${assignee3}"/>
  <endEvent id="sid-8991e623-e51a-4765-8dc4-043c9a2ea97b"/>
  <sequenceFlow id="sid-e3aedfaa-42a1-4f10-9215-2b5d77266cd7" sourceRef="sid-34c8a888-0fca-4ed3-a310-7c7e323e3f97"
                targetRef="sid-37addbf9-3c71-454e-ac96-bda52ca7d357"/>
  <sequenceFlow id="sid-8d1e0a83-7d1f-4eb0-ae0a-d13346983e15" sourceRef="sid-37addbf9-3c71-454e-ac96-bda52ca7d357"
                targetRef="sid-30af0221-fd06-463f-a12a-b5e88a2d4055"/>
  <sequenceFlow id="sid-94b3ec8c-2dd4-4901-a73c-92912fe83dd5" sourceRef="sid-30af0221-fd06-463f-a12a-b5e88a2d4055"
                targetRef="sid-b78692a2-2a06-4ce4-994e-f20c242d99d0"/>
  <sequenceFlow id="sid-36ebfe0c-e8d9-4da4-a71d-cfaf47ba1803" sourceRef="sid-b78692a2-2a06-4ce4-994e-f20c242d99d0"
                targetRef="sid-c85642c4-a46c-494b-a32d-6806ae070fa3"/>
  <sequenceFlow id="sid-06ac31ac-601e-40ba-a9a0-f70222a97ba2" sourceRef="sid-c85642c4-a46c-494b-a32d-6806ae070fa3"
                targetRef="sid-8991e623-e51a-4765-8dc4-043c9a2ea97b"/>
</process>
public class TestAssigneeUel {
  @Test
  public void testDeployment() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deploy = repositoryService.createDeployment().name("出差申请流程-uel").addClasspathResource("bpmn/myEvection-uel.bpmn20.xml").deploy();
    System.out.println("流程部署id=" + deploy.getId());
    System.out.println("流程部署名字=" + deploy.getName());
  }

  @Test
  public void startAssigneeUel() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    Map<String, Object> assigneeMap = new HashMap<>();
    assigneeMap.put("assignee0", "张三");
    assigneeMap.put("assignee1", "李经理");
    assigneeMap.put("assignee2", "王总经理");
    assigneeMap.put("assignee3", "赵财务");
    runtimeService.startProcessInstanceByKey("myEvection1", assigneeMap);
  }
}
13、使用监听器设置负责人
<process id="testListener" name="测试监听器" isExecutable="true">
  <startEvent id="sid-9015503c-e6d8-4220-a895-7c4c3f7bb724"/>
  <userTask id="sid-4d0f29c2-9049-4528-bf15-42b847d81117" name="创建申请">
    <extensionElements>
      <activiti:taskListener event="all" class="com.itheima.demo.listener.MyTaskListener"/>
    </extensionElements>
  </userTask>
  <userTask id="sid-b5c63971-7df9-4928-b33b-36c83dda63bb" name="审核申请"/>
  <endEvent id="sid-e277723c-2c3b-4422-a942-bb81790fd077"/>
  <sequenceFlow id="sid-a16a2391-4dba-44ed-8dca-01eebd52c626" sourceRef="sid-9015503c-e6d8-4220-a895-7c4c3f7bb724"
                targetRef="sid-4d0f29c2-9049-4528-bf15-42b847d81117"/>
  <sequenceFlow id="sid-5202d6bb-6bef-43a7-8c79-71cd50cf72be" sourceRef="sid-4d0f29c2-9049-4528-bf15-42b847d81117"
                targetRef="sid-b5c63971-7df9-4928-b33b-36c83dda63bb"/>
  <sequenceFlow id="sid-44bef2b5-3baa-485d-941d-d6256de46da6" sourceRef="sid-b5c63971-7df9-4928-b33b-36c83dda63bb"
                targetRef="sid-e277723c-2c3b-4422-a942-bb81790fd077"/>
</process>
public class MyTaskListener implements TaskListener {
  @Override
  public void notify(DelegateTask delegateTask) {
    if ("创建申请".equals(delegateTask.getName()) && "create".equals(delegateTask.getEventName())) {
      delegateTask.setAssignee("张三");
    }
  }
}
public class TestListener {
  @Test
  public void testDeployment() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deploy = repositoryService.createDeployment().name("测试监听器").addClasspathResource("bpmn/demo-listen.bpmn20.xml").deploy();
    System.out.println("流程部署id=" + deploy.getId());
    System.out.println("流程部署名字=" + deploy.getName());
  }

  @Test
  public void startDemoListener() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    runtimeService.startProcessInstanceByKey("testListener");
  }
}
14、分支
<process id="myEvection2" isExecutable="true">
  <startEvent id="sid-3e990e29-894c-48c0-9396-91745d6ca99d"/>
  <userTask id="sid-929cb679-b5f4-473a-a5eb-f96791e993e6" name="创建出差申请" activiti:assignee="${assignee0}"/>
  <userTask id="sid-a24fc775-e0ba-47bf-9833-fba2d4a2df65" name="部门经理审核" activiti:assignee="${assignee1}"/>
  <userTask id="sid-24f814a6-2115-4aad-8548-48442160f290" name="总经理审批" activiti:assignee="${assignee2}"/>
  <userTask id="sid-ba98ec88-ccdf-4bb9-b32f-fe3eb61aef8e" name="财务审批" activiti:assignee="${assignee3}"/>
  <endEvent id="sid-9377faf8-a549-4eba-97ae-6c771ba8d024"/>
  <sequenceFlow id="sid-ba3be30b-ae23-4250-8d69-2c310d692490" sourceRef="sid-3e990e29-894c-48c0-9396-91745d6ca99d"
                targetRef="sid-929cb679-b5f4-473a-a5eb-f96791e993e6"/>
  <sequenceFlow id="sid-8211cb8a-45dc-4691-9fd0-a44d32e30ce0" sourceRef="sid-929cb679-b5f4-473a-a5eb-f96791e993e6"
                targetRef="sid-a24fc775-e0ba-47bf-9833-fba2d4a2df65"/>
  <sequenceFlow id="sid-8feb8b44-eb1d-4937-b804-17d6fc48dae8" sourceRef="sid-a24fc775-e0ba-47bf-9833-fba2d4a2df65"
                targetRef="sid-ba98ec88-ccdf-4bb9-b32f-fe3eb61aef8e">
    <conditionExpression><![CDATA[${evection.num<3}]]></conditionExpression>
  </sequenceFlow>
  <sequenceFlow id="sid-5fe6fc69-b6a4-4c0c-9c98-2922656d5d22" sourceRef="sid-a24fc775-e0ba-47bf-9833-fba2d4a2df65"
                targetRef="sid-24f814a6-2115-4aad-8548-48442160f290">
    <conditionExpression><![CDATA[${evection.num>=3}]]></conditionExpression>
  </sequenceFlow>
  <sequenceFlow id="sid-1cdb2d2d-c75e-4bee-b36a-feea7c62839f" sourceRef="sid-24f814a6-2115-4aad-8548-48442160f290"
                targetRef="sid-ba98ec88-ccdf-4bb9-b32f-fe3eb61aef8e"/>
  <sequenceFlow id="sid-4ce18095-0995-4d89-94de-a9641b1bb091" sourceRef="sid-ba98ec88-ccdf-4bb9-b32f-fe3eb61aef8e"
                targetRef="sid-9377faf8-a549-4eba-97ae-6c771ba8d024"/>
</process>
public class TestVariables2Complete {
  @Test
  public void testDeployment() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deploy = repositoryService.createDeployment().name("出差申请流程-variables-complete").addClasspathResource("bpmn/evection-global.bpmn20.xml").deploy();
    System.out.println("流程部署id=" + deploy.getId());
    System.out.println("流程部署名字=" + deploy.getName());
  }

  @Test
  public void testStartProcess() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    String key = "myEvection2";
    Map<String, Object> variables = new HashMap<>();
    // Evection evection = new Evection();
    // evection.setNum(3d);
    // variables.put("evection", evection);
    variables.put("assignee0", "李四3");
    variables.put("assignee1", "王经理3");
    variables.put("assignee2", "杨总经理3");
    variables.put("assignee3", "张财务3");
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, variables);
    System.out.println(processInstance.getId());
  }

  @Test
  public void completTask() {
    String key = "myEvection2";
    String assingee = "王经理3";
    Evection evection = new Evection();
    evection.setNum(2d);
    Map<String, Object> map = new HashMap<>();
    map.put("evection", evection);
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee(assingee).singleResult();
    if (task != null) {
      taskService.complete(task.getId(), map);
      System.out.println(task.getId() + "----任务已完成");
    }
  }
}
15、组任务
<process id="testCandidate" name="出差申请-候选人" isExecutable="true">
  <startEvent id="sid-34c8a888-0fca-4ed3-a310-7c7e323e3f97"/>
  <userTask id="sid-37addbf9-3c71-454e-ac96-bda52ca7d357" name="创建出差申请" activiti:assignee="zhangsan"/>
  <userTask id="sid-30af0221-fd06-463f-a12a-b5e88a2d4055" name="经理审批" activiti:candidateUsers="lisi,wangwu"/>
  <userTask id="sid-b78692a2-2a06-4ce4-994e-f20c242d99d0" name="总经理审批" activiti:assignee="jack"/>
  <userTask id="sid-c85642c4-a46c-494b-a32d-6806ae070fa3" name="财务审批" activiti:assignee="rose"/>
  <endEvent id="sid-8991e623-e51a-4765-8dc4-043c9a2ea97b"/>
  <sequenceFlow id="sid-e3aedfaa-42a1-4f10-9215-2b5d77266cd7" sourceRef="sid-34c8a888-0fca-4ed3-a310-7c7e323e3f97"
                targetRef="sid-37addbf9-3c71-454e-ac96-bda52ca7d357"/>
  <sequenceFlow id="sid-8d1e0a83-7d1f-4eb0-ae0a-d13346983e15" sourceRef="sid-37addbf9-3c71-454e-ac96-bda52ca7d357"
                targetRef="sid-30af0221-fd06-463f-a12a-b5e88a2d4055"/>
  <sequenceFlow id="sid-94b3ec8c-2dd4-4901-a73c-92912fe83dd5" sourceRef="sid-30af0221-fd06-463f-a12a-b5e88a2d4055"
                targetRef="sid-b78692a2-2a06-4ce4-994e-f20c242d99d0"/>
  <sequenceFlow id="sid-36ebfe0c-e8d9-4da4-a71d-cfaf47ba1803" sourceRef="sid-b78692a2-2a06-4ce4-994e-f20c242d99d0"
                targetRef="sid-c85642c4-a46c-494b-a32d-6806ae070fa3"/>
  <sequenceFlow id="sid-06ac31ac-601e-40ba-a9a0-f70222a97ba2" sourceRef="sid-c85642c4-a46c-494b-a32d-6806ae070fa3"
                targetRef="sid-8991e623-e51a-4765-8dc4-043c9a2ea97b"/>
</process>
public class TestCandidate {
  @Test
  public void testDeployment() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deploy = repositoryService.createDeployment().name("出差申请流程-Candidate").addClasspathResource("bpmn/evection-candidate.bpmn20.xml").deploy();
    System.out.println("流程部署id=" + deploy.getId());
    System.out.println("流程部署名字=" + deploy.getName());
  }

  @Test
  public void testStartProcess() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    String key = "testCandidate";
    runtimeService.startProcessInstanceByKey(key);
  }

  @Test
  public void findGroupTaskList() {
    String key = "testCandidate";
    String candidateUser = "wangwu";
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(key).taskCandidateUser(candidateUser).list();
    for (Task task : taskList) {
      System.out.println("========================");
      System.out.println("流程实例ID=" + task.getProcessInstanceId());
      System.out.println("任务id=" + task.getId());
      System.out.println("任务负责人=" + task.getAssignee());
    }
  }

  @Test
  public void claimTask() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    String taskId = "37502";
    String candidateUser = "wangwu";
    Task task = taskService.createTaskQuery().taskId(taskId).taskCandidateUser(candidateUser).singleResult();
    if (task != null) {
      taskService.claim(taskId, candidateUser);
      System.out.println("taskid-" + taskId + "-用户-" + candidateUser + "-拾取任务完成");
    }
  }

  @Test
  public void testAssigneeToGroupTask() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    String taskId = "37502";
    String assignee = "wangwu";
    Task task = taskService.createTaskQuery().taskId(taskId).taskAssignee(assignee).singleResult();
    if (task != null) {
      taskService.setAssignee(taskId, null);
      System.out.println("taskid-" + taskId + "-归还任务完成");
    }
  }

  @Test
  public void testAssigneeToCandidateUser() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    String taskId = "37502";
    String assignee = "wangwu";
    String candidateUser = "lisi";
    Task task = taskService.createTaskQuery().taskId(taskId).taskAssignee(assignee).singleResult();
    if (task != null) {
      taskService.setAssignee(taskId, candidateUser);
      System.out.println("taskid-" + taskId + "-交接任务完成");
    }
  }

  @Test
  public void completTask() {
    String key = "testCandidate";
    String assingee = "zhangsan";
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee(assingee).singleResult();
    if (task != null) {
      taskService.complete(task.getId());
    }
  }
}
16、排他网关
<process id="exclusive" isClosed="false" isExecutable="true" name="出差流程-排他" processType="None">
  <startEvent id="_2" name="StartEvent"/>
  <userTask activiti:assignee="tom" activiti:exclusive="true" id="_3" name="填写出差单"/>
  <sequenceFlow id="_4" sourceRef="_2" targetRef="_3"/>
  <userTask activiti:assignee="jerry" activiti:exclusive="true" id="_5" name="部门经理审批"/>
  <sequenceFlow id="_6" sourceRef="_3" targetRef="_5"/>
  <userTask activiti:assignee="jack" activiti:exclusive="true" id="_7" name="总经理审批"/>
  <userTask activiti:assignee="rose" activiti:exclusive="true" id="_9" name="财务审批"/>
  <sequenceFlow id="_11" sourceRef="_7" targetRef="_9"/>
  <endEvent id="_12" name="EndEvent"/>
  <sequenceFlow id="_13" sourceRef="_9" targetRef="_12"/>
  <exclusiveGateway gatewayDirection="Unspecified" id="_14" name="出差天数判断"/>
  <sequenceFlow id="_15" sourceRef="_5" targetRef="_14"/>
  <sequenceFlow id="_16" name="出差天数等于3天" sourceRef="_14" targetRef="_7">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num>=3}]]></conditionExpression>
  </sequenceFlow>
  <sequenceFlow id="_8" name="出差天数小于3天" sourceRef="_14" targetRef="_9">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num<3}]]></conditionExpression>
  </sequenceFlow>
</process>
public class ActivitiGatewayExclusive {
  @Test
  public void testDeployment() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deployment = repositoryService.createDeployment().addClasspathResource("bpmn/evection-exclusive.bpmn20.xml").name("出差申请流程-排他网关").deploy();
    System.out.println("流程部署id:" + deployment.getId());
    System.out.println("流程部署名称:" + deployment.getName());
  }

  @Test
  public void startProcess() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    String key = "exclusive";
    Map<String, Object> map = new HashMap<>();
    Evection evection = new Evection();
    evection.setNum(2d);
    map.put("evection", evection);
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, map);
    System.out.println("流程实例名称=" + processInstance.getName());
    System.out.println("流程定义id==" + processInstance.getProcessDefinitionId());
  }

  @Test
  public void completTask() {
    String key = "exclusive";
    String assingee = "jerry";
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee(assingee).singleResult();
    if (task != null) {
      taskService.complete(task.getId());
    }
  }
}
17、并行网关
<process id="parallel" isClosed="false" isExecutable="true" name="出差申请-并行网关" processType="None">
  <startEvent id="_2" name="StartEvent"/>
  <userTask activiti:assignee="tom" activiti:exclusive="true" id="_3" name="创建出差申请"/>
  <parallelGateway gatewayDirection="Unspecified" id="_4" name="ParallelGateway"/>
  <sequenceFlow id="_5" sourceRef="_3" targetRef="_4"/>
  <sequenceFlow id="_6" sourceRef="_2" targetRef="_3"/>
  <userTask activiti:assignee="jerry" activiti:exclusive="true" id="_7" name="技术经理审批"/>
  <userTask activiti:assignee="jack" activiti:exclusive="true" id="_8" name="项目经理审批"/>
  <sequenceFlow id="_9" sourceRef="_4" targetRef="_7">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num>=3}]]></conditionExpression>
  </sequenceFlow>
  <sequenceFlow id="_10" sourceRef="_4" targetRef="_8">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num<3}]]></conditionExpression>
  </sequenceFlow>
  <parallelGateway gatewayDirection="Unspecified" id="_11" name="ParallelGateway"/>
  <sequenceFlow id="_12" sourceRef="_7" targetRef="_11"/>
  <sequenceFlow id="_13" sourceRef="_8" targetRef="_11"/>
  <exclusiveGateway gatewayDirection="Unspecified" id="_14" name="ExclusiveGateway"/>
  <sequenceFlow id="_15" sourceRef="_11" targetRef="_14"/>
  <userTask activiti:assignee="rose" activiti:exclusive="true" id="_16" name="总经理审批"/>
  <sequenceFlow id="_17" sourceRef="_14" targetRef="_16">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num>=3}]]></conditionExpression>
  </sequenceFlow>
  <endEvent id="_18" name="EndEvent"/>
  <sequenceFlow id="_19" sourceRef="_16" targetRef="_18"/>
  <sequenceFlow id="_20" sourceRef="_14" targetRef="_18">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num<3}]]></conditionExpression>
  </sequenceFlow>
</process>
public class ActivitiGatewayParallel {
  @Test
  public void testDeployment() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deployment = repositoryService.createDeployment().addClasspathResource("bpmn/evection-parallel.bpmn20.xml").name("出差申请流程-并行网关").deploy();
    System.out.println("流程部署id:" + deployment.getId());
    System.out.println("流程部署名称:" + deployment.getName());
  }

  @Test
  public void startProcess() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    String key = "parallel";
    Map<String, Object> map = new HashMap<>();
    Evection evection = new Evection();
    evection.setNum(4d);
    map.put("evection", evection);
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, map);
    System.out.println("流程实例名称=" + processInstance.getName());
    System.out.println("流程定义id==" + processInstance.getProcessDefinitionId());
  }

  @Test
  public void testStartProcess() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("parallel");
    System.out.println("流程定义id:" + processInstance.getProcessDefinitionId());
    System.out.println("流程实例id:" + processInstance.getId());
    System.out.println("当前活动Id:" + processInstance.getActivityId());
  }

  @Test
  public void completTask() {
    String key = "parallel";
    String assingee = "jerry";
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee(assingee).singleResult();
    if (task != null) {
      taskService.complete(task.getId());
    }
  }
}
18、包含网关
<process id="inclusive" isClosed="false" isExecutable="true" name="出差申请-包含网关" processType="None">
  <startEvent id="_2" name="StartEvent"/>
  <userTask activiti:assignee="tom" activiti:exclusive="true" id="_3" name="出差申请"/>
  <inclusiveGateway gatewayDirection="Unspecified" id="_4" name="InclusiveGateway"/>
  <userTask activiti:assignee="jerry" activiti:exclusive="true" id="_5" name="技术经理"/>
  <userTask activiti:assignee="miki" activiti:exclusive="true" id="_6" name="人事经理"/>
  <userTask activiti:assignee="jack" activiti:exclusive="true" id="_7" name="项目经理"/>
  <inclusiveGateway gatewayDirection="Unspecified" id="_8" name="InclusiveGateway"/>
  <exclusiveGateway gatewayDirection="Unspecified" id="_9" name="ExclusiveGateway"/>
  <userTask activiti:assignee="rose" activiti:exclusive="true" id="_10" name="总经理"/>
  <endEvent id="_11" name="EndEvent"/>
  <sequenceFlow id="_12" sourceRef="_2" targetRef="_3"/>
  <sequenceFlow id="_13" sourceRef="_3" targetRef="_4"/>
  <sequenceFlow id="_14" sourceRef="_4" targetRef="_5">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num<3}]]></conditionExpression>
  </sequenceFlow>
  <sequenceFlow id="_15" sourceRef="_4" targetRef="_6"/>
  <sequenceFlow id="_16" sourceRef="_4" targetRef="_7">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num>=3}]]></conditionExpression>
  </sequenceFlow>
  <sequenceFlow id="_17" sourceRef="_5" targetRef="_8"/>
  <sequenceFlow id="_18" sourceRef="_6" targetRef="_8"/>
  <sequenceFlow id="_19" sourceRef="_7" targetRef="_8"/>
  <sequenceFlow id="_20" sourceRef="_8" targetRef="_9"/>
  <sequenceFlow id="_21" sourceRef="_9" targetRef="_10">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${evection.num>=3}]]></conditionExpression>
  </sequenceFlow>
  <sequenceFlow id="_22" sourceRef="_9" targetRef="_11"/>
  <sequenceFlow id="_23" sourceRef="_10" targetRef="_11"/>
</process>
public class ActivitiGatewayInclusive {
  @Test
  public void testDeployment() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deployment = repositoryService.createDeployment().addClasspathResource("bpmn/evection-inclusive.bpmn20.xml").name("出差申请流程-包含网关").deploy();
    System.out.println("流程部署id:" + deployment.getId());
    System.out.println("流程部署名称:" + deployment.getName());
  }

  @Test
  public void startProcess() {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    String key = "inclusive";
    Map<String, Object> map = new HashMap<>();
    Evection evection = new Evection();
    evection.setNum(4d);
    map.put("evection", evection);
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, map);
    System.out.println("流程实例名称=" + processInstance.getName());
    System.out.println("流程定义id==" + processInstance.getProcessDefinitionId());
  }

  @Test
  public void completTask() {
    String key = "inclusive";
    String assingee = "jack";
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee(assingee).singleResult();
    if (task != null) {
      taskService.complete(task.getId());
    }
  }
}
posted on 2023-12-20 21:50  一路繁花似锦绣前程  阅读(74)  评论(0编辑  收藏  举报