public class ProcessVariablesTest
{
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 部署流程定义
* this.getClass().getClassLoader().getResourceAsStream("diagrams/variables.bpmn") 从classpath根目录下加载文件
* this.getClass().getResourceAsStream("variables.bpmn") 从但前包下加载指定文件
* this.getClass().getResourceAsStream("/diagrams/variables.bpmn") 从classpath根目录下加载文件
*/
@Test
public void depolymentProcessDefinition_InputStream()
{
InputStream inbpmn = this.getClass().getClassLoader().getResourceAsStream("diagrams/variables.bpmn");
InputStream inpng = this.getClass().getResourceAsStream("/diagrams/variables.png");
Deployment deploy = processEngine.getRepositoryService()
.createDeployment()
.name("变量流程")
.addInputStream("variables.bpmn", inbpmn)
.addInputStream("variables.png", inpng)
.deploy();
System.out.println(deploy.getId());
System.out.println(deploy.getName());
}
/**
* 启动流程
*/
@Test
public void startProcessDefinition()
{
String key = "variables";
ProcessInstance processInstance = processEngine.getRuntimeService()
.startProcessInstanceByKey(key);
System.out.println(processInstance.getId());
System.out.println(processInstance.getProcessDefinitionId());
System.out.println(processInstance.getProcessDefinitionKey());
System.out.println(processInstance.getProcessDefinitionName());
System.out.println(processInstance.getProcessInstanceId());
}
/**
* 完成任务
*/
@Test
public void complteTask()
{
String taskId = "65002";
processEngine.getTaskService().complete(taskId);
System.out.println("任务完成");
}
/**
* 设置变量
*/
@Test
public void setVariables()
{
String taskId = "70004";
TaskService taskService = processEngine.getTaskService();
// taskService.setVariableLocal(taskId, "请假天数", 2); // 与任务ID绑定,绑定任务ID,存储变量的表和 历史表不会覆盖
// taskService.setVariable(taskId, "请假原因", "探亲111");
// taskService.setVariable(taskId, "请假日期", new Date());
/**
* 实体类要求
* 1、序列化 implements Serializable
* 2、添加固定版本信息 private static final long serialVersionUID = -6638629184599698536L;
* 不序列化 无法设置
* 不添加固定版本信息,如果实体类属性变化获取时出现异常
*/
Hostiry s = new Hostiry();
s.setDate(new Date());
s.setDays(4);
s.setParson("吃饭");
taskService.setVariableLocal(taskId, "请假信息", s);
}
/**
* 获取变量
*/
@Test
public void getVariablse()
{
TaskService taskService = processEngine.getTaskService();
String taskId = "47504";
// Date date = (Date) taskService.getVariable(taskId, "请假日期");
// Integer days = (Integer) taskService.getVariable(taskId, "请假天数");
// String reason = (String) taskService.getVariable(taskId, "请假原因");
//
// System.out.println(date);
// System.out.println(days);
// System.out.println(reason);
/**
* 实体类
*
*/
Hostiry h = (Hostiry) taskService.getVariable(taskId, "请假信息");
System.out.println(h.getParson());
System.out.println(h.getDays());
System.out.println(h.getDate());
}
/**
* 模拟设置流程变量设置和获取
*/
public void setAndGetVariables()
{
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
/**设置流程变量**/
// runtimeService.setVariable(executionId, variableName, value); // 执行对象ID, 变量名称, 变量值
// runtimeService.setVariables(executionId, map); // 执行对象ID 多个变量
// taskService.setVariable(taskId, variableName, value); // 任务ID,变量名称,变量值
// taskService.setVariables(taskId, map); // 任务ID,多个变量
// runtimeService.startProcessInstanceByKey(processDefinitionKey, map); // 启动流程实例同时设置变量
// taskService.complete(taskId, map); // 完成任务是设置变量
// runtimeService.getVariable(executionId, variableName) // 执行对象ID,变量名
// Map<String, Object> map = runtimeService.getVariables(executionId); // 执行对象ID获取所有变量
// Map<String, Object> map = runtimeService.getVariables(executionId, variableNames) // 执行对象ID,变量名称集合
// taskService.getVariable(taskId, variableName) // 任务ID,变量名
// Map<String, Object> map = taskService.getVariables(taskId); // 任务ID获取所有变量
// Map<String, Object> map = taskService.getVariables(taskId, variableNames) // 任务ID,变量名称集合
}
/**
* 查询历史流程变量信息
*/
@Test
public void findHistoryVariables()
{
String taskId = "70004";
List<HistoricVariableInstance> list = processEngine.getHistoryService()
.createHistoricVariableInstanceQuery()
.taskId(taskId)
.list();
if (list != null && !list.isEmpty())
{
for (HistoricVariableInstance h : list)
{
Hostiry s = (Hostiry) h.getValue();
System.out.println(h.getTaskId() + " " + h.getVariableName() + " " + s.getDays() + " " + s.getParson());
}
}
}
}