美文网首页
Activiti HelloWorld程序

Activiti HelloWorld程序

作者: __元昊__ | 来源:发表于2019-10-09 16:06 被阅读0次

07【掌握】HelloWorld程序(模拟流程的执行)

1.画流程图


image.png
image.png
image.png
image.png

指定节点办理人


image.png
image.png
image.png
image.png
2.部署流程
/**
     * 部署流程
     */
    @Test
    public void deploymentProcessDefinition() {
        //1,得到一个service可以操作act_ge_bytearray act_re_deployment act_re_procdef
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("HelloWorld.bpmn")
        .addClasspathResource("HelloWorld.png")
        .name("请假流程002")//设置流程部署名称 act_re_deployment里面的name属性值
        .deploy();//部署
        System.out.println("部署成功:流程部署ID:"+deploy.getId()+" 流程部署名称:"+deploy.getName());
    }

3.启动流程

/**
     * 启动流程
     */
    @Test
    public void startProcess() {
        //1,得到启动流程的service
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
//      String processDefinitionId="HelloWorld:1:4";
//      runtimeService.startProcessInstanceById(processDefinitionId);
        String processDefinitionKey="HelloWorld";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);//启动的永远是最新版本的流程
        System.out.println("流程启动成功");
        System.out.println("部署ID:"+processInstance.getDeploymentId());//指act_re_deployment的id
        System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId());//act_re_procdef里面的id
        System.out.println("流程实例ID:"+processInstance.getId());//act_ru_execution这个表里面的id
    }

4.任务查询

/**
     * 任务查询
     */
    @Test 
    public void queryTask() {
        TaskService taskService = this.processEngine.getTaskService();
        String assignee="王五";
        List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
        for (Task task : list) {
            System.out.println("任务ID:"+task.getId());
            System.out.println("执行实例ID:"+task.getExecutionId());
            System.out.println("流程实例ID:"+task.getProcessInstanceId());
            System.out.println("任务名称:"+task.getName());
            System.out.println("任务定义的Key:"+task.getTaskDefinitionKey());
            System.out.println("任务办理人:"+task.getAssignee());
            System.out.println("#####################");
        }
    }

5.完成任务

/**
     * 完成任务
     */
    @Test
    public void completeTask() {
        TaskService taskService = this.processEngine.getTaskService();
        String taskId="10002";
        taskService.complete(taskId);
        System.out.println("任务完成成功");
    }

08【掌握】管理流程定义

1.流程部署

private ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
    
    
    /**
     * 流程部署
     * 使用addClasspathResource
     */
    @Test
    public void deployProcessDefinition1() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("HelloWorld.bpmn")
        .addClasspathResource("HelloWorld.png")
        .name("请假流程001")
        .deploy();
        System.out.println("部署成功:流程部署ID:"+deploy.getId()+" 流程部署名称:"+deploy.getName());
    }
    /**
     * 流程部署
     * 使用addInputStream
     */
    @Test
    public void deployProcessDefinition2() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        /**this.getClass().getResourceAsStream()
         |--里面没有 / 代表相对于当前类所在包的路径
         |--里面有  / 代表相对的源码包的根路径
        **/
        InputStream bpmnInputStream=this.getClass().getResourceAsStream("HelloWorld.bpmn");
        InputStream pngInputStream=this.getClass().getResourceAsStream("HelloWorld.png");
        
        /**
         * this.getClass().getClassLoader().getResourceAsStream()
         *    |--里面没有 / 代表相对于当前源码包的路径
         *    |--没有/  相取于项目路径
         */
//      InputStream bpmnInputStream=this.getClass().getClassLoader().getResourceAsStream("HelloWorld.bpmn");
//      InputStream pngInputStream=this.getClass().getClassLoader().getResourceAsStream("HelloWorld.png");
        System.out.println(bpmnInputStream);
        Deployment deploy = repositoryService.createDeployment()
                .addInputStream("HelloWorld.bpmn", bpmnInputStream)
                .addInputStream("HelloWorld.png", pngInputStream)
                .name("请假流程002")
                .deploy();
        System.out.println("部署成功:流程部署ID:"+deploy.getId()+" 流程部署名称:"+deploy.getName());
    }
    
    /**
     * 流程部署
     * 使用addInputStream
     */
    @Test
    public void deployProcessDefinition3() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ZipInputStream zipInputStream=new ZipInputStream(this.getClass().getResourceAsStream("/HelloWorld.zip"));
        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("请假流程003")
                .deploy();
        System.out.println("部署成功:流程部署ID:"+deploy.getId()+" 流程部署名称:"+deploy.getName());
    }

2.查询流程部署

/**
     * 查询流程部署
     */
    @Test
    public void queryProcessDeploy() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //创建一个查询对象
        List<Deployment> list = repositoryService.createDeploymentQuery()
            //查询条件
//          .deploymentId(deploymentId)//根据部署ID查询流程部署信息
//          .deploymentKey(key)  根据部署的KEY查询流程部署信息
//          .deploymentKeyLike(keyLike)  //根据部署KEY模糊查询
//          .deploymentName(name)//根据部署名称等值查询
//          .deploymentNameLike(nameLike)//根据部署名称模糊查询
            //排序条件
//          .orderByDeploymentId().asc()//根据流程部署ID 升序
//          .orderByDeploymenTime().desc() //根据流程部署时间 降序
            //结果集
            .list()  //返回list集合
//          .singleResult()  返回单条数据对象
//          .count()  //返回查询总条
//          .listPage(firstResult, maxResults)//分查查询
            ;
        for (Deployment d : list) {
            System.out.println("部署ID:"+d.getId());
            System.out.println("部署名称:"+d.getName());
            System.out.println("部署KEY:"+d.getKey());
            System.out.println("部署CATEGORY:"+d.getCategory());
            System.out.println("部署TENANTID:"+d.getTenantId());
            System.out.println("部署时间:"+d.getDeploymentTime());
            System.out.println("######################");
        }
        
    }

3.查询流程定义

/**
     * 查询流程定义
     */
    @Test
    public void queryProcessDefinition() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
         List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
            //查询条件
//          .processDefinitionId(processDefinitionId)//根据流程定义ID等值查询
//          .processDefinitionIds(processDefinitionIds) processDefinitionIds就一个set集合,in (set)
//          .processDefinitionCategory(processDefinitionCategory)
//          .processDefinitionCategoryLike(processDefinitionCategoryLike)
//          .processDefinitionKey(processDefinitionKey)根据流程定义的key查询
//          .processDefinitionKeyLike(processDefinitionKeyLike)根据流程定义的key模糊查询
//          .processDefinitionName(processDefinitionName)根据流程定义的名称等值查询
//          .processDefinitionNameLike(processDefinitionNameLike)根据流程定义的名称模糊查询
//          .deploymentId("1") //根据流程部署ID查询
//          .deploymentIds(deploymentIds)根据流程部署ID的set集合查询
            //排序条件
//          .orderByProcessDefinitionVersion().asc()
            //结果集
//          .singleResult();
            .list();
        for (ProcessDefinition processDefinition : list) {
            System.out.println("流程定义ID:"+processDefinition.getId());
            System.out.println("流程定义名称:"+processDefinition.getName());
            System.out.println("流程定义KEY:"+processDefinition.getKey());
            System.out.println("流程部署ID:"+processDefinition.getDeploymentId());
            System.out.println("######################");
        }
        
    }

4.删除流程定义和部署

/**
     * 删除流程定义和部署
     */
    @Test
    public void deleteProcessDeployment() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        String deploymentId="7501";
//      repositoryService.deleteDeployment(deploymentId);//根据流程部署ID删除  如果流程正在使用则会抛异常
        repositoryService.deleteDeployment(deploymentId, true);//根据流程部署ID删除  如果流程再在使用也能级联删除
        System.out.println("删除成功");
    }

5.修改流程定义
重新部署让版本增加

6.查询资源文件[图片]

/**
     * 查询流程图
     * @throws FileNotFoundException 
     */
    @Test
    public void queryProcessDeploymentImage() throws Exception {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        String deploymentId="2501";
        List<String> names = repositoryService.getDeploymentResourceNames(deploymentId);
        for (String resourceName : names) {
            System.out.println(resourceName);
            if(resourceName.endsWith(".png")) {
                InputStream stream = repositoryService.getResourceAsStream(deploymentId, resourceName);
                File file=new File("D:/"+resourceName);
                FileOutputStream fos=new FileOutputStream(file);
                int len=0;
                byte[] b=new byte[1024];
                while((len=stream.read(b))!=-1) {
                    fos.write(b, 0, len);
                    fos.flush();
                }
                fos.close();
                stream.close();
            }
        }
        System.out.println("资源文件保存成功");
    }
    
    /**
     * 查询流程图
     * @throws已知流程定义id  act_re_procdef 的id
     * 
     */
    @Test
    public void queryProcessDeploymentImage2() throws Exception {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        String processDefinitionId="HelloWorld:2:2504";
        //1,根据流程定义ID查询流程定义的对象
        ProcessDefinition processDefinition=repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId)
                .singleResult();
        
        String deploymentId=processDefinition.getDeploymentId();
        String resourceName=processDefinition.getDiagramResourceName();
        InputStream stream = repositoryService.getResourceAsStream(deploymentId, resourceName);
        
        File file=new File("D:/"+resourceName);
        FileOutputStream fos=new FileOutputStream(file);
        int len=0;
        byte[] b=new byte[1024];
        while((len=stream.read(b))!=-1) {
            fos.write(b, 0, len);
            fos.flush();
        }
        fos.close();
        stream.close();
        System.out.println("资源文件保存成功");
    }

7.附加功能:查询最新版本的流程定义

/**
     * 7:附加功能:查询最新版本的流程定义
     */
    @Test
    public void queryTopNewProcessDefinition() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().latestVersion().list();
        for (ProcessDefinition processDefinition : list) {
            System.out.println("流程定义ID:"+processDefinition.getId());
            System.out.println("流程定义名称:"+processDefinition.getName());
            System.out.println("流程定义KEY:"+processDefinition.getKey());
            System.out.println("流程部署ID:"+processDefinition.getDeploymentId());
            System.out.println("流程定义版本:"+processDefinition.getVersion());
            System.out.println("######################");
        }
    }
    /**
     * 7:附加功能:查询最新版本的流程定义
     */
    @Test
    public void queryTopNewProcessDefinition2() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .orderByProcessDefinitionVersion().asc()
                .list();
        
        Map<String,ProcessDefinition> map=new HashMap<>();
        
        for (ProcessDefinition processDefinition : list) {
            map.put(processDefinition.getKey(), processDefinition);
        }
        
        for (ProcessDefinition processDefinition : map.values()) {
            System.out.println("流程定义ID:"+processDefinition.getId());
            System.out.println("流程定义名称:"+processDefinition.getName());
            System.out.println("流程定义KEY:"+processDefinition.getKey());
            System.out.println("流程部署ID:"+processDefinition.getDeploymentId());
            System.out.println("流程定义版本:"+processDefinition.getVersion());
            System.out.println("######################");
        }
        
    }

8.附加功能:删除流程定义(删除key相同的所有不同版本的流程定义)

/**
     * 8,附加功能:删除流程定义(删除key相同的所有不同版本的流程定义)
     * 
     * 已知key
     */
    
    @Test
    public void deleteAllSameVersionProcessDefinition() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        String processDefinitionKey="HelloWorld2";
        //根据KEY找流程定义的对象
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey(processDefinitionKey).list();
        for (ProcessDefinition processDefinition : list) {
            String deploymentId = processDefinition.getDeploymentId();
            repositoryService.deleteDeployment(deploymentId, true);
            
        }
    }

相关文章

网友评论

      本文标题:Activiti HelloWorld程序

      本文链接:https://www.haomeiwen.com/subject/aojgpctx.html