Activiti集成SpringBoot实现流程部署的简单例子

2023-12-13 04:56:36

学习SpringBoot整合Activiti遇到的几个问题
1.连接controller时跳转登录页面,这是因为Activiti依赖中存在security包
可以选择在启动类上屏蔽security.class和org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
在这里插入图片描述
但是因为我依赖中没加security的依赖决定在yml配置中添加账号密码
不添加这个的话,原始账号是user 密码是启动类运行 下面提供的编码
在这里插入图片描述
配置中可以把自动部署关掉在这里插入图片描述
在这里插入图片描述

搞完这些就可以解决登录问题
在这里插入图片描述
找了很多文章,没有找到一个完美的集成,东拼西凑自己写了下。有的地方可能存在问题目前就是想实现在SpringBoot中成功部署流程。
首先application.yml配置

# 应用服务 WEB 访问端口
server.port: 8081
spring:

  # 数据源配置
 datasource:
  driver-class-name: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://localhost:3306/activiti7? nullCatalogMeansCurrent=true&serverTimezone=UTC&characterEncoding=UTF8&nullCatalogMeansCurrent=true
  username: "root"
  password: "123456"
  type: com.zaxxer.hikari.HikariDataSource
  hikari:
  # 等待连接池分配连接的最大时间(毫秒),超过这个时长还没有可用的连接,则会抛出SQLException
   connection-timeout: 30000
  # 最小连接数
   minimum-idle: 5
  # 最大连接数
   maximum-pool-size: 20
  # 自动提交
   auto-commit: true
  # 连接超时的最大时长(毫秒),超时则会被释放(retired)
   idle-timeout: 600000
  # 连接池的名字
   pool-name: DataSourceHikariCP
  # 连接池的最大生命时长(毫秒),超时则会被释放(retired)
   max-lifetime: 18000000
 security:
    user:
     name: root
     password: 123456
 # activiti7配置
 activiti:
  # 自动部署验证设置:true-开启(默认)、false-关闭
  check-process-definitions: false
  # 保存历史数据
  history-level: full
  # 检测历史表是否存在
  db-history-used: true
  # 关闭自动部署
  deployment-mode: never-fail
  # 对数据库中所有表进行更新操作,如果表不存在,则自动创建
  # create_drop:启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)
  # drop-create:启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)
  database-schema-update: true
  # 解决频繁查询SQL问题
  async-executor-activate: false

然后依赖

<?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>
    <groupId>com.yao</groupId>
    <artifactId>activiti7demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>activiti7demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.4.2</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter</artifactId>
            <version>7.1.0.M2</version>
            <!--<exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
            </exclusions>-->
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.activiti.dependencies</groupId>
            <artifactId>activiti-dependencies</artifactId>
            <version>7.1.0.M2</version>
            <type>pom</type>
        </dependency>
        <!-- 生成流程图 -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-image-generator</artifactId>
            <version>7.1.0.M2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>

        <dependency>
            <groupId>org.activiti.api</groupId>
            <artifactId>activiti-api-process-model</artifactId>
            <version>7.1.0.M2</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>7.1.0.M2</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-model</artifactId>
            <version>7.1.0.M2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.yao.activiti7demo.Activiti7demoApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

然后是BPM.xml看好它的路径

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef">
  <process id="test1" name="test1" isExecutable="true">
    <documentation>test1</documentation>
    <startEvent id="startEvent1"></startEvent>
    <userTask id="sid-C369C919-5DBA-43E5-84E0-F9B665F0F3B2" name="人事审批" activiti:assignee="张三">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-E740DBC1-2D73-45CC-9CEC-1559D0094A23" sourceRef="startEvent1" targetRef="sid-C369C919-5DBA-43E5-84E0-F9B665F0F3B2"></sequenceFlow>
    <userTask id="sid-F7256F24-C3EB-4EEA-956B-678D50AFB171" name="经理审批" activiti:assignee="lisi">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-910A5074-B40F-4B8A-BF4F-77A4A3B241AC" sourceRef="sid-C369C919-5DBA-43E5-84E0-F9B665F0F3B2" targetRef="sid-F7256F24-C3EB-4EEA-956B-678D50AFB171"></sequenceFlow>
    <endEvent id="sid-789A2607-3E5C-450B-BC1F-196552938A9C"></endEvent>
    <sequenceFlow id="sid-E28ACB27-F588-4D1B-AD0C-A988877BB89F" sourceRef="sid-F7256F24-C3EB-4EEA-956B-678D50AFB171" targetRef="sid-789A2607-3E5C-450B-BC1F-196552938A9C"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_test1">
    <bpmndi:BPMNPlane bpmnElement="test1" id="BPMNPlane_test1">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="100.0" y="163.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-C369C919-5DBA-43E5-84E0-F9B665F0F3B2" id="BPMNShape_sid-C369C919-5DBA-43E5-84E0-F9B665F0F3B2">
        <omgdc:Bounds height="80.0" width="100.0" x="175.0" y="138.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-F7256F24-C3EB-4EEA-956B-678D50AFB171" id="BPMNShape_sid-F7256F24-C3EB-4EEA-956B-678D50AFB171">
        <omgdc:Bounds height="80.0" width="100.0" x="314.9999953061343" y="134.99999798834327"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-789A2607-3E5C-450B-BC1F-196552938A9C" id="BPMNShape_sid-789A2607-3E5C-450B-BC1F-196552938A9C">
        <omgdc:Bounds height="28.0" width="28.0" x="465.0" y="164.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-910A5074-B40F-4B8A-BF4F-77A4A3B241AC" id="BPMNEdge_sid-910A5074-B40F-4B8A-BF4F-77A4A3B241AC">
        <omgdi:waypoint x="275.0" y="176.92857067420013"></omgdi:waypoint>
        <omgdi:waypoint x="314.9999953061343" y="176.07142731414314"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-E28ACB27-F588-4D1B-AD0C-A988877BB89F" id="BPMNEdge_sid-E28ACB27-F588-4D1B-AD0C-A988877BB89F">
        <omgdi:waypoint x="414.9999953061343" y="174.99999798834327"></omgdi:waypoint>
        <omgdi:waypoint x="439.9999976530671" y="174.99999798834327"></omgdi:waypoint>
        <omgdi:waypoint x="439.9999976530671" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="465.0" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-E740DBC1-2D73-45CC-9CEC-1559D0094A23" id="BPMNEdge_sid-E740DBC1-2D73-45CC-9CEC-1559D0094A23">
        <omgdi:waypoint x="130.0" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="175.0" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

Controller层

package com.yao.activiti7demo.testMyController;


import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProcessController {
    @Autowired
    private ProcessEngine processEngine;

    @RequestMapping("/startProcess")
    public String startProcess(@RequestParam("name") String name) {
        System.out.println("name"+name);
       // ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("flow/test1.bpmn20.xml").name("测试流程").deploy();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();

        RuntimeService runtimeService = processEngine.getRuntimeService();
        runtimeService.startProcessInstanceById(processDefinition.getId(), name);

        return "Process started successfully";//这里可以跳转到流程页面 查询历史等
    }
}

启动类
在这里插入图片描述
在这里插入图片描述
浏览器访问路径http://localhost:8081/startProcess?name=张三
在这里插入图片描述

数据库中已经成功部署
在这里插入图片描述

文章来源:https://blog.csdn.net/qq_48128672/article/details/134952081
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。