// ## runtimeService操作的都是executionId
runtimeService.startProcessInstanceByKey(processDefinitionKey, vars);
runtimeService.setVariables(executionId, vars);
runtimeService.setVariablesLocal(executionId, vars)
runtimeService.getVariable(executionId, varName);
runtimeService.getVariableLocal(executionId, varName);
// ## taskService操作的都是taskId
taskService.setVariables(taskId, vars);
taskService.setVariablesLocal(taskId, vars);
taskService.complete(taskId, vars);
taskService.complete(taskId, vars, localScope);
taskService.complete(taskId, vars, transientVars);
taskService.getVariable(taskId, varName);
taskService.getVariableLocal(taskId, varName);
delegateTask在TaskListener#notify(DelegateTask)可获取到。
delegateExecution在ExecutionListener#notify(DelegateExecution)可获取到(操作流程变量的api,与delegateTask是类似的)
// ## delegateTask
delegateTask.setVariables(vars);
delegateTask.setVariablesLocal(vars);
delegateTask.setTransientVariables(vars);
delegateTask.setTransientVariableLocal(vars);
delegateTask.getVariable(varName);
delegateTask.getVariable(varName, fetchaAllVariables);
delegateTask.getVariableLocal(varName);
delegateTask.getVariableLocal(varName, fetchaAllVariables);
delegateTask.getTransientVariable(varName);
delegateTask.getTransientVariableLocal(varName);
// ## delegateTask.getExecution()
delegateTask.getExecution().getVariable(varName);
delegateTask.getExecution().getVariable(varName, fetchAllVariables);
delegateTask.getExecution().getVariableLocal(varName);
delegateTask.getExecution().getVariableLocal(varName, fetchAllVariables);
delegateTask.getExecution().getTransientVariable(varName);
delegateTask.getExecution().getTransientVariableLocal(varName);
delegateTask.getExecution().setVariable(varName, val);
delegateTask.getExecution().setVariable(varName, val, fetchAllVariables);
delegateTask.getExecution().setVariableLocal(varName, val);
delegateTask.getExecution().setVariableLocal(varName, val, fetchAllVariables);
delegateTask.getExecution().setTransientVariable(varName,val);
delegateTask.getExecution().setTransientVariableLocal(varName,val);
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef">
<process id="test-var-key" name="test-var-name" isExecutable="true">
<startEvent id="startEvent1">startEvent>
<sequenceFlow id="sid-F041E175-966B-40C4-900D-89EAB5B59938" sourceRef="startEvent1" targetRef="sid-3CCFB6F9-686F-4846-B401-FFF693FA07D8">sequenceFlow>
<userTask id="sid-3CCFB6F9-686F-4846-B401-FFF693FA07D8" name="任务1">userTask>
<userTask id="sid-E5660C81-BD9A-4EC3-9F33-888D59F201AE" name="任务2">userTask>
<sequenceFlow id="sid-3D938ADD-1267-4DB0-BBD4-8B1D6595D9CD" sourceRef="sid-3CCFB6F9-686F-4846-B401-FFF693FA07D8" targetRef="sid-E5660C81-BD9A-4EC3-9F33-888D59F201AE">sequenceFlow>
<sequenceFlow id="sid-857BFBEC-CB34-4BDE-A733-4C6CB09CB700" sourceRef="sid-E5660C81-BD9A-4EC3-9F33-888D59F201AE" targetRef="sid-0F1DA5F6-185E-4910-AFC5-A51C170BD35B">sequenceFlow>
<parallelGateway id="sid-0F1DA5F6-185E-4910-AFC5-A51C170BD35B">parallelGateway>
<userTask id="sid-F6E87384-711F-4244-8B2E-DBFDD7CFA922" name="任务3">userTask>
<sequenceFlow id="sid-9ECB4E24-0F17-4632-B5AE-6CA850CE1A83" sourceRef="sid-0F1DA5F6-185E-4910-AFC5-A51C170BD35B" targetRef="sid-F6E87384-711F-4244-8B2E-DBFDD7CFA922">sequenceFlow>
<userTask id="sid-6851AA5B-9C2B-4CAB-AE1A-08FB68741B19" name="任务4">userTask>
<sequenceFlow id="sid-132CAE85-EF06-4CFA-8E19-F159BD4FCF84" sourceRef="sid-F6E87384-711F-4244-8B2E-DBFDD7CFA922" targetRef="sid-6851AA5B-9C2B-4CAB-AE1A-08FB68741B19">sequenceFlow>
<sequenceFlow id="sid-B40FB063-96CF-4073-BBA5-5A51702AA83B" sourceRef="sid-6851AA5B-9C2B-4CAB-AE1A-08FB68741B19" targetRef="sid-E79A7816-1AD6-48FD-823E-30F8E162A82C">sequenceFlow>
<parallelGateway id="sid-E79A7816-1AD6-48FD-823E-30F8E162A82C">parallelGateway>
<userTask id="sid-4949FC74-BBF4-49EF-A0B0-827B4A0AFBD2" name="任务5">userTask>
<userTask id="sid-959DCBAC-E3A1-4B99-9272-C28796B40668" name="任务6">userTask>
<sequenceFlow id="sid-990CC958-7511-40FC-A8A5-D0610339CC43" sourceRef="sid-4949FC74-BBF4-49EF-A0B0-827B4A0AFBD2" targetRef="sid-959DCBAC-E3A1-4B99-9272-C28796B40668">sequenceFlow>
<sequenceFlow id="sid-D172FE60-24C6-416A-AA18-2CC8E22A6835" sourceRef="sid-0F1DA5F6-185E-4910-AFC5-A51C170BD35B" targetRef="sid-4949FC74-BBF4-49EF-A0B0-827B4A0AFBD2">sequenceFlow>
<sequenceFlow id="sid-E34141E5-CAD5-4A68-8B81-F0846E3DC0DA" sourceRef="sid-959DCBAC-E3A1-4B99-9272-C28796B40668" targetRef="sid-E79A7816-1AD6-48FD-823E-30F8E162A82C">sequenceFlow>
<userTask id="sid-64C2097E-106D-4E14-8BF0-8A7EDC6A470C" name="任务7">userTask>
<sequenceFlow id="sid-88B705AB-B1BB-46A9-AB6F-280C8647FDA7" sourceRef="sid-E79A7816-1AD6-48FD-823E-30F8E162A82C" targetRef="sid-64C2097E-106D-4E14-8BF0-8A7EDC6A470C">sequenceFlow>
<userTask id="sid-EDBCDF98-90B0-4A19-AA29-4A66103FA620" name="任务8">userTask>
<sequenceFlow id="sid-5B05CAD1-EE33-4D39-A93E-8E71BF2ACC48" sourceRef="sid-64C2097E-106D-4E14-8BF0-8A7EDC6A470C" targetRef="sid-EDBCDF98-90B0-4A19-AA29-4A66103FA620">sequenceFlow>
<endEvent id="sid-EF953203-812E-4AF3-B9E1-EB73FA26ADB1">endEvent>
<sequenceFlow id="sid-0CA0222D-FEDF-44CD-AA15-73DE43B82F3B" sourceRef="sid-EDBCDF98-90B0-4A19-AA29-4A66103FA620" targetRef="sid-EF953203-812E-4AF3-B9E1-EB73FA26ADB1">sequenceFlow>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_test-var-key">
<bpmndi:BPMNPlane bpmnElement="test-var-key" id="BPMNPlane_test-var-key">
<bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
<omgdc:Bounds height="30.0" width="30.0" x="100.0" y="163.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-3CCFB6F9-686F-4846-B401-FFF693FA07D8" id="BPMNShape_sid-3CCFB6F9-686F-4846-B401-FFF693FA07D8">
<omgdc:Bounds height="80.0" width="100.0" x="195.0" y="138.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-E5660C81-BD9A-4EC3-9F33-888D59F201AE" id="BPMNShape_sid-E5660C81-BD9A-4EC3-9F33-888D59F201AE">
<omgdc:Bounds height="80.0" width="100.0" x="345.0" y="138.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-0F1DA5F6-185E-4910-AFC5-A51C170BD35B" id="BPMNShape_sid-0F1DA5F6-185E-4910-AFC5-A51C170BD35B">
<omgdc:Bounds height="40.0" width="40.0" x="510.0" y="158.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-F6E87384-711F-4244-8B2E-DBFDD7CFA922" id="BPMNShape_sid-F6E87384-711F-4244-8B2E-DBFDD7CFA922">
<omgdc:Bounds height="80.0" width="100.0" x="595.0" y="75.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-6851AA5B-9C2B-4CAB-AE1A-08FB68741B19" id="BPMNShape_sid-6851AA5B-9C2B-4CAB-AE1A-08FB68741B19">
<omgdc:Bounds height="80.0" width="100.0" x="740.0" y="75.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-E79A7816-1AD6-48FD-823E-30F8E162A82C" id="BPMNShape_sid-E79A7816-1AD6-48FD-823E-30F8E162A82C">
<omgdc:Bounds height="40.0" width="40.0" x="870.0" y="158.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-4949FC74-BBF4-49EF-A0B0-827B4A0AFBD2" id="BPMNShape_sid-4949FC74-BBF4-49EF-A0B0-827B4A0AFBD2">
<omgdc:Bounds height="80.0" width="100.0" x="595.0" y="210.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-959DCBAC-E3A1-4B99-9272-C28796B40668" id="BPMNShape_sid-959DCBAC-E3A1-4B99-9272-C28796B40668">
<omgdc:Bounds height="80.0" width="100.0" x="740.0" y="210.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-64C2097E-106D-4E14-8BF0-8A7EDC6A470C" id="BPMNShape_sid-64C2097E-106D-4E14-8BF0-8A7EDC6A470C">
<omgdc:Bounds height="80.0" width="100.0" x="955.0" y="138.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-EDBCDF98-90B0-4A19-AA29-4A66103FA620" id="BPMNShape_sid-EDBCDF98-90B0-4A19-AA29-4A66103FA620">
<omgdc:Bounds height="80.0" width="100.0" x="1100.0" y="138.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-EF953203-812E-4AF3-B9E1-EB73FA26ADB1" id="BPMNShape_sid-EF953203-812E-4AF3-B9E1-EB73FA26ADB1">
<omgdc:Bounds height="28.0" width="28.0" x="1245.0" y="164.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-D172FE60-24C6-416A-AA18-2CC8E22A6835" id="BPMNEdge_sid-D172FE60-24C6-416A-AA18-2CC8E22A6835">
<omgdi:waypoint x="530.5" y="197.5">omgdi:waypoint>
<omgdi:waypoint x="530.5" y="250.0">omgdi:waypoint>
<omgdi:waypoint x="595.0" y="250.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-857BFBEC-CB34-4BDE-A733-4C6CB09CB700" id="BPMNEdge_sid-857BFBEC-CB34-4BDE-A733-4C6CB09CB700">
<omgdi:waypoint x="445.0" y="178.18450184501845">omgdi:waypoint>
<omgdi:waypoint x="510.4259259259259" y="178.42592592592592">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-990CC958-7511-40FC-A8A5-D0610339CC43" id="BPMNEdge_sid-990CC958-7511-40FC-A8A5-D0610339CC43">
<omgdi:waypoint x="695.0" y="250.0">omgdi:waypoint>
<omgdi:waypoint x="740.0" y="250.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-3D938ADD-1267-4DB0-BBD4-8B1D6595D9CD" id="BPMNEdge_sid-3D938ADD-1267-4DB0-BBD4-8B1D6595D9CD">
<omgdi:waypoint x="295.0" y="178.0">omgdi:waypoint>
<omgdi:waypoint x="345.0" y="178.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-132CAE85-EF06-4CFA-8E19-F159BD4FCF84" id="BPMNEdge_sid-132CAE85-EF06-4CFA-8E19-F159BD4FCF84">
<omgdi:waypoint x="695.0" y="115.0">omgdi:waypoint>
<omgdi:waypoint x="740.0" y="115.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-9ECB4E24-0F17-4632-B5AE-6CA850CE1A83" id="BPMNEdge_sid-9ECB4E24-0F17-4632-B5AE-6CA850CE1A83">
<omgdi:waypoint x="530.5" y="158.5">omgdi:waypoint>
<omgdi:waypoint x="530.5" y="115.0">omgdi:waypoint>
<omgdi:waypoint x="595.0" y="115.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-F041E175-966B-40C4-900D-89EAB5B59938" id="BPMNEdge_sid-F041E175-966B-40C4-900D-89EAB5B59938">
<omgdi:waypoint x="130.0" y="178.0">omgdi:waypoint>
<omgdi:waypoint x="195.0" y="178.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-E34141E5-CAD5-4A68-8B81-F0846E3DC0DA" id="BPMNEdge_sid-E34141E5-CAD5-4A68-8B81-F0846E3DC0DA">
<omgdi:waypoint x="840.0" y="250.0">omgdi:waypoint>
<omgdi:waypoint x="890.0" y="250.0">omgdi:waypoint>
<omgdi:waypoint x="890.0" y="198.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-0CA0222D-FEDF-44CD-AA15-73DE43B82F3B" id="BPMNEdge_sid-0CA0222D-FEDF-44CD-AA15-73DE43B82F3B">
<omgdi:waypoint x="1200.0" y="178.0">omgdi:waypoint>
<omgdi:waypoint x="1245.0" y="178.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-5B05CAD1-EE33-4D39-A93E-8E71BF2ACC48" id="BPMNEdge_sid-5B05CAD1-EE33-4D39-A93E-8E71BF2ACC48">
<omgdi:waypoint x="1055.0" y="178.0">omgdi:waypoint>
<omgdi:waypoint x="1100.0" y="178.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-B40FB063-96CF-4073-BBA5-5A51702AA83B" id="BPMNEdge_sid-B40FB063-96CF-4073-BBA5-5A51702AA83B">
<omgdi:waypoint x="840.0" y="115.0">omgdi:waypoint>
<omgdi:waypoint x="890.5" y="115.0">omgdi:waypoint>
<omgdi:waypoint x="890.5" y="158.5">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-88B705AB-B1BB-46A9-AB6F-280C8647FDA7" id="BPMNEdge_sid-88B705AB-B1BB-46A9-AB6F-280C8647FDA7">
<omgdi:waypoint x="909.5833333333334" y="178.41666666666666">omgdi:waypoint>
<omgdi:waypoint x="955.0" y="178.2183406113537">omgdi:waypoint>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
将流程部署后,使用runtimeService.startProcessInstanceByKey(procDefKey, vars)开启流程,并且传入自定义变量:p1=p1-p1。
得到流程实例:【流程实例id: 2501 执行实例id: 2501(2501的父执行实例id是null), 2503(2503的父执行实例id是2501) 当前任务id: 2506(所属执行实例是2503)】
/**
* 开启流程
*/
@Test
public void test_03() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
HashMap<String, Object> vars = new HashMap<>();
vars.put("p1", "p1-p1");
runtimeService.startProcessInstanceByKey("test-var-key", vars);
}
/**
* 完成任务
*/
@Test
public void test_04() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
// 流程实例id: 2501 执行实例id: 2501, 2503 当前任务id: 2506
Task task = taskService.createTaskQuery().processInstanceId("2501").singleResult();
// p1-p1
// 先执行(先查当前执行实例):
// select * from ACT_RU_VARIABLE where EXECUTION_ID_ = '2503' and NAME_= 'p1' and TASK_ID_ is null
// 再执行(再查父executionId):
// select * from ACT_RU_VARIABLE where EXECUTION_ID_ = '2501' and NAME_= 'p1' and TASK_ID_ is null
System.out.println(runtimeService.getVariable(task.getExecutionId(), "p1"));
// null
// 只执行(只查当前执行实例):
// select * from ACT_RU_VARIABLE where EXECUTION_ID_ = '2503' and NAME_= 'p1' and TASK_ID_ is null
System.out.println(runtimeService.getVariableLocal(task.getExecutionId(), "p1"));
// p1-p1
// 先执行(先查当前taskId):
// select * from ACT_RU_VARIABLE where TASK_ID_ = '2506' and NAME_= 'p1'
// 再执行(再查当前taskId所属exectionId):
// select * from ACT_RU_VARIABLE where EXECUTION_ID_ = '2503' and NAME_= 'p1' and TASK_ID_ is null
// 再执行(再查当前taskId所属exectionId的父executionId):
// select * from ACT_RU_VARIABLE where EXECUTION_ID_ = '2501' and NAME_= 'p1' and TASK_ID_ is null
System.out.println(taskService.getVariable(task.getId(), "p1"));
// null
// 只执行(只查当前taskId):
// select * from ACT_RU_VARIABLE where TASK_ID_ = ? and NAME_= ?
System.out.println(taskService.getVariableLocal(task.getId(), "p1"));
}
@Test
public void test_05() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery().processInstanceId("2501").singleResult();
// 执行:(这里插入的是父执行实例的executionId: 2501)
// insert into ACT_RU_VARIABLE (ID_, REV_, TYPE_, NAME_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, BYTEARRAY_ID_, DOUBLE_, LONG_ , TEXT_, TEXT2_) values ( ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 5001 1 string e1 2501 2501 null e1-e1
runtimeService.setVariable(task.getExecutionId(), "e1", "e1-e1");
// 执行:(这里插入的是当前执行实例的executionId: 2503)
// insert into ACT_RU_VARIABLE (ID_, REV_, TYPE_, NAME_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, BYTEARRAY_ID_, DOUBLE_, LONG_ , TEXT_, TEXT2_) values ( ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 7501 1, string,e11, 2501, 2503(String), null, e11-e11(String)
runtimeService.setVariableLocal(task.getExecutionId(), "e11", "e11-e11");
// 执行:(这里居然没有插入taskId, 而是插入了procInstId和executionId, 并且其中的executionId是父executionId, 效果与runtimeService.setVariable(task.getExecutionId(), "e1", "e1-e1");一致)
// insert into ACT_RU_VARIABLE (ID_, REV_, TYPE_, NAME_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, BYTEARRAY_ID_, DOUBLE_, LONG_ , TEXT_, TEXT2_) values ( ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 10001,1 string,t1, 2501, 2501, null, t1-t1
taskService.setVariable(task.getId(), "t1", "t1-t1");
// 执行:(这里插入了taskId, 并且插入了对应的procInstId和当前执行实例executionId)
// insert into ACT_RU_VARIABLE (ID_, REV_, TYPE_, NAME_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, BYTEARRAY_ID_, DOUBLE_, LONG_ , TEXT_, TEXT2_) values ( ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 12501,1 string,t11, 2501, 2503, 2506, t11-t11
taskService.setVariableLocal(task.getId(), "t11", "t11-t11");
}
/**
* 完成任务
*/
@Test
public void test_05() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
// 流程实例id: 2501 执行实例id: 2501, 2503 当前任务id: 2506
Task task = taskService.createTaskQuery().processInstanceId("2501").singleResult();
HashMap<String, Object> vars = new HashMap<>();
vars.put("t2", "t2-t2");
// 执行:(这里插入了taskId, 并且插入了对应的procInstId和当前执行实例executionId, 等同于taskService.setVariable(task.getId(), varName, var);)
// insert into ACT_RU_VARIABLE (ID_, REV_, TYPE_, NAME_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, BYTEARRAY_ID_, DOUBLE_, LONG_ , TEXT_, TEXT2_) values ( ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 15001,1 string,t2, 2501(String), 2501(String), t2-t2,
taskService.complete(task.getId(), vars); // 同: taskService.complete(task.getId(), vars, false);
// 此任务完成之后, 不会往act_ru_variable表中插入数据
// taskService.complete(task.getId(), vars, true);
}