Activiti HelloWorld程序

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);
            
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容