Intermediate Response from Asynchronous Service Task

Hi,

I have a BPMN Process1 which has two Service Task. ServiceTask1 generates a random value then call the ServiceTask2 which internally call other system. ServiceTask2 takes long time to complete and that’s why it is asynchronous.
I am initiating the process through REST url and I want that my random generated number from ServiceTask1 one should come as a REST response immediately while ServiceTask2 can take long time to complete.
How I can send the random number Response from ServiceTask1 intermediately into REST Response?

Thanks in Advance
SG

Hi Supriyo

You may want your process model to look something like below;

Start->ServiceTask1---->ServiceTask2->End

If you are starting the process using the REST API and ServiceTask1 is not asynchronous and the random number is stored as a process variable, then set the withVariablesInReturn flag to true in the start process request body and the random value will be returned in the start process call.

See docs here for more detail…

regards

Rob

Hi Rob,

Thanks for your response. I have got all the process variables now using (“withVariablesInReturn”: true) but that does not include the random value which has been calculated and assigned into a variable in ServiceTask1 because that’s probably is not the part of process variable. Can I set my variable and value (which I am generating in ServiceTask1 by using some logic) into the process variable so that I can get that in REST response? Or by any other way can I get variable and value into my REST Response?

Thanks Again,
SG

Hi,

In serviceTask1, you will need some code something like;

int rand = randomInt();
execution.setVariable(“Random_Int”,rand);

Thus you should get a variable called Random_Int in your return variables… Then your serviceTask2 can use this random value via execution.getVariable(“Random_Int”)

regards
Rob

Hi Rob,

Thank you for your response. Yes i can get “rand_int” in ServiceTask2 by setting and getting in execution in ServiceTask1 but I want to have “rand_int” back in REST response when Asynchronous ServiceTask2 call start. Is it possible?

Thanks in Advance
SG

Hi,

Im not sure where your async is occurring. Hence the process pattern Im talking to looks like this;

regards

Rob

Hi Rob,

Thanks for your detailed response. The design is absolutely perfect. I am with similar design. I am adding “withVariablesInReturn”: true in REST Request but I dont find the “Rand” (variable which i created in ServiceTadk1) in REST Response. I received all process variables but not the Rand variable.

Anything am I mistaking here?

Thank You Again
SG

Hi,

it should be working then. Check that you set a process variable in serviceTask1. Also look in cockpit to ensure it exists as a process variable…

regards

Rob

Hi Rob,

I have checked but not found my “sampleOrderId” variable in my REST response in process variables. I am attaching the implementation here below. Can you please suggest where I am mistaking?

ServiceTask1 Code

public void execute(DelegateExecution execution) throws Exception {
RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
String randomOrderId = generateOrderIdSample();
execution.setVariable(“sampleOrderId”,sampleOrderId);
runtimeService.setVariable(execution.getProcessInstanceId(), “sampleOrderId”, randomOrderId);
}
private String generateOrderIdSample()
{
// create instance of Random class
Random rand = new Random();

// Generate random integers in range 0 to 999 
int rand_int1 = rand.nextInt(1000);
return Integer.toString(rand_int1);

} 

REST Request is -

{
“orderName”:“Order1”,
“orderId”:"",
“orderType”:“New”,
“withVariablesInReturn”: true
}

REST Response is -
{“context”:{“headers”:{},“entity”:“efef6f50-da26-11ea-a72d-c85b76191d47”,“entityType”:“java.lang.String”,“entityAnnotations”:[],“entityStream”:{“closed”:false,“committed”:false},“length”:-1,“language”:null,“location”:null,“date”:null,“lastModified”:null,“mediaType”:null,“committed”:false,“acceptableMediaTypes”:[{“type”:"",“subtype”:"",“parameters”:{},“quality”:1000,“wildcardSubtype”:true,“wildcardType”:true}],“acceptableLanguages”:["*"],“requestCookies”:{},“responseCookies”:{},“allowedMethods”:[],“entityTag”:null,“links”:[],“stringHeaders”:{},“lengthLong”:-1,“entityClass”:“java.lang.String”},“status”:200,“length”:-1,“language”:null,“location”:null,“date”:null,“lastModified”:null,“metadata”:{},“mediaType”:null,“entity”:“efef6f50-da26-11ea-a72d-c85b76191d47”,“allowedMethods”:[],“cookies”:{},“statusInfo”:“OK”,“entityTag”:null,“links”:[],“stringHeaders”:{},“headers”:{}}


Thanks in Advance,
SG

Hi

you only need the first line from here;

execution.setVariable(“sampleOrderId”,sampleOrderId);
runtimeService.setVariable(execution.getProcessInstanceId(), “sampleOrderId”, randomOrderId);

Can you check that your serviceTask1 is not asynchronous with respect to the start of the process. In other words, ensure in your process model that your serviceTask1 is called before your client calling request returns…

R

Hi Rob,

I have verified that my ServiceTask1 is NOT Asynchronous. ServiceTask2 is Asynchronous. I have also added a break point in ServiceTask2 which confirms that response is returning only when ServiceTask2 is invoked. Still response is not containing the variable which has been defined in ServiceTask1 as I mentioned.

Do you have any clue for this issue?

Thanks in Advance
SG

Hi Rob,

Also like to mention that below command I am using to return the response.
Response.status(200).entity(processInstance.getId()).build();

Is that fine or this should be different?

Thanks
SG

Hi SG,

Actually Im a bit puzzled by your last post. I thought your client was starting a process instance using the Camunda native REST API. Are you starting your process instance a different way?

regards

Rob

Hi Rob,

I have just able to get the variable in my Rest Controller by using a little bit of java code. I am really thank full to you for your super fast help and support and for the right direction to get that out from the crowd of data… I must give you a Big Thank to you.

Thanks You Very Much Rob.
Regards,
SG

Hi Rob,

I must inform you that I have changed my process instance initiation call from browser REST to my newly created Rest Controller programatically and I have able to get the variables from there. This has also solved my purpose.

Thanks for all your effort and support Rob.

Many Many Thanks,
SG