介绍: 在任务的执行过程中,会有很多的变量参数(比如请假任务需要天数等)。变量从作用域来分:本地变量和全局变量。 从类型区分:基本变量、自定义变量和BPMN自带变量。
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
//直接new一个任务,不是生产上的方式,此处只为演示
String taskId = UUID.randomUUID().toString();
Task task = taskService.newTask(taskId);
task.setName("基本变量测试任务");
taskService.saveTask(task);
//给指定任务设置任务变量。此处变量类型为String
taskService.setVariable(taskId,"variable1", "hello");
//查询任务变量
Object variable1 = taskService.getVariable(taskId, "variable1");
System.out.println(variable1);
//关闭
processEngine.close();
System.exit(0);
private String id;
private String username;
private String password;
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.newTask("1");
task.setName("自定义变量测试任务");
taskService.saveTask(task);
//给任务设置自定义变量,变量是序列化后保存的
Person person = new Person();
person.setId("1");
person.setUsername("xjf");
person.setPassword("123456");
taskService.setVariable(task.getId(),"person1",person);
//查询任务变量
Person person1 = taskService.getVariable(task.getId(), "person1", Person.class);
System.out.println("查询到的变量:" person1);
processEngine.close();
System.exit(0);
介绍: 本地变量是指在一个任务节点的变量,该变量只在当前节点有效,当该节点complete后会实效(在数据库中被删除)。全局变量是对该流程实例一直有效,可以随时获取。测试如下:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
//发布
Deployment deployment = repositoryService.createDeployment().addClasspathResource("variable.bpmn").deploy();
//获取流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//创建流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
System.out.println("当前任务:" task.getName());
//设置任务本地变量:该变量只在当前任务节点有效
taskService.setVariableLocal(task.getId(),"date","2019-11-20");
//获取本地变量
System.out.println("当前任务本地变量:" taskService.getVariableLocal(task.getId(), "date"));
//设置全局变量
taskService.setVariable(task.getId(),"days","9");
//可以查看数据库,本地变量存在,complete之后再查看会没有了
Thread.sleep(10000);
//当前任务结束
taskService.complete(task.getId());
System.out.println("============================分隔符===========================");
//继续获取下个任务
task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
System.out.println("当前任务:" task.getName());
System.out.println("再次获取上个任务的本地变量:" taskService.getVariableLocal(task.getId(), "date"));
System.out.println("获取全局变量:" taskService.getVariable(task.getId(),"days"));
System.out.println("getVariableLocal方法获取全局变量:" taskService.getVariableLocal(task.getId(),"days"));
processEngine.close();
System.exit(0);
**介绍:**在bpmn文件中,我们可以根据bpmn规范直接加入变量,在使用时可以直接使用代码获取。由此可以减少代码变量跟流程的耦合。测试如下:
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
<!--此处为变量对象-->
<dataObject id="personName" name="personName"
itemSubjectRef="xsd:string">
<extensionElements>
<activiti:value>dale</activiti:value>
</extensionElements>
</dataObject>
<startEvent id="_2" name="StartEvent"/>
<userTask activiti:exclusive="true" id="_3" name="请假填写"/>
<userTask activiti:exclusive="true" id="_4" name="经理审批"/>
<endEvent id="_5" name="EndEvent"/>
<sequenceFlow id="_6" sourceRef="_2" targetRef="_3"/>
<sequenceFlow id="_7" sourceRef="_3" targetRef="_4"/>
<sequenceFlow id="_8" sourceRef="_4" targetRef="_5"/>
</process>
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("dataobject.bpmn").deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
System.out.println(processDefinition.getId());
//获取流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
//获取任务
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
//获取bpmn中带的变量
System.out.println("变量:" taskService.getVariable(task.getId(), "personName"));
processEngine.close();
System.exit(0);