Camunda工作流引擎简单入门

官网:https://camunda.com/

官方文档:https://docs.camunda.org/get-started/spring-boot/project-setup/

一、简介

    Camunda是一个工作流引擎,执行Bpmn2.0标准,因此依赖于基于bpmn的流程图(本质上是一个xml文件)

二、一个完整的报销 demo 入门

1、创建一个SpringBoot项目,导入数据库依赖、Camunda 等依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.camunda.bpm.springboot/camunda-bpm-spring-boot-starter-webapp -->
        <dependency>
            <groupId>org.camunda.bpm.springboot</groupId>
            <artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
            <version>3.4.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.camunda.bpm.springboot/camunda-bpm-spring-boot-starter-rest -->
        <dependency>
            <groupId>org.camunda.bpm.springboot</groupId>
            <artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
            <version>3.4.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.6</version>
        </dependency>

application.yml 部分配置:

spring:
  application:
    name: camunda-demo
  #数据源配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/camunda-demo?serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
camunda:
  bpm:
    #配置账户密码来访问Camunda自带的管理界面
    admin-user:
      id: admin
      password: admin
      first-name: admin
    filter:
      create: All tasks
    #禁止自动部署resources下面的bpmn文件
    auto-deployment-enabled: false
2、启动后会生成47张表:

3、访问 http://localhost:8080/

4、下载bpmn建模工具:https://camunda.com/download/modeler/

5、画一个报销流程图,申请人为shangsan、人事为lisi、经理为wangwu,保存后放到项目resources目录下面,同时进入控制台Admin目录下建立相关的人员信息。

注意:报销金额判断条件使用 Expression 表达式

6、流程定义部署

    @PostMapping("/deploy")
    public void deploy() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("BPMN/apply.bpmn")
                .deploy();
        System.out.println(deploy.getId());
    }

用PostMan发起流程后会在ACT_RE_DEPLOYMENT表中看到一个流程的实例

7、启动流程实例

    @PostMapping("/start")
    public void runProcinst(){
        Map<String,Object> params = new HashMap<>();
        params.put("money",2001);
        ProcessInstance holiday = runtimeService.startProcessInstanceByKey("apply",params);
    }

然后切换到zhangsan用户,在控制台TaskList下查看

8、审批通过,在审批的过程中可以随意添加审批信息

    @PostMapping("/taskComplete")
    public void taskComplete(){
        Task task = taskService.createTaskQuery()
                .taskAssignee("zhangsan")
                .singleResult();
        params.put("approve2","lisi");
        Map<String,Object> params = new HashMap<>();
        taskService.complete(task.getId(),params);
    }

切换 lisi 用户在控制台查看任务

 

9、进入控制台在Cockplt中查看流程走到哪(因为我们的money设置的是2001,大于2000,所以流程走到下面的那个分支)

10、其他关于流程的一些简单操作

package com.camunda.camundademo.controller;

import org.camunda.bpm.engine.*;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.repository.ProcessDefinitionQuery;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: camunda-demo
 * @Description:
 * @Author: zhouhong
 * @Create: 2021-07-08 10:06
 **/

@RestController
public class Test {

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;
    @Autowired
    ProcessEngine processEngine;

    @Autowired
    ProcessEngine engine;

    /**
    * @Description: 流程定义部署
    * @Author: zhouhong
    * @Date: 2021/7/8
    */
    @PostMapping("/deploy")
    public void deploy() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("BPMN/apply.bpmn")
                .deploy();
        System.out.println(deploy.getId());
    }

    /**
     * @Description: 开启一个流程实例
     * @Author: zhouhong
     * @Date: 2021/7/8
     */
    @PostMapping("/start")
    public void runProcinst(){
        Map<String,Object> params = new HashMap<>();
        params.put("money",2001);
        ProcessInstance apply = runtimeService.startProcessInstanceByKey("apply",params);
        System.out.println(apply.getProcessDefinitionId());
        System.out.println(apply.getId());
        System.out.println(apply.getProcessInstanceId());
    }

    /**
     * @Description: 流程任务查询
     * @Author: zhouhong
     * @Date: 2021/7/8
     */
    @PostMapping("/taskquery")
    public void taskQuery() {
        List<Task> tasks = taskService.createTaskQuery()
                .processDefinitionKey("apply")
                .list();
         for (Task task : tasks) {
            System.out.println(task.getAssignee());
            System.out.println(task.getId());
            System.out.println(task.getName());
            System.out.println(task.getTenantId());
        }
    }

    /**
    * @Description: 当前需要处理的任务
    * @Author: zhouhong
    * @Date: 2021/7/8
    */
    @PostMapping("/mytaskquery")
    public List<HistoricTaskInstance> myTaskQuery() {
        List<HistoricTaskInstance> instances = engine.getHistoryService().createHistoricTaskInstanceQuery()
                .taskAssignee("lisi").unfinished().orderByHistoricActivityInstanceStartTime().asc().list();
        return instances;
    }

    /**
     * @Description: 流程任务执行
     * @Author: zhouhong
     * @Date: 2021/7/8
     */
    @PostMapping("/taskComplete")
    public void taskComplete(){
        //目前lisi只有一个任务,业务中根据场景选择其他合适的方式
        Task task = taskService.createTaskQuery()
                .taskAssignee("zhangsan")
                .singleResult();
        Map<String,Object> params = new HashMap<>();
        params.put("approve2","lisi");
        taskService.complete(task.getId(),params);
    }

    /**
     * @Description: 流程定义查询
     * @Author: zhouhong
     * @Date: 2021/7/8
     */
    @PostMapping("/queryDefine")
    public void queryDefine(){
        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> definitions = query.processDefinitionKey("apply")
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
        for (ProcessDefinition definition : definitions) {
            System.out.println(definition.getDeploymentId());
            System.out.println(definition.getName());
            System.out.println(definition.getVersion());
            System.out.println(definition.getId());
            System.out.println(definition.getKey());
        }
    }

    /**
     * 删除流程定义
     */
    @PostMapping("/deleteDefine")
    public void deleteDefine(){
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> definitions = processDefinitionQuery.processDefinitionKey("apply")
                .orderByProcessDefinitionVersion()
                .asc()
                .list();
        ProcessDefinition processDefinition = definitions.get(0);
        if (processDefinition != null){
            repositoryService.deleteDeployment(processDefinition.getDeploymentId(),true);
        }
    }

    /**
     * 查询历史信息
     */
    @PostMapping("/queryHistory")
    public void queryHistory(){
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .finished()
                .orderByHistoricActivityInstanceEndTime()
                .asc()
                .list();
        for (HistoricActivityInstance instance : list) {
            System.out.println(instance.getActivityId());
            System.out.println(instance.getProcessDefinitionKey());
            System.out.println(instance.getAssignee());
            System.out.println(instance.getStartTime());
            System.out.println(instance.getEndTime());
            System.out.println("=============================");
        }
    }

    /**
     * 启动一个流程实例,并且添加一个业务key
     * 业务key 可以在 act_ru_execution 中看到
     */

    public void startProcInstAddBusinessKey(){
        ProcessInstance apply = runtimeService.startProcessInstanceByKey("apply", "aaaa-scsc-89uc");
        System.out.println(apply.getBusinessKey());
    }

}

 


已有 0 条评论

    欢迎您,新朋友,感谢参与互动!