java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot Activiti工作流

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

作者:MarlonBrando1998

本文主要介绍了SpringBoot整合Activiti工作流框架的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

版本问题

使用 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工作流内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文