工作流JBPM_day01:7-使用流程变量

工作流JBPM_day01:7-使用流程变量

工作流就像流水线

  

  

  

  

对应数据库中的一张表

  

ProcessVariableTest.Java

import java.util.List;

import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.task.Task;
import org.junit.Test;

public class ProcessVariableTest {
    private ProcessEngine processEngine = Configuration.getProcessEngine();
    
    // 启动流程实例
    // jbpm4_execution
    @Test
    public void testStartProcessInstance() {
        ProcessInstance pi = processEngine.getExecutionService().startProcessInstanceByKey("helloworld");
        System.out.println("流程实例启动成功:id=" + pi.getId());// 所使用的流程定义的Id
    }
    //设置流程变量
    @Test
    public void testSetVariable() {
        String executionId = "helloworld.170001";
        processEngine.getExecutionService().setVariable(executionId, "请假天数", 15);
    }
    
    //获取流程变量
    @Test
    public void testGetVariable() {
        String executionId = "helloworld.170001";
        Integer days = (Integer) processEngine.getExecutionService().getVariable(executionId, "请假天数");
        System.out.println("请假天数=" + executionId);
    }
    
    /**
    {
        ExecutionService executionService = processEngine.getExecutionService();
        TaskService taskService = processEngine.getTaskService();

        // ============ 设置变量 ========================
        executionService.setVariable(executionId, name, value); // 设置一个变量
        executionService.setVariables(executionId, variablesMap); // 设置多个变量
        taskService.setVariables(taskId, variables); // 设置多个变量

        executionService.startProcessInstanceByKey(processDefinitionKey, variablesMap); // 启动流程实例时,先设置一些变量
        taskService.completeTask(taskId, variablesMap); // 真正办理完任务前先设置一些变量
        
        
        // ============ 获取变量 ========================
        executionService.getVariable(executionId, variableName); // 获取一个变量
        executionService.getVariableNames(executionId); // 返回Set<String>,是所有变量的名称集合
        executionService.getVariables(executionId, variableNames); //获取多个变量,返回Map<String,Object>,表示指定名称的变量信息
    
        taskService.getVariable(taskId, variableName);
        taskService.getVariableNames(taskId);
        taskService.getVariables(taskId, variableNames);
    }
    */
}

  

Form.java

public class Form /* implements java.io.Serializable */{

    private Long id;
    private String title;

    // ...

    public Form() {
    }

    public Form(String title) {
        this.title = title;
    }

    public Form(Long id, String title) {
        this.id = id;
        this.title = title;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "[Form: id=" + id + ", title=" + title + "]";
    }
}

Form.hbm.xml

<hibernate-mapping package="cn.itcast.d_processvariable">

    <class name="Form" table="itcast_form" lazy="false">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="title"/>
    </class>
    
</hibernate-mapping>

  

  

ProcessVariableTest2.Java

import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.junit.Test;

public class ProcessVariableTest2 {
    private ProcessEngine processEngine = Configuration.getProcessEngine();

    // 启动流程实例
    @Test
    public void testStartProcessInstance() throws Exception {
        ProcessInstance pi = processEngine.getExecutionService().startProcessInstanceByKey("helloworld");
        System.out.println("流程实例启动成功:id=" + pi.getId());
    }

    // 设置流程变量
    @Test
    public void testSetVariable() throws Exception {
        String executionId = "helloworld.190001";
        Form form = new Form(1L, "我要请假,我是张三"); // 通过指定id来模拟一个游离状态的对象
        processEngine.getExecutionService().setVariable(executionId, "form", form);
    }

    // 获取流程变量
    @Test
    public void testGetVariable() throws Exception {
        String executionId = "helloworld.190001";
        Form form = (Form) processEngine.getExecutionService().getVariable(executionId, "form");
        System.out.println(form);
    }
}

原文地址:https://www.cnblogs.com/justdoitba/p/8012668.html