跳转至

1.部署流程

1.1 classpath 方式

    /**
     * 部署流程使用classpath
     */
    @Test
    public void deployProcess01() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        Deployment deploy = repositoryService
                // 创建
                .createDeployment()
                // 名称
                .name("请假流程001")
                // 路径
                .addClasspathResource("HelloWorld.bpm")
                // 路径
                .addClasspathResource("HelloWorld.png")
                // 部署
                .deploy();
        System.out.println("部署成功:流程部署ID = " + deploy.getId());
    }

1.2 inputStream 方式

    /**
     * 部署流程使用zip 流程图的文件必须是xxx.zip
     */
    @Test
    public void deployProcess02() {
        // 获取流文件
        InputStream inputStream = this.getClass().getResourceAsStream("/HelloWorld.zip");
        System.out.println(inputStream);
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        Deployment deploy = repositoryService
                // 创建
                .createDeployment()
                // 名称
                .name("请假流程001")
                // 添加流文件
                .addZipInputStream(new ZipInputStream(inputStream))
                // 部署
                .deploy();
        System.out.println("部署成功:流程部署ID = " + deploy.getId());
    }

1.3 查询流程部署

    /**
     * 查询流程部署信息
     */
    @Test
    public void queryProcessDeploy() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();

        // 创建部署信息查询
        long count = repositoryService.createDeploymentQuery()
                //// 根据id查询
                // .deploymentId("1")
                //// 根据部署名称查询
                .deploymentName("请假流程001")
                // .deploymentNameLike("请假")
                //// 根据tenantId查询
                // .deploymentTenantId("")
                // .deploymentTenantIdLike("")
                //// 排序
                //// id升序
                // .orderByDeploymentId().asc()
                //// 时间降序
                // .orderByDeploymenTime().desc()
                //// 名称升序
                // .orderByDeploymentName().asc()
                //// 结果集
                //// list集合
                // .list();
                //// 分页
                // .listPage(firstResult, maxResults)
                //// 返回单个
                // .singleResult()
                //// 统计
                .count();
        System.out.println(count);
    }

3.流程定义

3.1查询

    /**
     * 查询流程定义
     */
    public void queryProcDef() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        repositoryService.createProcessDefinitionQuery()
        //条件
        //部署ID
//      .deploymentId(deploymentId)
//      .deploymentIds(deploymentIds)
        //流程定义ID
//      .processDefinitionId(processDefinitionId)
//      .processDefinitionIds(processDefinitionIds)
        //流程定义的key
//      .processDefinitionKey(processDefinitionKey)
//      .processDefinitionKeyLike(processDefinitionKeyLike)
        //流程定义的名称
//      .processDefinitionName(processDefinitionName)
//      .processDefinitionNameLike(processDefinitionNameLike)
        //流程图的bpmn文件名
//      .processDefinitionResourceName(resourceName)
//      .processDefinitionResourceNameLike(resourceNameLike)
        //流程定义的版本
//      .processDefinitionVersion(processDefinitionVersion)
//      .processDefinitionVersionGreaterThan(processDefinitionVersion)//大于
//      .processDefinitionVersionGreaterThanOrEquals(processDefinitionVersion)//大于等于
//      .processDefinitionVersionLowerThan(processDefinitionVersion)//小于
//      .processDefinitionVersionLowerThanOrEquals(processDefinitionVersion)//小于等于
        //排序
//      .orderByDeploymentId()
//      .orderByProcessDefinitionId()
//      .orderByProcessDefinitionKey()
//      .orderByProcessDefinitionName()
//      .orderByProcessDefinitionVersion()
        //结果集
//      .list()
//      .listPage(firstResult, maxResults)
//      .count()
//      .singleResult()
    }

3.2删除

    /**
     * 删除流程定义
     */
    @Test
    public void deletePoocessDef() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        String deploymentId ="2501";
        //根据流程部署id删除流程定义 如果当前流程正在执行 , 那么会报错
//      repositoryService.deleteDeployment(deploymentId);
        //根据流程部署id删除流程定义 true 时 , 删除有关的 act_ru_* 和 act_hi_* 的数据
//      repositoryService.deleteDeployment(deploymentId, true);
        //和上面的方法一致,但过时
//      repositoryService.deleteDeploymentCascade(deploymentId);
    }

3.3 修改

  • 修改流程图之后从新部署,只要key不变,它的版本号就会+1
  • 这样是为了保护之前流程定义所运行的数据

3.4 查询流程图

3.4.1 根据流程定义id

    /**
     * 查询流程图
     */
    @Test
    public void viewProcessImg() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        String processDefinitionId = "HelloWorld_ID:2:2504";
        //获取流程图流文件
        InputStream inputStream = repositoryService.getProcessDiagram(processDefinitionId);

        File file = new File("d:/helloworld.png");

        try {
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
            int len = 0;
            byte[] b = new byte[1024];
            // 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数
            while ((len = inputStream.read(b)) != -1) {
                // b:数据 off: 偏移量 len:写入的字节量
                outputStream.write(b, 0, len);
                outputStream.flush();
            }
            inputStream.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("查询成功");
    }

3.4.2根据流程部署id

    /**
     * 查询流程图 根据流程部署id
     */
    @Test
    public void viewProcessImg2() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        // 根据流程部署id查询流程定义对象
        String deploymentId = "1";
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId).singleResult();
        String id = processDefinition.getId();
        InputStream inputStream = repositoryService.getProcessDiagram(id);
        //获取文件名称,给定保存路径
        File file = new File("d:/" + processDefinition.getDiagramResourceName());

        try {
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
            int len = 0;
            byte[] b = new byte[1024];
            // 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数
            while ((len = inputStream.read(b)) != -1) {
                // b:数据 off: 偏移量 len:写入的字节量
                outputStream.write(b, 0, len);
                outputStream.flush();
            }
            inputStream.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

3.5查询最新版本的流程定义

    /**
     * 查询最新版本的流程定义
     */
    @Test
    public void queryNewProcessDef() {
        Map<String, ProcessDefinition> map = new HashMap<>();

        // 查询所有的流程定义根据版本号
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .orderByProcessDefinitionVersion().asc().list();

        if (null != list && 0 < list.size()) {
            for (ProcessDefinition pd : list) {
                map.put(pd.getKey(), pd);
            }
        }
        Collection<ProcessDefinition> values = map.values();
        for (ProcessDefinition pd : values) {
            System.out.println("流程定义ID:" + pd.getId());
            System.out.println("流程部署ID:" + pd.getDeploymentId());
            System.out.println("流程定义key:" + pd.getKey());
            System.out.println("流程定义名称:" + pd.getName());
        }
    }

3.6删除流程定义(删除key相同的所有不同的流程定义)

    /**
     * 删除流程定义(删除key相同的所有不同的流程定义)
     */
    @Test
    public void deleteAllProcessDefByKey() {
        // 查询所有的流程定义根据版本号
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        String processDefinitionKey = "HelloWorld_ID";
        // 查询流程定义 根据key
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(processDefinitionKey).list();
        if (list != null && list.size() > 0) {
            for (ProcessDefinition pd : list) {
                repositoryService.deleteDeployment(pd.getDeploymentId(), true);
            }
        }
    }