I want to know how can I start a process with a REST call (synchronous) and retrieve a variable after the end of the process and return it in the call response?.
This is how I did it but the response is not synchronus :
Controller @PostMapping(path =“/publish”)
public ResponseEntity publishAd(@RequestBody Ad ad) {
ProcessInstance instance=camunda.getRuntimeService()
.createMessageCorrelation("PublishAdEvent")
.processInstanceBusinessKey(message.getTraceid())
.setVariable("Ad",ad)
.correlateWithResult()
.getProcessInstance();
// Here I retrieve the variable in data base with the id of the instance
String responseStatus = historicRepo.getLastStatus(instance.getId());
// I want to return the response after the end of the process but it doesn't work!!!
return responseStatus;
}
Some Adapter @Component
public class PublishAdAdapter implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
//I want to retrieve this Variable after the end of the process
execution.setVariable("responseStatus", "Created");
}
}
This is my BPMN: I’m working with Kafka, that’s why i used Send Task
@Adel16 if you want to execute the whole process execution in single thread(synchronously), then you shouldn’t mark asyncBefore/asyncAfter = true.
And to return the variables in the response you need to use the function .correlateAllWithResultAndVariables(), refer below code.
import java.util.List;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.MessageCorrelationResultWithVariables;
import org.camunda.bpm.engine.variable.VariableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessagingService {
@Autowired
private ProcessEngine processEngine;
public String PublishAds(String ad) {
List<MessageCorrelationResultWithVariables> messageCorrelationResultWithVariables = processEngine
.getRuntimeService().createMessageCorrelation("PublishAdEvent").processInstanceBusinessKey("traceId")
.setVariable("Ad", ad).correlateAllWithResultAndVariables(true);
//looping through each variable instances
messageCorrelationResultWithVariables.forEach(var -> {
VariableMap variableMap = var.getVariables();
String someValue = (String) variableMap.get("someVariable");
});
// Here I retrieve the variable in data base with the id of the instance
String responseStatus = "";
// I want to return the response after the end of the process but it doesn't
// work!!!
return responseStatus;
}
}
@Adel16 A Receive Task is a simple task that waits for the arrival of a certain message. When the process execution arrives at a Receive Task, the process state is committed to the persistence storage. This means that the process will stay in this wait state until a specific message is received by the engine, which triggers continuation of the process beyond the Receive Task., so you’ll get response once the execution token arrives at the first waiting state (response 1 task). If you would like to receive the response with variables at the end of the process execution, then I would suggest you to add webhooks as callback in the end event of the process to return the variables.
I tried asyncBefore/asyncAfter = false and the code bellow did not return to me the variable witch is setted in the last Adapter (“responseStatus”), so I don’t understand how the Rest Call for the whole process is Synchronous?, because i’m not able to return this variable to my Client
Do you have any example with a REST CALL to the whole process with a response retrieved in the end event?
Dummy solution would be to spin instance of process in rest call and loop with sleep checking if processes ended and then returning response to caller.
I didn’t find solution for this problem, I want to know if Camunda allows to do REST Call and return response after the end of the whole process or every execution is synchronus? If Yes, do you have any example or video to solve this kind of need?