Keshav Taparia Keshav Taparia - 4 months ago 63
Java Question

How to use process variables in Camunda-bpm?

I'm new to Camunda and didn't find any tutorial or reference explaining how to achieve the following:
I have a simple bpmn process in which I am using a service task. I want to execute my process by using processEngine.getRuntimeService().startProcessInstanceByKey("Process_1", variables);
where my variables are as follows:

Map variables = new HashMap();
variables.put("a", 2);
variables.put("b", 5);


Now my service task is implementing a java class in which I want to use to process variables "a" and "b"?

How can I get the same process variables "a" and "b" in that class?

Answer

Let your class implement JavaDelegate and add it as service delegate in the modeler. Then access the variable via the DelegateExecution instance camunda passes to the execute method.

public class MyServiceDelegate implements JavaDelegate {
    public void execute(DelegateExecution execution) {
        execution.getVariable("a");
    }
}

This is very basic and for shure is covered in the getting started examples. Maybe a good idea to take a step back and study the fundamental concepts?