農農  

1. 什么是流程实例

  参与者(可以是用户也可以是程序)按照流程定义内容发起一个流程,这就是一个流程实例,是态的。

2.  流程定义和流程实例的图解

  

 

 

3.使用压缩包 部署流程

  编写测试代码 

 //以压缩包方式进行部署流程
    @Test
    public void deploymentByZip(){
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取一个RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //定义一个输入流,加载Zip文件
        InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("bpmnfile/holiday.zip");
        //定义一个ZIPInputStream对象
        ZipInputStream zipInputStream=new ZipInputStream(inputStream);
        //流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("请求审批流程")
                .key("holidayKey")
                .deploy();
        System.out.println("流程名称:"+deploy.getName());
    }

  查看数据库

 

 

 

4. 查看流程定义

  编写测试代码

 //流程定义的查看
    @Test
    public void getProDef(){
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取一个RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //得到流程定义查看对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //指定查看的流程   processDefinitionKey()指定流程定义的key值   orderByProcessDefinitionVersion根据流程定义版本号排序
        List<ProcessDefinition> holiday = processDefinitionQuery.processDefinitionKey("holiday").orderByProcessDefinitionVersion().desc().list();
        for (ProcessDefinition proDef:holiday) {
            System.out.println("流程定义ID:"+proDef.getId());
            System.out.println("流程定义的名称:"+proDef.getName());
            System.out.println("流程定义的Key:"+proDef.getKey());
            System.out.println("流程定义的版本号:"+proDef.getVersion());
        }
    }

  控制台结果

 

 

 5. 删除流程

  编写测试代码

   //删除流程
    @Test
    public  void deletDeployment(){
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取一个RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //删除流程
        repositoryService.deleteDeployment("5002",true);
    }

6. 从数据中读取资源文件到本地

  编写测试代码 

 //从数据中读取资源文件到本地
    @Test
    public void getResource() throws IOException {
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取一个RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //获取到流程查询对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //获取到流程资源
        processDefinitionQuery.processDefinitionKey("holiday");
        //获取单一结果
        ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
        //流程部署的ID
        String deploymentId = processDefinition.getDeploymentId();
        //获取到bpmnResource
        InputStream bpmnStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
        //获取到png
        InputStream pngStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
        //构建输出流
        OutputStream bpmnOut = new FileOutputStream("F:\\" + processDefinition.getResourceName());
        OutputStream pngOut = new FileOutputStream("F:\\" + processDefinition.getDiagramResourceName());
        //将数据输出到磁盘当中
        IOUtils.copy(bpmnStream,bpmnOut);
        IOUtils.copy(pngStream,pngOut);
        //关流
        pngOut.close();
        bpmnOut.close();
        pngStream.close();
        bpmnStream.close();
    }

  查看文件

 

7.  业务系统和流程系统进行关联

  编写测试代码

 //业务系统和流程系统进行关联
    @Test
    public void startInstance(){
        /**
         * 创建张三请假的实例流程
         */
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //生成流程
        ProcessInstance holiday = runtimeService.startProcessInstanceByKey("holiday","2");

        /**
         * 张三提交请假审批
         */
        //获取一个TaskService对象
        TaskService taskService = processEngine.getTaskService();
        //获取张三的任务
        Task task = taskService.createTaskQuery().taskAssignee("zhangsan").processDefinitionKey("holiday").singleResult();
        //任务审批
        taskService.complete(task.getId());
    }

  查看数据库

8. 流程激活和挂起

  编写测试代码

 /**
     * 所有流程定义的挂起
     */
    @Test
    public void allSuspend(){
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取repositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //获取流程定义
        ProcessDefinition holiday = repositoryService.createProcessDefinitionQuery().processDefinitionKey("holiday").singleResult();
        //获取到当前流程定义是否为暂停状态   suspended方法为true代表为暂停   false就是运行的
        boolean suspended = holiday.isSuspended();
        if(suspended){
            repositoryService.activateProcessDefinitionById(holiday.getId(),true,null);
            System.out.println("该流程定义激活");
        }else{
            repositoryService.suspendProcessDefinitionById(holiday.getId(),true,null);
            System.out.println("该流程定义暂停");
        }
    }

  控制台效果

 

 

 

 9. 单个流程实例的挂起

  编写测试代码 

 /**
     * 单个流程实例的挂起
     */
    @Test
    public void singleSuspend(){
        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RuntimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId("2501").singleResult();
        //获取到当前流程定义是否为暂停状态   suspended方法为true代表为暂停   false就是运行的
        boolean suspended = processInstance.isSuspended();
        if(suspended){
            runtimeService.activateProcessInstanceById("2501");
            System.out.println("xx的请假流程激活");
        }else{
            runtimeService.suspendProcessInstanceById("2501");
            System.out.println("xx的请假流程挂起");
        }
    }

  控制台效果

 

 

 

posted on 2020-04-02 17:27  Baekhyunne  阅读(1600)  评论(0编辑  收藏  举报