My bpmn file is as follows: Addition.bpmn
I am using receive task so that i can use RuntimeService, I am starting my process in sayHello class as follows:
public void sayHello(ProcessEngine processEngine) {
try {
System.out.println("inside postdeploy ");
variables.put("a", 2);
variables.put("b", 5);
variables.put("c", 0);
ProcessInstance instance= processEngine.getRuntimeService().startProcessInstanceByKey("Process_2", variables);
variables.put("c",processEngine.getRuntimeService().getVariable(instance.getId(), "c"));
Execution execution = processEngine.getRuntimeService().createExecutionQuery()
.processInstanceId(instance.getId())
.activityId("ReceiveTask_16nulbx")
.singleResult();
processEngine.getRuntimeService().signal(execution.getId());
I set my c variable in Calculator class which is implementing by my service task as follows:
public class Calculator 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 is"+add);
exe.setVariable("c", add);
}
The problem is my process instance is not ended after this. My question is how can i end my Process instance after getting my c variable?
The problem is that your process instance object will not be updated after the receive task is completed. You have to query the process instance if you want to check whether the instance is ended or not. Use the following statement to check if the instance still exists:
ProcessInstance processInstance = processEngine
.getRuntimeService()
.createProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
If the processInstance
is null
then the instance was completed.