Activiti6.0 + Spring boot 2.0.1 集成过程

序言

工作流框架是每一个比较复杂的OA或者ERP系统都会用到的一套自动化工作流程的组件,主要针对于复杂的流程流转任务系统提供代码与任务解耦的解决方案!
目前工作主要用到Activiti6.0,开发框架为Spring Boot 2.0.1 Release脚手架,在集成工程中踩了一些坑,特来记录完成整合流程,希望能够帮助同行道友!

整合流程如下

  • POM Jar 版本
    项目框架使用的Maven 管理 ,pom文件如下:

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



    UTF-8
    UTF-8
    1.8
    6.0.0



    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        org.activiti
        activiti-spring-boot-starter-basic
        ${activiti.version}
    

    
        com.baomidou
        mybatis-plus
        2.3
    


  • 建立"processes"目录
    在项目 “src\main\resources\” 目录下面建立“processes” 资源目录,
    把流程定义的文件放在此目录下面,程序启动使用会自动加载目录下面定义的流程文件自动部署到数据库里面!
    Activiti6.0 + Spring boot 2.0.1 集成过程_第1张图片
  • 在启动类上排除SecurityAutoConfiguration类
    如果不排除文件,项目启动会抛出异常
java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class})
@EnableTransactionManagement
@EnableCaching
@EnableSwagger2
public class OpApplication {

	public static void main(String[] args) {
		SpringApplication.run(OpApplication.class, args);
	}

}

  • 编辑Activiti集成配置Bean
    本文直接使用Bean类做出配置也可以使用xml文件配置。
package com.jfh.op.config;

import org.activiti.engine.*;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

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

/**
 * @author guohz
 * @email [email protected]
 * @description activiti 集成配置
 * @date 2018/11/12
 */
@Configuration
public class ActivitiConfig {

    @Bean
    public ProcessEngine processEngine(DataSourceTransactionManager transactionManager, DataSource dataSource) throws IOException {
        SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        //自动部署已有的流程文件
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(ResourceLoader.CLASSPATH_URL_PREFIX + "processes/*.bpmn");
        configuration.setTransactionManager(transactionManager);
        configuration.setDataSource(dataSource);
        configuration.setDatabaseSchemaUpdate("true");
        configuration.setDeploymentResources(resources);
        configuration.setDbIdentityUsed(false);
        return configuration.buildProcessEngine();
    }

    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine) {
        return processEngine.getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine) {
        return processEngine.getRuntimeService();
    }

    @Bean
    public TaskService taskService(ProcessEngine processEngine) {
        return processEngine.getTaskService();
    }

    @Bean
    public HistoryService historyService(ProcessEngine processEngine) {
        return processEngine.getHistoryService();
    }

    @Bean
    public ManagementService managementService(ProcessEngine processEngine) {
        return processEngine.getManagementService();
    }

    @Bean
    public IdentityService identityService(ProcessEngine processEngine) {
        return processEngine.getIdentityService();
    }

}

  • 编写测试案例
    本文编写了一个最简单的流程定义文件,一个UserTask和一个ServiceTask 截图如下:
    Activiti6.0 + Spring boot 2.0.1 集成过程_第2张图片

测试类编写如下:

package com.jfh.op;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author guohz
 * @email [email protected]
 * @description AcitiviTest 流程整合测试
 * @date 2018/11/12
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=OpApplication.class)// 指定spring-boot的启动类
public class ActivitiTest {


    Logger logger = LoggerFactory.getLogger(ActivitiTest.class);

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private TaskService taskService;

    @Test
    public void testAppProcess(){
        //开启流程
        System.out.println("执行成功!");

        //查找流程定义
        ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("test-app").singleResult();

        // 启动流程
        ProcessInstance pi = this.runtimeService.startProcessInstanceByKey(pd
                .getKey());

        // 查询第一个任务
        Task task = this.taskService.createTaskQuery()
                .processInstanceId(pi.getId()).singleResult();

        logger.info("task name: {}",task.getName());

        //执行任务
        taskService.complete(task.getId());


        task = this.taskService.createTaskQuery()
                .processInstanceId(pi.getId()).singleResult();

//        logger.info("task name: {}",task.getName());

    }

}

运行结果截图:
Activiti6.0 + Spring boot 2.0.1 集成过程_第3张图片

你可能感兴趣的:(Activiti)