本章节记录整合过程和部分问题,目前整合并不完美后续会继续更新…

文档链接:
5.2.1 activiti用户手册
activiti用户手册
activiti6 API

技术栈: springboot 2.1.3 activiti6 tk.mybatis 本文仅记录activiti相关部分

1. pom.xml

<properties>
    <mybatis-spring-boot>1.3.2</mybatis-spring-boot>
    <springboot>2.1.3.RELEASE</springboot>
    <tkmybatis>2.1.5</tkmybatis>
    <activiti.version>6.0.0</activiti.version>
</properties>
<dependencies>
    <!-- Spring Boot Mybatis 依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis-spring-boot}</version>
    </dependency>
    <!-- Spring Boot TK.Mybtis-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>${tkmybatis}</version>
        <!-- activiti默认使用JPA, tk的实现会出现问题,具体原因自行了解 -->
        <exclusions>
            <exclusion>
                <artifactId>persistence-api</artifactId>
                <groupId>javax.persistence</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
    </dependency>
    <!-- activiti -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring-boot-starter-basic</artifactId>
        <version>${activiti.version}</version>
    </dependency>
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring-boot-starter-jpa</artifactId>
        <version>${activiti.version}</version>
    </dependency>
<dependencies>

2. application.yml

spring:
    activiti:
        # true会校验部署流程的 .bpmn 文件,一般activiti支持两种扩展名的部署: .bpmn .bpmn20.xml
        # 可以选择springboot自动配置 processEngine, 我没有选择这种方式
        check-process-definitions: false

3. java configuration

import org.activiti.engine.*;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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;

@Configuration
public class ActivitiConfig {

    @Bean
    @Primary
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(DataSource dataSource,
        DataSourceTransactionManager transactionManager) throws IOException {

        // 自动部署流程的读取位置
        Resource[] resources = new PathMatchingResourcePatternResolver()
            .getResources(ResourceLoader.CLASSPATH_URL_PREFIX + "processes/*.bpmn");
        SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration();
        // 配置自己的数据源和事务管理器
        config.setDataSource(dataSource);
        config.setTransactionManager(transactionManager);
        // jdbc最大等待时间20秒,超出后重新连接
        config.setJdbcMaxWaitTime(2000);
        /**
         * 数据库更新模式:
         * ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE  如果数据库里面没有activit的表,也不会创建
         * ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP 创建表,使用完之后删除
         * ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE  如果数据库里面没有表,就创建
         * "dorp-create" 如果数据库里面有表,那么先删除再创建
         * nullCatalogMeansCurrent=true 数据库连接后面记得加这个,否则自动建表可能会失败
         */
        config.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE);
        config.setDeploymentResources(resources);
        // 检测身份信息表
        config.setDbIdentityUsed(false);
        // 防止图片乱码
        config.setActivityFontName("宋体");
        config.setAnnotationFontName("宋体");
        config.setLabelFontName("宋体");
        return config;
    }

    @Bean
    @Primary
    public ProcessEngineFactoryBean processEngineFactoryBean(
        SpringProcessEngineConfiguration springProcessEngineConfiguration) throws ClassNotFoundException {

        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration);
        return processEngineFactoryBean;
    }

    /** 我在创建引擎的时候一直报错:JpaProcessEngineAutoConfiguration 重复定义了相同名字的bean让我开启spring重写, 我就把自启动排除掉了。 */
    @Bean("processEngine")
    @Primary
    public ProcessEngine processEngine(ProcessEngineFactoryBean processEngineFactoryBean) throws Exception {
        return processEngineFactoryBean.getObject();
    }

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

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

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

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

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

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

    @Bean
    @Primary
    public FormService formService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getFormService();
    }
}

4. applicationBoot.java


import org.activiti.spring.boot.JpaProcessEngineAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/** org.activiti.spring.boot.SecurityAutoConfiguration.class activiti6中的配置是有问题的,后续修改一下源码解决一下。先排除掉
 *  org.activiti.spring.boot.JpaProcessEngineAutoConfiguration 这个东西就好烦人,自动使用JPA,而且会重写自定义的processEngine,所以排除掉它。其实可能只有我这有问题,有大神好心帮我解个惑嘛...
 */
@SpringBootApplication(exclude = {org.activiti.spring.boot.SecurityAutoConfiguration.class,
    JpaProcessEngineAutoConfiguration.class})
@EnableTransactionManagement
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

配置到这基本就结束了,下面可以简单测试一下是否成功

import com.zwzgame.mwerp.model.floworder.MwClaimProjectOrder;
import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivitiTest {

    private ProcessEngine processEngine = null;

    @Before
    public void executeBefor() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://192.168.*.*:3306/mwerp?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&userSSL=true&serverTimezone=Hongkong&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=true&nullCatalogMeansCurrent=true");
        dataSource.setUsername("root");
        dataSource.setPassword("111111");
        processEngine = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration()
            .setDataSource(dataSource)
            .setJdbcMaxWaitTime(2000)
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
            .buildProcessEngine();
    }

    @Test
    public void activitiTest() {
        RepositoryService rs = processEngine.getRepositoryService();
        List<ProcessDefinition> list = rs.createProcessDefinitionQuery().list();
        list.stream().forEach(System.out::println);
    }

    @Test
    public void startFlowTest() {
        RuntimeService runtimeService = processEngine.getRuntimeService();
        String k = MwClaimProjectOrder.class.getSimpleName();
        Map<String, Object> var = new HashMap<>();
        var.put("username", "testpm");
        ProcessInstance pi = runtimeService.startProcessInstanceByKey(k, k + ":123", var);
        System.out.println("businessKey:" + pi.getBusinessKey());
        System.out.println("piId:" + pi.getId());
        System.out.println("pdName:" + pi.getProcessDefinitionName());
    }

    @Test
    public void listTaskTest() {
        String u = "testpm";
        List<Task> list = processEngine.getTaskService().createTaskQuery().taskAssignee(u).list();
        for (Task t : list) {
            System.out.println(t.getId());
            System.out.println(t.getAssignee());
            System.out.println(t.getName());
            System.out.println(t.getOwner());
            System.out.println(t.getTaskDefinitionKey());
        }
    }

    @Test
    public void nextFlowTest() {
        TaskService taskService = processEngine.getTaskService();
        Map<String, Object> var = new HashMap<>();
        var.put("approved", "agreed");
        taskService.complete("15006", var);
        System.out.println("执行成功");
    }

    @Test
    public void deleteFlowTest() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        repositoryService.deleteDeployment("7501");
        System.out.println("删除成功");
    }

}

版权声明:本文为knowledgemine原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/knowledgemine/p/13100446.html