springboot2.2.2.RELEASE 集成Activiti最佳实践

1.新建springboot项目,项目结构如下,注意在resources文件夹下新建一个processes文件夹,到时候需要放置流程文件在里边

springboot2.2.2.RELEASE 集成Activiti最佳实践_第1张图片

1.先在pom中引入相关依赖

 
        
            org.activiti
            activiti-spring-boot-starter-basic
            6.0.0
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

2.修改pom中的字符编码为utf-8


        UTF-8
        UTF-8
        
        UTF-8
        1.8

3.创建mysql数据库,修改application.yml文件如下,注意配置流程文件的位置,写错就会报错,

spring:
  activiti:
    check-process-definitions: true #自动检查、部署流程定义文件
    database-schema-update: true #自动更新数据库结构
    #流程定义文件存放目录
    process-definition-location-prefix: classpath:/processes/
    #process-definition-location-suffixes: #流程文件格式
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    jdbc-url: jdbc:mysql://127.0.0.1:3306/boot_activiti?characterEncoding=UTF8&serverTimezone=UTC
    username: root
    password: root
  hikari:
    minimum-idle: 5
    maximum-pool-size: 15
    auto-commit: true
    idle-timeout: 30000
    pool-name: DatebookHikariCP
    max-lifetime: 1800000
    connection-timeout: 30000
    connection-test-query: SELECT 1
server:
  port: 8089

5.创建ActivitiConfig.java类

package cn.jorian.activiti.iactiviti;

import org.activiti.spring.SpringAsyncExecutor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @author: jorian
 * @date: 2019/12/9 11:41
 * @description: this is  description for the class
 */
@Configuration//声名为配置类,继承Activiti抽象配置类
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource activitiDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(
            PlatformTransactionManager transactionManager,
            SpringAsyncExecutor springAsyncExecutor) throws IOException {

        return baseSpringProcessEngineConfiguration(
                activitiDataSource(),
                transactionManager,
                springAsyncExecutor);
    }

}

6.启动类,注意上边的注释:@SpringBootApplication(exclude =  SecurityAutoConfiguration.class),没有这行会报错

package cn.jorian.activiti.iactiviti;

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

@SpringBootApplication(exclude =  SecurityAutoConfiguration.class)
@ComponentScan(value = {"cn.jorian", "cn.jorian.activiti" })
public class IactivitiApplication {
    public static void main(String[] args) {
        SpringApplication.run(IactivitiApplication.class, args);
    }

}

7.创建单元测试类:activitiTest.java

package cn.jorian.activiti.iactiviti;

import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * @author: jorian
 * @date: 2019/12/10 09:04
 * @description: this is  description for the class
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = IactivitiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ActivitiTest {
        @Autowired
        RuntimeService runtimeService;
        @Autowired
        private TaskService taskService;

        @Test
        public void TestStartProcess() {
            // 创建流程引擎
            ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        // 获取流程存储服务组件
            RepositoryService repositoryService = processEngine.getRepositoryService();

        // 获取运行时服务组件
            RuntimeService runtimeService = processEngine.getRuntimeService();

        // 获取流程任务组件
            TaskService taskService = processEngine.getTaskService();

        // 1、部署流程文件
            repositoryService.createDeployment().name("myProcess01")
                    .addClasspathResource("processes/MyProcesses.bpmn").deploy();

        // 2、启动1个流程,每个用户都可以拉起一个流程,每个流程有单独的id
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess01");
        //runtimeService.startProcessInstanceById("myProcess");
            System.out.println(processInstance.getId());

        // 3、查询任务
            //使用singleResult()方法时,查询结果只能有一条记录,当返回大于1条的时候就会报上面的错误,将singleResult()改成list()即可。
            List tasks = taskService.createTaskQuery().list();
            tasks.stream().forEach(task->{
                System.out.println("====任务执行中====" + task.getId() + "===="+task.getName()+"======");
                taskService.complete(task.getId()); // 完成任务
            });

       /* // 4、查询第二个任务
            task = taskService.createTaskQuery().singleResult();
            System.out.println("============" + task.getId() + "============");
            taskService.complete(task.getId()); // 完成任务

        // 5、流程结束
            task = taskService.createTaskQuery().singleResult();
            System.out.println("============ 任务结束(" + task + ") ============");*/

       //删除modelid
        }
        @Test
        public void findTasksByUserId() {
            String userId ="dulingjiang";
            List resultTask = taskService.createTaskQuery().processDefinitionKey("myProcess01").taskCandidateOrAssigned(userId).list();
            System.out.println("任务列表:"+resultTask);
        }

    }

9.IDEA安装activiti流程图插件:actiBPM

springboot2.2.2.RELEASE 集成Activiti最佳实践_第2张图片

10.绘制简单的流程图:MyProcesses.bpmn

springboot2.2.2.RELEASE 集成Activiti最佳实践_第3张图片

11.启动项目,生成28张表,启动test测试类

springboot2.2.2.RELEASE 集成Activiti最佳实践_第4张图片

 

你可能感兴趣的:(web框架,activiti,工作流)