首页 > 编程语言 > Springboot集成activity过程图解
2020
09-28

Springboot集成activity过程图解

1.第一步添加bpmn文件得插件,不然没法查看和编辑bpmn文件,添加插件的方法各自百度即可,很简单

2.安装好bpmn插件后开始新建bpmn文件,也就是画流程图

一般是在代码中进行指定流程审批人的,我这里只做入门案例

3.添加pom依赖

这里包含了在线设计流程图的相关依赖,在下一篇会写出来,项目中一般都是在前端在线设计;所以直接放上去也没事

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com</groupId>
  <artifactId>springboot_activiti</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot_activiti</name>
  <description>Demo project for Spring Boot</description>



  <properties>
    <springboot.version>2.2.0.RELEASE</springboot.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <activiti.version>6.0.0</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>
      <exclusions>
        <exclusion>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-core-asl</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <artifactId>spring-context</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-jdbc</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-tx</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-orm</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-beans</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>mybatis</artifactId>
          <groupId>org.mybatis</groupId>
        </exclusion>
        <exclusion>
          <artifactId>activation</artifactId>
          <groupId>javax.activation</groupId>
        </exclusion>
      </exclusions>

    </dependency>

    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-spring-boot-starter-basic</artifactId>
      <version>${activiti.version}</version>
    </dependency>

    <!-- Activiti 流程图 -->
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-common-rest</artifactId>
      <version>${activiti.version}</version>
    </dependency>
    <!-- Activiti 在线设计 -->
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-modeler</artifactId>
      <version>5.22.0</version>
      <exclusions>
        <exclusion>
          <artifactId>spring-beans</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-context</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-core</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-tx</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-web</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-security-config</artifactId>
          <groupId>org.springframework.security</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-security-core</artifactId>
          <groupId>org.springframework.security</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-security-crypto</artifactId>
          <groupId>org.springframework.security</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-security-web</artifactId>
          <groupId>org.springframework.security</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-webmvc</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>activation</artifactId>
          <groupId>javax.activation</groupId>
        </exclusion>
        <exclusion>
          <artifactId>commons-io</artifactId>
          <groupId>commons-io</groupId>
        </exclusion>
      </exclusions>
    </dependency>




    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>${springboot.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.3</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.10.0</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>


  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>8081</port>
          <path>/activity</path>
          <systemProperties>
            <com.sun.management.jmxremote.port>4000</com.sun.management.jmxremote.port>
          </systemProperties>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

4.yml文件

这里采用的数据库还是mysql

这里有个需要注意的地方,springboot2.2中的driver-class-name值变了,不是以前的那个了,不然会报错

spring:
datasource:
url: jdbc:mysql://localhost:3307/t2?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
activiti:
# 自动部署验证设置:true-开启(默认)、false-关闭
check-process-definitions: false
#自动部署文件路径后缀
# process-definition-location-prefix: classpath:/processes/
# process-definition-location-suffixes:
# - **.bpmn
# - **.bpmn20.xml
server:
port: 8082

5.配置已经好了,下面开始写代码了,只是初步写了一些新建流程,正常提交流程,直接结束流程,以及查询待办的案例,细节还需要各自自己去调试学习

controller

package com.springboot_activiti.controller;

import com.springboot_activiti.service.ActivityConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/activiti")
public class ActivitiController {

  @Autowired
  private ActivityConsumerService activityConsumerService;

  /**
   * 注册流程
   * @return
   */
  @RequestMapping("/createDeployment")
  public Boolean createDeployment(){
    return activityConsumerService.createDeployment();
  }

  /**
   * 启动流程
   * @return
   */
  @RequestMapping("/startActivityDemo")
  public Boolean startActivityDemo(){
    return activityConsumerService.startActivityDemo("test01");
  }

  /**
   * 获取待办
   * @return
   */
  @RequestMapping("/getTaskList")
  public boolean getTaskList(){
    return activityConsumerService.getTaskList();
  }

  /**
   * 提交
   * @param taskId
   * @return
   */
  @RequestMapping("/complete")
  public boolean complete(String taskId){
    return activityConsumerService.complete(taskId);
  }
  /**
   * 根据流程id直接结束流程
   * @paramru
   * @return
   */
  @RequestMapping("/deleteProcessInstance")
  public boolean deleteProcessInstance(String runId){
    return activityConsumerService.deleteProcessInstance(runId);
  }
}

service

package com.springboot_activiti.service;
public interface ActivityConsumerService {
  boolean createDeployment();
  boolean startActivityDemo(String key);
  boolean getTaskList();
  boolean complete(String taskId);
  boolean deleteProcessInstance(String runId);
}

实现类

package com.springboot_activiti.service;


import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("activityService")
public class ActivityConsumerServiceImpl implements ActivityConsumerService {


  @Autowired
  private RuntimeService runtimeService;
  @Autowired
  private TaskService taskService;

  @Autowired
  private RepositoryService repositoryService;

  /**
   * 注册一个流程
   * @return
   */
  @Override
  public boolean createDeployment() {
    DeploymentBuilder builder=repositoryService.createDeployment();
    builder.addClasspathResource("test01.bpmn");
    builder.deploy();
    return true;
  }

  /**
   * 查询待办
   * @return
   */
  public boolean getTaskList(){
    //获取待办的一些信息,这里可以传入需要查询的用户,
    //我这里查询的所有待办
    List<Task> tasks = taskService.createTaskQuery().list();
    for (Task t:tasks) {
      System.out.println(t.getCreateTime());
      System.out.println(t.getId());
      System.out.println(t.getName());
      System.out.println(t.getProcessInstanceId());
      System.out.println(t.getTaskDefinitionKey());
      System.out.println(t.getParentTaskId());
    }
    return true;
  }


  /**
   * 根据流程key开启一个流程
   * @param key
   * @return
   */
  @Override
  public boolean startActivityDemo(String key) {
    ProcessInstance test01 = runtimeService.startProcessInstanceByKey(key);
    String id = test01.getId();
    System.out.println("流程id="+id);
    /* */
  /*  String assignee = "abc";*/
    return true;
  }

  /**
   * 根据任务id提交任务
   * @param taskId
   * @return
   */
  @Override
  public boolean complete(String taskId){
    taskService.complete(taskId);
    return true;
  }

  /**
   * 根据流程id直接结束流程
   * @param runId
   * @return
   */
  @Override
  public boolean deleteProcessInstance(String runId){
    runtimeService.deleteProcessInstance(runId,"结束");
    return true;
  }
}

启动类,这里存在一个在线视图设计的验证处理,

package com.springboot_activiti;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com")
@SpringBootApplication
@EnableAutoConfiguration(exclude = {
org.activiti.spring.boot.SecurityAutoConfiguration.class
    })
public class SpringbootActivitiApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringbootActivitiApplication.class, args);
  }
}

所有的代码已经写好了

备注说明:

项目启动会新建28张表,各自代表的含义自行百度把

因为我的数据都是直接在流程图bpmn文件里面写好的,实际中肯定是在代码里面指定的

首先访问注册流程

启动流程(会返回一个流程id)

查看待办就可以获取相关的信息了

正常提交任务,会审批到下一节点(查看待办可以得到任务id)

直接结束流程(非正常结束,启动流程的id传入即可)

请求案例

查询待办

启动流程

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧