Reputation:
While reading (https://docs.camunda.org/manual/7.5/user-guide/process-engine/variables/) I am not sure how you retrieve a variable?
At the moment I am struggling to find out how to access previously set process variables. What I tried is:
I have a simple bpmn process in which i have start event, 1 service task and end event, I am starting my process by passing 2 variables (a&b) and my service task is implementing following java class:
public class Addition implements JavaDelegate {
public void execute(DelegateExecution exe) throws Exception {
System.out.println("Inside calculator again");
Integer x = (Integer) exe.getVariable("a");
Integer y = (Integer) exe.getVariable("b");
int add = x+y;
System.out.println("Addition of two number is"+add);
exe.setVariable("add",add);
}
I am starting my process as follows:
public void sayHello(ProcessEngine processEngine)
{
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("a", 3);
variables.put("b", 5);
ProcessInstance instance= processEngine.getRuntimeService().startProcessInstanceByKey("Process_3", variables);
}
I want to access add
variable (present in Addition class) in sayHello class?
As process has been completed so I can't use runtimeService so I tried to use history service but couldn't find out any solution.
Is there any Java API which I can use or is there any other way?
Upvotes: 1
Views: 8288
Reputation: 706
I use flowable. I have no problem to get those processInstance level variables. They are at root level. All the execution, taskservice, javaDelegate can get them out. I guess it is same for camunda.
select * from ACT_RU_VARIABLE
//to set
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder().processDefinitionKey(key)
.variable(AppConstants.TENANT_ID, tenantId)
.variable(AppConstants.CONSUMER_ID, consumerId)
.start();
//from taskService
Task currentTask = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
var testVariable = taskService.getVariables(currentTask.getId());
//from JavaDelegate
public class notificationService implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {
Map<String, Object> variables = execution.getVariables();
String str = variables.get(AppConstants.TENANT_ID) + " "
+ variables.get(AppConstants.CONSUMER_ID) + " "
+ execution.getProcessInstanceId();
Upvotes: 0
Reputation: 307
for anyone struggling with the same issue the variables used to start the process are process variables, the ones we retrieve via delegate are local ones so you have to somehow get to the process instance. To make your code works I'll rewrite it as follow (will change from implementing JavaDelegate to implementing ActivityBehavior and to get the variable you have to go througth call to getParent()).
public class Addition implements ActivityBehavior{
public void execute(ActivityExecution exe) throws Exception {
System.out.println("Inside calculator again");
int x = (int) exe.getParent().getVariable("a");
int y = (int) exe.getParent().getVariable("b");
int add = x+y;
System.out.println("Addition of two number is: " + add);
exe.setVariable("add",add);
}
The sayHello method won't change
public void sayHello(ProcessEngine processEngine)
{
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("a", 3);
variables.put("b", 5);
ProcessInstance instance=
processEngine.getRuntimeService().startProcessInstanceByKey("Process_3", variables);
}
Upvotes: 0
Reputation: 5516
If you want to get all historic variable instances use the list
method in the HistoricVariableInstanceQuery
.
For Example
List<HistoricVariableInstance> variables = processEngine.getHistoryService().createHistoricVariableInstanceQuery.list();
If you want to get specific variables with the given name you can use the method variableName(String)
For Example:
List<HistoricVariableInstance> variables = processEngine.getHistoryService().createHistoricVariableInstanceQuery().variableName("myVar").list();
To get the variables of a specific process instance use the method processInstanceId
For Example:
List<HistoricVariableInstance> variables = processEngine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()).variableName("myVar").list();
See for further information the documentation of the HistoryService and HistoricVariableInstanceQuery
Upvotes: 4