34、Spring Cloud 2.x之整合工作流Activiti

工作流在项目中非常常用,这里先来看两张图:

第一张:
34、Spring Cloud 2.x之整合工作流Activiti_第1张图片

对以上两张图进行说明:

假设这两张图就是华谊兄弟的请假流程图

图的组成部分:

人物:范冰冰、冯小刚、王中军

事件(动作):请假、批准、不批准
34、Spring Cloud 2.x之整合工作流Activiti_第2张图片

工作流(Workflow):就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档、信息或任务的过程自动进行,从而实现某个预期的业务目标,或者促使此目标的实现”。目前市面上常用的工作流引擎包括jbpm、activiti和OSWorkflowOSWorkflow。

1、新建项目sc-activiti,对应的pom.xml文件如下




    4.0.0

    com.sc
    sc-activiti
    0.0.1-SNAPSHOT

    sc-activiti-drools
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            

        
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.activiti
            activiti-spring-boot-starter-basic
            5.22.0
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                
                    maven-clean-plugin
                    3.0.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.7.0
                
                
                    maven-surefire-plugin
                    2.20.1
                
                
                    maven-jar-plugin
                    3.0.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
    


2、新建配置文件application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/sc?characterEncoding=utf8&useSSL=true
    username: root
    password: root
  activiti:
    check-process-definitions: false
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
  show-sql: true

server:
  port: 8081
  context-path: /
  session:
    timeout: 10
  tomcat:
    uri-encoding: UTF-8

3、新建请假流程文件holiday.bpmn



  
    
    
    
    
    
    
    
  
  
    
      
        
      
      
        
      
      
        
      
      
        
      
      
        
        
      
      
        
        
      
      
        
        
      
    
  

对应的bpmn文件的图形如下
34、Spring Cloud 2.x之整合工作流Activiti_第3张图片

4、新建springboot启动类

package sc.ad;


import org.activiti.spring.boot.SecurityAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class ActivitiApp {
    public static void main(String[] args)
    {
        SpringApplication.run(ActivitiApp.class, args);
    }

}

备注:一定要忽略SecurityAutoConfiguration.class,否则会出现如下异常。
34、Spring Cloud 2.x之整合工作流Activiti_第4张图片

5、新建一个controller,用来启动请假流程

package sc.ad.controller;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ADController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @RequestMapping("/ad")
    public void firstDemo() {

        // 根据bpmn文件部署流程
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday.bpmn").deploy();
        // 获取流程定义
        ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).singleResult();
        // 启动流程定义,返回流程实例
        ProcessInstance pi = runtimeService
                .startProcessInstanceById(processDefinition.getId());
        String processId = pi.getId();
        System.out.println("流程创建成功,当前流程实例ID:" + processId);

        Task task = taskService.createTaskQuery().processInstanceId(processId)
                .singleResult();
        System.out.println("第一次执行前,任务名称:" + task.getName());
        taskService.complete(task.getId());

        task = taskService.createTaskQuery().processInstanceId(processId)
                .singleResult();
        System.out.println("第二次执行前,任务名称:" + task.getName());
        taskService.complete(task.getId());

        task = taskService.createTaskQuery().processInstanceId(processId)
                .singleResult();
        System.out.println("task为null,任务执行完毕:" + task);
    }
}

6、启动项目并进行验证
查看日志,项目启动成功
34、Spring Cloud 2.x之整合工作流Activiti_第5张图片

访问:http://127.0.0.1:8081/ad
34、Spring Cloud 2.x之整合工作流Activiti_第6张图片

如果使用sql语句查看相应的表可以看到对应的库表里留下了相应的请假及审批记录信息。

你可能感兴趣的:(spring,springboot)