At the moment I am struggling to find out how to access previously set process variable. 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??
I’ve never used the java api, but is it possible that you cannot retrieve the runtime variables because the execution has no pauses and you are trying to access an execution that has already finished?
sayHello is the launcher of the process & where you want to get the output of your process (if I understand you well).
Actually I don’t see your scenario as an acceptable use case to be implemented as a BPMN model.
A series of activities will be executed following the start event
In your case all subsequent activities are automated (service task & end event)
but in most use cases many of subsequent activities require user interaction or get in a wait state.
Even in your case it needs unknown time for the automated activities to be done
Actually I am new to camunda- bpm and exploring it that’s why i am doing nothing in my process.
I added a receive task before my end event and then by using following query i am able to get access to my add variable in sayHello class.
processEngine.getRuntimeService().getVariable(instance.getId(), “variable”)
and then i can end my process by using
Execution execution = processEngine.getRuntimeService().createExecutionQuery()
.processInstanceId(instance.getId()).activityId(“ReceiveTask_1vxlm60”) .singleResult();
processEngine.getRuntimeService().signal(execution.getId());
Alternatively i can use HistoryService query after completion of my project to get ‘add’ variable in sayHello class as suggested by Deivarayan.
In your case, you can only access the runtime variable inside the execution listener. As far as i understand, you dont have any activity where the execution would wait, so process ends after startProcessInstanceByKey() is executed. So you cannot access the runtime variable after the process has ended.
Also I dont get a clear picture of your BPMN model. Could you post your bpmn diagram/xml?
I am passing variables as an argument where i pass a and b as process variable and i use in my calculator class and want to retrieve their addition in variable c in my sayHello class.