(十)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());
    }

 

转载于:https://www.cnblogs.com/shyroke/p/8000757.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值