SpringBoot整合Activiti工作流框架的使用

 更新时间:2022年02月18日 10:45:46   作者:MarlonBrando1998  
本文主要介绍了SpringBoot整合Activiti工作流框架的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Activiti 介绍

  • Activiti是一个开源的工作流引擎,它实现了BPMN 2.0规范,可以发布设计好的流程定义,并通过api进行流程调度。Activiti 作为一个遵从 Apache 许可的工作流和业务流程管理开源平台,其核心是基于 Java 的超快速、超稳定的 BPMN2.0 流程引擎,强调流程服务的可嵌入性和可扩展性,同时更加强调面向业务人员。
  • 简单来说activiti是一个业务流程管理引擎,会沿着设计者设计好的流程,一步一步的执行下去,直到终点。

SpringBoot 整合

配置

activiti会框架会创建一系列的表,所以要配置相关数据库的信息,需要注意的是,在url中,添加了针对数据库的条件,其中最后一条nullCatalogMeansCurrent=true非常重要,至于有什么用就不概述了,但是没有这条语句的话就无法自动创建对应的二十八张表。

server:
  port: 8014

spring:
  application:
    name: workflow
  datasource:
    name: mysqlDatasource
    url: jdbc:mysql://localhost:3306/core?useUnicode=true&nullCatalogMeansCurrent=true
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    # 监控统计拦截的filters,如果启用log4j记得添加依赖
    filters: stat,wall
  # activiti
  activiti:
    #每次应用启动不检查Activiti数据表是否存在及版本号是否匹配,提升应用启动速度
    database-schema-update: true
    #在项目单独作为一个引擎,本身不部署流程的时候,如果resources目录没有“processes”目录,启动项目报错–找不到processes目录。需要在配置文件中添加以下内容:
    check-process-definitions: false
    process-definition-location-prefix: classpath:/processes/
    process-definition-location-suffixes:
      -**.bpmn
      -**.bpmn20.xml
    #保存历史数据级别设置为full最高级别,便于历史数据的追溯
    history-level: full
  # activiti 安全访问
  security:
    basic:
      enabled: true
    user:
      name: root
      password: root

版本问题

  • 注意 SpringBootActiviti 的版本问题
  • springboot2.0不能与activiti6.0.0直接集成使用,因为activiti6.0.0出来的时候springboot2.0还没有出来,activiti6.0.0 支持springboot1.2.6以上,2.0.0以下的版本。

使用 starter

依赖

这个版本满足高版本的springboot,直接使用就行

<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-spring-boot-starter</artifactId>
    <version>7.1.0.M3.1</version>
</dependency>

需要注意的是,这里的依赖版本,需要对应数据库中act_ge_property表中schema.version版本信息,所以一般不建议在创建完表之后修改依赖信息

启动项目成功后自动创建表

请添加图片描述

需要在配置文件中加上 activiti-security的配置

 # activiti 安全访问
  security:
    basic:
      enabled: true
    user:
      name: root
      password: root

不使用 starter

依赖

<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-spring</artifactId>
    <version>6.0.0</version>
</dependency>

配置代码

@Configuration
public class ActivitiConfig {

    private static final Logger logger = LoggerFactory.getLogger(ActivitiConfig.class);


    /**
     * 配置分为以下几步骤
     * 1. 创建ActivitiConfig
     * 2. 使用ActivitiConfig创建ProcessEngineFactoryBean
     * 3. 使用ProcessEngineFactoryBean创建ProcessEngine对象
     * 4. 使用ProcessEngine对象创建需要的服务对象
     */
    private final DataSource dataSource;

    private final PlatformTransactionManager platformTransactionManager;

    @Autowired
    public ActivitiConfig(DataSource dataSource, PlatformTransactionManager transactionManager) {
        this.dataSource = dataSource;
        platformTransactionManager = transactionManager;
    }

    /*
     * 1. 创建配置文件,也就是提供一些配置信息,这样就可以自定义自己的创建信息了
     * 需要一些参数,1. 数据源。2. 事务管理器。
     * 这里还加入了自动扫描process包下的bpmn(流程定义文件)的设置,这样就可以省去了部署
     * */
    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
        SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
        spec.setDataSource(dataSource);
        spec.setTransactionManager(platformTransactionManager);
        spec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        Resource[] resources = null;
        // 启动自动部署流程
        try {
            resources = new PathMatchingResourcePatternResolver().getResources("classpath*:processes/*.*.xml");
        } catch (IOException e) {
            logger.error("Error Occur:", e);
        }
        spec.setDeploymentResources(resources);
        return spec;
    }

    @Bean
    public ProcessEngineFactoryBean processEngine() {
        ProcessEngineFactoryBean engineFactoryBean = new ProcessEngineFactoryBean();
        engineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration());
        return engineFactoryBean;
    }

    @Bean
    public RepositoryService repositoryService() throws Exception {
        return Objects.requireNonNull(processEngine().getObject()).getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService() throws Exception {
        return Objects.requireNonNull(processEngine().getObject()).getRuntimeService();
    }

    @Bean
    public TaskService taskService() throws Exception {
        return Objects.requireNonNull(processEngine().getObject()).getTaskService();
    }

    @Bean
    public HistoryService historyService() throws Exception {
        return Objects.requireNonNull(processEngine().getObject()).getHistoryService();
    }
}

resources中创建process文件夹,文件夹的路径和名字需要和ActivitiConfig中的配置保持一致启动springBoot项目即可创建完成

使用 Activiti

Idea 安装 Activiti BPMN visualizer 插件

编写测试 bpmn.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="test" name="test" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <endEvent id="endevent1" name="End"></endEvent>
        <userTask id="usertask1" name="HelloWorld" activiti:assignee="goxcheer"></userTask>
        <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_test">
        <bpmndi:BPMNPlane bpmnElement="test" id="BPMNPlane_test">
            <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
                <omgdc:Bounds height="35.0" width="41.0" x="220.0" y="180.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="640.0" y="180.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
                <omgdc:Bounds height="55.0" width="105.0" x="390.0" y="170.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
                <omgdi:waypoint x="261.0" y="197.0"></omgdi:waypoint>
                <omgdi:waypoint x="390.0" y="197.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
                <omgdi:waypoint x="495.0" y="197.0"></omgdi:waypoint>
                <omgdi:waypoint x="640.0" y="197.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>

编写测试代码

测试代码

@RequestMapping("/test")
@RestController
public class ActivitiTestController {

    private static final Logger logger = LoggerFactory.getLogger(ActivitiTestController.class);

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @RequestMapping("/test1")
    public void test1() {
        logger.info("Start.........");
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("test");
        logger.info("流程启动成功,流程id:{}", pi.getId());
    }


    @RequestMapping("/test2")
    public void test2() {
        String userId = "root";
        List<Task> resultTask = taskService.createTaskQuery().processDefinitionKey("test").taskCandidateOrAssigned(userId).list();
        logger.info("任务列表:{}", resultTask);
    }

}

…简单配置到此结束

完整配置代码见 :https://gitee.com/Marlon_Brando/onlineshop_back/tree/develop/os_workflow

到此这篇关于SpringBoot整合Activiti工作流框架的使用的文章就介绍到这了,更多相关SpringBoot Activiti工作流内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • java获取文件编码,jsoup获取html纯文本操作

    java获取文件编码,jsoup获取html纯文本操作

    这篇文章主要介绍了java获取文件编码,jsoup获取html纯文本操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • 基于Arrays.sort()和lambda表达式

    基于Arrays.sort()和lambda表达式

    这篇文章主要介绍了Arrays.sort()和lambda表达式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 详解Maven settings.xml配置(指定本地仓库、阿里云镜像设置)

    详解Maven settings.xml配置(指定本地仓库、阿里云镜像设置)

    这篇文章主要介绍了详解Maven settings.xml配置(指定本地仓库、阿里云镜像设置),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • SpringBoot集成消息队列的项目实践

    SpringBoot集成消息队列的项目实践

    本文主要介绍了SpringBoot集成消息队列的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • springboot多文件上传实现使用postman测试多文件上传接口

    springboot多文件上传实现使用postman测试多文件上传接口

    这篇文章主要介绍了springboot多文件上传实现使用postman测试多文件上传接口,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java中数据转换及字符串的“+”操作方法

    Java中数据转换及字符串的“+”操作方法

    本文主要介绍了Java中的数据类型转换,包括隐式转换和强制转换,隐式转换通常用于将范围较小的数据类型转换为范围较大的数据类型,而强制转换则是将范围较大的数据类型转换为范围较小的数据类型,本文介绍Java中数据转换以及字符串的“+”操作,感兴趣的朋友一起看看吧
    2024-10-10
  • 零基础写Java知乎爬虫之将抓取的内容存储到本地

    零基础写Java知乎爬虫之将抓取的内容存储到本地

    上一回我们说到了如何把知乎的某些内容爬取出来,那么这一回我们就说说怎么把这些内容存储到本地吧。
    2014-11-11
  • Eclipse安装Free marker插件教程

    Eclipse安装Free marker插件教程

    这篇文章主要为大家详细介绍了Eclipse安装Free marker插件教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • 浅谈Java多线程实现及同步互斥通讯

    浅谈Java多线程实现及同步互斥通讯

    下面小编就为大家带来一篇浅谈Java多线程实现及同步互斥通讯。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Java详解AVL树的应用

    Java详解AVL树的应用

    AVL树是高度平衡的二叉树,它的特点是AVL树中任何节点的两个子树的高度最大差别为1,本文主要给大家介绍了Java如何实现AVL树,需要的朋友可以参考下
    2022-07-07

最新评论