其他分享
首页 > 其他分享> > 记一次学习Activiti7&SpringBoot

记一次学习Activiti7&SpringBoot

作者:互联网

pom依赖
主要引入activiti依赖于jdbc和mysql依赖

<?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.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.thz</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter</artifactId>
            <version>7.0.0.Beta2</version>
        </dependency>

    </dependencies>

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

</project>

application.yml配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/activiti?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  activiti:
    #1.flase:默认值。activiti在启动时,对比数据库表中保存的版本,如果没有表或者版本不匹配,将抛出异常
    #2.true: activiti会对数据库中所有表进行更新操作。如果表不存在,则自动创建
    #3.create_drop: 在activiti启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)
    #4.drop-create: 在activiti启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)
    database-schema-update: true
    #检测历史表是否存在 activiti7默认没有开启数据库历史记录 启动数据库历史记录
    db-history-used: true
    #记录历史等级 可配置的历史级别有none, activity, audit, full #none:不保存任何的历史数据,因此,在流程执行过程中,这是最高效的。
    #activity:级别高于none,保存流程实例与流程行为,其他数据不保存。
    #audit:除activity级别会保存的数据外,还会保存全部的流程任务及其属性。audit为history的默认值。
    #full:保存历史数据的最高级别,除了会保存audit级别的数据外,还会保存其他全部流程相关的细节数据,包括一些 流程参数等。
    history-level: full
    #校验流程文件,默认校验resources下的processes文件夹里的流程文件
    check-process-definitions: false

准备流程文件 mytest.bpmn
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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/test">
  <process id="myTest" isClosed="false" isExecutable="true" processType="None">
    <startEvent id="Event_1fb6jx2" name="StartEvent"/>
    <userTask activiti:assignee="zhangsan" activiti:exclusive="true"  id="Activity_0vizqus"  name="创建出差申请单"/>
    <userTask activiti:exclusive="true"  id="Activity_0dm0jl0" activiti:assignee="jack" name="总经理审批"/>
    <endEvent id="Event_1ug08ii" name="EndEvent"/>
    <sequenceFlow id="Flow_13wzyw1" sourceRef="Event_1fb6jx2" targetRef="Activity_0vizqus"/>
    <sequenceFlow id="Flow_09pjrbx" sourceRef="Activity_0vizqus" targetRef="Activity_1aomhta"/>
    <sequenceFlow id="Flow_164iiwb" sourceRef="Activity_0dm0jl0" targetRef="Event_1ug08ii"/>
    <serviceTask id="Activity_1aomhta" name="部门经理审批" activiti:expression="${applyActivitiServiceImpl.finalAudit(execution)}"/>
    <sequenceFlow id="Flow_1p1kzwk" sourceRef="Activity_1aomhta" targetRef="Activity_0dm0jl0"/>
  </process>
  <bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
    <bpmndi:BPMNPlane bpmnElement="myTest">
      <bpmndi:BPMNShape bpmnElement="Event_1fb6jx2" id="Shape-Event_1fb6jx2">
        <omgdc:Bounds height="32.0" width="32.0" x="184.0" y="84.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="Activity_0vizqus" id="Shape-Activity_0vizqus">
        <omgdc:Bounds height="55.0" width="85.0" x="159.0" y="149.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="Activity_0dm0jl0" id="Shape-Activity_0dm0jl0">
        <omgdc:Bounds height="55.0" width="85.0" x="166.0" y="362.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="Event_1ug08ii" id="Shape-Event_1ug08ii">
        <omgdc:Bounds height="32.0" width="32.0" x="193.0" y="454.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="Activity_1aomhta" id="Shape-Activity_1aomhta">
        <omgdc:Bounds height="80.0" width="100.0" x="159.0" y="240.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="80.0" width="100.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="Flow_164iiwb" id="BPMNEdge_Flow_164iiwb" sourceElement="Activity_0dm0jl0" targetElement="Event_1ug08ii">
        <omgdi:waypoint x="209.0" y="417.0"/>
        <omgdi:waypoint x="209.0" y="454.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="Flow_09pjrbx" id="BPMNEdge_Flow_09pjrbx" sourceElement="Activity_0vizqus" targetElement="Activity_1aomhta">
        <omgdi:waypoint x="201.5" y="204.0"/>
        <omgdi:waypoint x="201.5" y="240.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="Flow_13wzyw1" id="BPMNEdge_Flow_13wzyw1" sourceElement="Event_1fb6jx2" targetElement="Activity_0vizqus">
        <omgdi:waypoint x="200.0" y="116.0"/>
        <omgdi:waypoint x="200.0" y="149.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="Flow_1p1kzwk" id="BPMNEdge_Flow_1p1kzwk" sourceElement="Activity_1aomhta" targetElement="Activity_0dm0jl0">
        <omgdi:waypoint x="208.5" y="320.0"/>
        <omgdi:waypoint x="208.5" y="362.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

启动类排除Spring Security
因为Activiti7与SpringBoot整合后,默认情况下,集成了SpringSecurity安全框架,这样我们就要去准备SpringSecurity整合进来的相关用户权限配置信息。这样就很麻烦,这里可以先排除掉。

@SpringBootApplication(
        exclude = {
                org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class,
                org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class
        }
)

controller

@RestController
public class MyController {
    @Autowired
    ApplyActivitiService applyActivitiService;

    @RequestMapping("/submit")
    public String submitApply(){
        return applyActivitiService.submitApply();
    }

    @RequestMapping("/create")
    public String createApply(String assignee){
        return applyActivitiService.createApply(assignee);
    }

    @RequestMapping("/secondAudit")
    public String secondAudit(String assignee){
        return applyActivitiService.secondAudit(assignee);
    }
}

service

public interface ApplyActivitiService {
    public String submitApply();
    public String createApply(String assignee);
    public String secondAudit(String assignee);
}

实现类

@Service
public class ApplyActivitiServiceImpl implements ApplyActivitiService {

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @PostConstruct
    public void init(){
        // 3、使用RepositoryService进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/mytest.bpmn") // 添加bpmn资源
//                .addClasspathResource("bpmn/evection.png") // 添加png资源
                .name("出差申请流程")
                .deploy();
        // 4、输出部署信息
        System.out.println("流程部署id:" + deployment.getId());
        System.out.println("流程部署名称:" + deployment.getName());
    }


    @Override
    public String submitApply() {
        // 3、根据流程定义Id启动流程
        Map<String,Object> variables = new HashMap<>();
        variables.put("myid","123456789");
        ProcessInstance processInstance = runtimeService .startProcessInstanceByKey("myTest",variables);
        // 输出内容
        System.out.println("流程定义id:" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例id:" + processInstance.getId());
        System.out.println("当前活动Id:" + processInstance.getActivityId());
        System.out.println("Name:" + processInstance.getName());
        System.out.println("LocalizedName:" + processInstance.getLocalizedName());
        System.out.println("ProcessDefinitionName:" + processInstance.getProcessDefinitionName());
        return "success";
    }

    @Override
    public String createApply(String assignee) {
        // 根据流程key 和 任务负责人 查询任务
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("myTest") //流程Key
                .taskAssignee(assignee)//只查询该任务负责人的任务
//                .taskId("Activity_0vizqus")
                .list();
        for (Task task : list) {
            System.out.println("流程实例id:" + task.getProcessInstanceId());
            System.out.println("任务id:" + task.getId());
            System.out.println("任务负责人:" + task.getAssignee());
            System.out.println("任务名称:" + task.getName());
            String myid = (String) runtimeService.getVariable(task.getExecutionId(),"myid");
            System.out.println("myid:" + myid);
            Map<String,Object> map = new HashMap<>();
            map.put("bb","BB");
            taskService.complete(task.getId(),map);
        }
        return "scuccess";
    }

    @Override
    public String secondAudit(String assignee) {
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("myTest") //流程Key
                .taskAssignee(assignee)//只查询该任务负责人的任务
//                .taskId("Activity_0dm0jl0")
                .list();
        for (Task task : list) {
            String cc = (String) runtimeService.getVariable(task.getExecutionId(),"cc");
            System.out.println("cc:" + cc);
            taskService.complete(task.getId());
        }
        return "scuccess";
    }

    public void finalAudit(DelegateExecution execution) {
        String bb = (String) execution.getVariable("bb");
        System.out.println("bb:" + bb);
        String myid = (String) execution.getVariable("myid");
        System.out.println("myid:" + myid);
        execution.setVariable("cc","987654321");
    }

}

启动测试

依次调用下面接口

localhost:8080/submit
localhost:8080/create?assignee=zhangsan
localhost:8080/secondAudit?assignee=jack

控制台打印

Connected to the target VM, address: '127.0.0.1:0', transport: 'socket'
SLF4J: The requested version 1.7.16 by your slf4j binding is not compatible with [1.6]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

2021-04-27 14:49:09.326  INFO 15484 --- [           main] com.thz.demo.DemoApplication             : Starting DemoApplication using Java 1.8.0_211 on DESKTOP-0B0L3CT with PID 15484 (D:\test_code\activiti\activitiDemo1\target\classes started by Tang in D:\test_code\activiti\activitiDemo1)
2021-04-27 14:49:09.328  INFO 15484 --- [           main] com.thz.demo.DemoApplication             : No active profile set, falling back to default profiles: default
2021-04-27 14:49:10.256  INFO 15484 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@375084c9' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-04-27 14:49:10.259  INFO 15484 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityConfig' of type [org.activiti.spring.boot.MethodSecurityConfig$$EnhancerBySpringCGLIB$$91e5f2b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-04-27 14:49:10.264  INFO 15484 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-04-27 14:49:10.412  INFO 15484 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-04-27 14:49:10.417  INFO 15484 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-04-27 14:49:10.418  INFO 15484 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-04-27 14:49:10.465  INFO 15484 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-04-27 14:49:10.465  INFO 15484 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1106 ms
2021-04-27 14:49:10.591  INFO 15484 --- [           main] c.t.d.c.DemoApplicationConfiguration     : > Registering new user: jack with the following Authorities[[ROLE_ACTIVITI_USER, GROUP_activitiTeam]]
2021-04-27 14:49:10.658  INFO 15484 --- [           main] c.t.d.c.DemoApplicationConfiguration     : > Registering new user: rose with the following Authorities[[ROLE_ACTIVITI_USER, GROUP_activitiTeam]]
2021-04-27 14:49:10.721  INFO 15484 --- [           main] c.t.d.c.DemoApplicationConfiguration     : > Registering new user: tom with the following Authorities[[ROLE_ACTIVITI_USER, GROUP_activitiTeam]]
2021-04-27 14:49:10.783  INFO 15484 --- [           main] c.t.d.c.DemoApplicationConfiguration     : > Registering new user: other with the following Authorities[[ROLE_ACTIVITI_USER, GROUP_otherTeam]]
2021-04-27 14:49:10.845  INFO 15484 --- [           main] c.t.d.c.DemoApplicationConfiguration     : > Registering new user: system with the following Authorities[[ROLE_ACTIVITI_USER]]
2021-04-27 14:49:10.906  INFO 15484 --- [           main] c.t.d.c.DemoApplicationConfiguration     : > Registering new user: admin with the following Authorities[[ROLE_ACTIVITI_ADMIN]]
2021-04-27 14:49:11.014  INFO 15484 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-27 14:49:11.102  INFO 15484 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-04-27 14:49:11.199  INFO 15484 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-04-27 14:49:11.611  INFO 15484 --- [           main] aultActiviti5CompatibilityHandlerFactory : Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
2021-04-27 14:49:11.633  INFO 15484 --- [           main] o.a.engine.impl.ProcessEngineImpl        : ProcessEngine default created
2021-04-27 14:49:11.758  INFO 15484 --- [           main] o.a.e.impl.bpmn.deployer.BpmnDeployer    : Process deployed: {id: myTest:2:ac5d7800-a724-11eb-9fa0-70b5e84c1e7c, key: myTest}
流程部署id:ac4fbc5e-a724-11eb-9fa0-70b5e84c1e7c
流程部署名称:出差申请流程
2021-04-27 14:49:12.234  INFO 15484 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-04-27 14:49:12.296  INFO 15484 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-04-27 14:49:12.307  INFO 15484 --- [           main] com.thz.demo.DemoApplication             : Started DemoApplication in 3.27 seconds (JVM running for 3.685)
2021-04-27 14:49:12.454  INFO 15484 --- [1)-192.168.1.87] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-27 14:49:12.454  INFO 15484 --- [1)-192.168.1.87] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-04-27 14:49:12.455  INFO 15484 --- [1)-192.168.1.87] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
流程定义id:myTest:2:ac5d7800-a724-11eb-9fa0-70b5e84c1e7c
流程实例id:b664e9a1-a724-11eb-9fa0-70b5e84c1e7c
当前活动Id:null
Name:null
LocalizedName:null
ProcessDefinitionName:null
流程实例id:b664e9a1-a724-11eb-9fa0-70b5e84c1e7c
任务id:b6669757-a724-11eb-9fa0-70b5e84c1e7c
任务负责人:zhangsan
任务名称:创建出差申请单
myid:123456789
bb:BB
myid:123456789
cc:987654321

分析

使用@PostConstruct注解在项目启动时自动部署流程
依次执行的三个接口分别执行了启动流程(个人理解为和类 的实例化差不多)、创建出差申请单、总经理审批。因为创建出差申请单和总经理审批为UserTask,需要调用接口执行任务使流程继续向下流动。部门经理审批为ServiceTask,在创建出差申请单后自动完成任务。如果debug启动项目,并在finalAudit方法中打上断点,就会发现在执行完localhost:8080/create?assignee=zhangsan接口后代码并没有执行完,而是进入了finalAudit中的断点。这表明在完成创建申请单任务后,activiti自动去执行下一个ServiceTask了。

关于流程变量,即上面代码中的myid、bb、cc。这些流程变量的作用域为整个流程实例,在设置之后的所有任务都可以使用。通常结合UEL表达式用来控制流程的走向等。
获取某个执行点的变量
RuntimeService
Map<String, Object> getVariables(String executionId);
DelegateExecution
Map<String, Object> getVariables();
Object getVariable(String variableName);
设置某个执行点的变量
RuntimeService
void setVariable(String executionId, String variableName, Object value);
void setVariables(String executionId, Map<String, ? extends Object> variables);
DelegateExecution
void setVariable(String variableName, Object value);
void setVariables(Map<String, ? extends Object> variables);

标签:INFO,27,String,15484,学习,Activiti7,2021,14,SpringBoot
来源: https://blog.csdn.net/m0_49558851/article/details/116241444