(十)Activitivi5之啟動流程/完成任務的時候設置流程變量


一、啟動流程的時候設置流程變量

  1.1 案例

    /**
     * 啟動流程實例
     */
    @Test
    public void start() {
        Student student=new Student();
        student.setId(1);
        student.setName("張三");

        Map<String, Object> variables=new HashMap<String,Object>();
        variables.put("days", 2);
        variables.put("date", new Date());
        variables.put("reason", "發燒");
        variables.put("student", student);
        
        ProcessInstance instance=processEngine.getRuntimeService() // 運行時Service
            .startProcessInstanceByKey("StudentLeaveProcess",variables);   // 流程定義表act_re_procdef的KEY字段值
        
        System.out.println("流程實例ID:"+instance.getId());
        System.out.println("流程定義ID:"+instance.getProcessDefinitionId()); 
    }
    
  • 如上述例子流程啟動之后,任何任務節點都可以通過excutionId獲取到流程變量的值。
  •  

    /**
     * 獲取流程變量數據
     */
    @Test
    public void getVariableValues(){
        RuntimeService runtimeService=processEngine.getRuntimeService();
        String excutionId="52501";
        Integer days=(Integer) runtimeService.getVariable(excutionId, "days");
         Date date=(Date) runtimeService.getVariableLocal(excutionId, "date");
        String reason=(String) runtimeService.getVariable(excutionId, "reason");
        Student student=(Student) runtimeService.getVariable(excutionId, "student"); 
        System.out.println("請假天數:"+days);
        System.out.println("請假日期:"+date);
        System.out.println("請假原因:"+reason);
        System.out.println("請假對象:"+student.getId()+","+student.getName());
    }

 

 

 二、完成任務的時候設置流程變量

   2.1  需求

  • 在完成某個任務節點之后設置流程變量,接下來的任務節點都可以使用這個流程變量。

 比如,當完成“學生請假”任務節點之后設置流程變量,然后在“班長審批”和“班主任審批”節點就可以獲取該流程變量。

 

  2.2  案例

 

/**
     * 完成任務
     */
    @Test
    public void test_completeTask2() {
        
        Student student=new Student();
        student.setId(1);
        student.setName("張三");

        Map<String, Object> variables=new HashMap<String,Object>();
        variables.put("days", 2);
        variables.put("date", new Date());
        variables.put("reason", "發燒");
        variables.put("student", student);
        
       processEngine.getTaskService().complete("62504",variables);
    }
    
    /**
     * 獲取流程變量數據
     */
    @Test
    public void getVariableValues(){
        RuntimeService runtimeService=processEngine.getRuntimeService();
        String excutionId="62501";
        Integer days=(Integer) runtimeService.getVariable(excutionId, "days");
         Date date=(Date) runtimeService.getVariableLocal(excutionId, "date");
        String reason=(String) runtimeService.getVariable(excutionId, "reason");
        Student student=(Student) runtimeService.getVariable(excutionId, "student"); 
        System.out.println("請假天數:"+days);
        System.out.println("請假日期:"+date);
        System.out.println("請假原因:"+reason);
        System.out.println("請假對象:"+student.getId()+","+student.getName());
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM