C/C++教程

activity6.0部署BPMN文件的多种方式,直接上代码

本文主要是介绍activity6.0部署BPMN文件的多种方式,直接上代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package test;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.zip.ZipInputStream;

/**
 * @author ***
 * @Description: 流程BPMN部署
 * @date 2021/04/04
 * 在使用的时候,注意activity的数据库配置是否正确,《activiti.cfg.xml》
 */
public class ProcessDefinitionTest {


    /**流程引擎(核心对象),默认加载类路径下命名为activiti.cfg.xml*/
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    
    //部署流程定义
    @Test
    public void deployementProcessDefinition(){
        Deployment deployment = processEngine.getRepositoryService()//获取流程定义和部署对象相关的Service
                        .createDeployment()//创建部署对象
                        .name("审计方案管理&审计通知书")//声明流程的名称
                        .addClasspathResource("process/executeSolutionApproval.bpmn")//加载资源文件,一次只能加载一个文件
                        .addClasspathResource("process/executeSolutionApproval.png")//
                        .deploy();//完成部署
        System.out.println("部署ID:"+deployment.getId());//1
        System.out.println("部署时间:"+deployment.getDeploymentTime());
    }

    @Test
    public void deployementProcessDefinitionByInputStream() throws FileNotFoundException {
        //获取资源相对路径
        String bpmnPath = "process/executeSolutionApproval.bpmn";
        String bpmnName = "executeSolutionApproval.bpmn";
        String pngPath = "process/executeSolutionApproval.png";
        String pngName = "executeSolutionApproval.png";

        //读取资源作为一个输入流
        FileInputStream bpmnfileInputStream = new FileInputStream(bpmnPath);
        FileInputStream pngfileInputStream = new FileInputStream(pngPath);
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        Deployment deployment = processEngine.getRepositoryService()//获取流程定义和部署对象相关的Service
                .createDeployment()//创建部署对象
                .addInputStream(bpmnName,bpmnfileInputStream)
                .addInputStream(pngName, pngfileInputStream)
                .deploy();//完成部署
        System.out.println("部署ID:"+deployment.getId());//1
        System.out.println("部署时间:"+deployment.getDeploymentTime());
    }


    /**
     * 字符串方式部署
     * @throws FileNotFoundException
     */
    @Test
    public void deployementProcessDefinitionByString() throws FileNotFoundException{
        //字符串
        String text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><definitions>...</definitions>";

        Deployment deployment = processEngine.getRepositoryService()//获取流程定义和部署对象相关的Service
                .createDeployment()//创建部署对象
                .addString("helloworld.bpmn",text)
                .deploy();//完成部署
        System.out.println("部署ID:"+deployment.getId());//1
        System.out.println("部署时间:"+deployment.getDeploymentTime());
    }

    /**
     * 压缩包方式部署
     * zip/bar
     */
    @Test
    public void deployementProcessDefinitionByzip(){
        //从classpath路径下读取资源文件
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/helloworld.zip");
        ZipInputStream zipInputStream = new ZipInputStream(in);
        Deployment deployment = processEngine.getRepositoryService()//获取流程定义和部署对象相关的Service
                .createDeployment()//创建部署对象
                .addZipInputStream(zipInputStream)//使用zip方式部署,将helloworld.bpmn和helloworld.png压缩成zip格式的文件
                .deploy();//完成部署
        System.out.println("部署ID:"+deployment.getId());//1
        System.out.println("部署时间:"+deployment.getDeploymentTime());
    }


}

多个方式任选,另外,在部署完毕之后,!!!记得准备脚本文件避免线上翻车!!!

这篇关于activity6.0部署BPMN文件的多种方式,直接上代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!