springboot 整合 activiti流程引擎 入门demo

activiti流程引擎开发指南

安装插件

参考链接:https://blog.csdn.net/x15011238662/article/details/86488754

解决乱码问题

1.idea安装目录bin目录下如下图所示两个文件 :

idea64.exe.vmoptions/idea.exe.vmoptions 添加 -Dfile.encoding=UTF-8

2.修改如下目录文件

C:\Users\Administrator\.IntelliJIdea2019.2\config\idea64.exe.vmoptions 添加 -Dfile.encoding=UTF-8

新建bpmn文件

元素列表

springboot 整合 activiti流程引擎 入门demo_第1张图片

 

我们主要用到的元素为:开始节点、结束节点、UserTask、ExclusiveGateway

画图规则

  1. 必须包含起始节点、结束节点,至少有一个UserTask节点,至少有一条完整的流程线路,额外的孤立节点,不会解析。
  2. 互斥网关上一个节点必须是UserTask类型

springboot 整合 activiti流程引擎 入门demo_第2张图片

 

集成actitivi

引入POM依赖

<dependency>
    <groupId>org.activitigroupId>    <artifactId>activiti-spring-boot-starter-basicartifactId>
    <version>6.0.0version>
dependency>

启动文件排除

@SpringBootApplication(exclude = org.activiti.spring.boot.SecurityAutoConfiguration.class)
public class RcApplication {
    public static void main(String[] args) {
        SpringApplication.run(RcApplication.class, args);
    }
}

添加配置类

package cn.cloudedu.rc.base.config;
import org.activiti.engine.*;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;

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

/**
 * activiti 配置类
 * 初始化activiti,注入相关的bean供后续使用
 * @author liubiao
 * @date 2019.11.1
 */
@Configuration
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {
    private final DataSource dataSource;
    private final PlatformTransactionManager platformTransactionManager;

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

    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
        SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
        spec.setDataSource(dataSource);
        spec.setTransactionManager(platformTransactionManager);
        spec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);//DB_SCHEMA_UPDATE_FALSE
        Resource[] resources = null;
        // 启动自动部署流程
        try {
            resources = new PathMatchingResourcePatternResolver().getResources("classpath*:process/*.bpmn");
        } catch (IOException e) {
            e.printStackTrace();
        }
        spec.setDeploymentResources(resources);
        return spec;
    }
    @Bean
    public ProcessEngineFactoryBean processEngineFactory() {
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration());
        return processEngineFactoryBean;
    }
    @Bean
    public ProcessEngine processEngine() throws Exception {
        return processEngineFactory().getObject();
    }

    @Bean
    public RepositoryService repositoryService() throws Exception {
        return processEngineFactory().getObject().getRepositoryService();
    }
    @Bean
    public RuntimeService runtimeService() throws Exception {
        return processEngineFactory().getObject().getRuntimeService();
    }
    @Bean
    public TaskService taskService() throws Exception {
        return processEngineFactory().getObject().getTaskService();
    }
    @Bean
    public HistoryService historyService() throws Exception {
        return processEngineFactory().getObject().getHistoryService();
    }
}

 

执行过程说明

 springboot 整合 activiti流程引擎 入门demo_第3张图片

流程部署

Deployment deployment = repositoryService.createDeployment().addInputStream(fileName,file.getInputStream()).deploy();

String deploymentId = deployment.getId();

//根据部署ID获取流程定义实例
ProcessDefinition processDefinition = processEngine.getRepositoryService()
        .createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();

String definitionId = processDefinition.getId();

 

流程启动

//根据部署流程ID启动流程
ProcessInstance processInstance = runtimeService.startProcessInstanceById(definitionId);

String processInstanceId = processInstance.getProcessInstanceId();

 

流程流转

Task task = taskService.createTaskQuery().processInstanceId(procInstanceId).singleResult();
Map condition = new HashMap<>();
condition.put("result",params.get("condition"));

taskService.complete(task.getId(),condition);

查看流程状态

//获取当前节点

Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();

BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(task.getProcessDefinitionId());
FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(activityId);

flowNode.getName();

flowNode.getId();

 

//获取该流程组件的之后/之前的组件信息
List sequenceFlowListOutGoing = flowNode.getOutgoingFlows();

//sequenceFlowListOutGoing数量可能大于1,可以自己做判断,此处只取第一个
FlowElement flowElement = sequenceFlowListOutGoing.get(0).getTargetFlowElement();

if(flowElement instanceof ExclusiveGateway){
    oprList.clear();
    ExclusiveGateway gateway = (ExclusiveGateway) flowElement;
    List list = gateway.getOutgoingFlows();
    for(SequenceFlow flow:list){
        flow.getName();
        String condition = flow.getConditionExpression();
    }
}

 

你可能感兴趣的:(三方插件,activiti流程引擎)