Requesting Help : How to trigger two Async REST API call's go out concurrently

Supriya25
Tera Guru

Hi Team,

 

we have two REST API call's via MID-servers based on user inputs
if user gives his inputs any one of REST API call will trigger, if users doesn't give his input then two REST API call's will trigger.

Current SETUP : here system running API calls  sequentially 
example :

Field name : Requesting User input  
var gr1="";var gr2=""; var r ="".....etc;
if(Requesting User input  =="")
{
 gr1 = true;
 gr2 = true;
}
if(gr1)
{
r= new sn_ws.RESTMessageV2(........);
..
..
response=r.executeAsync();  // it is Asysnc Method
responseBoby = response.getBody();
httpstatus = response.getStatusCode(); // it is Sysnc Method
if(httpstatus==200){
some code will execute
}

if(gr2)
{
r= new sn_ws.RESTMessageV2(........);
..
..
response=r.executeAsync();  // it is Asysnc Method
responseBoby = response.getBody();
httpstatus = response.getStatusCode(); // it is Sysnc Method
if(httpstatus==200){
some code will execute
}

here at First -loop if(gr1) getStatusCode() making system to wait for httpcode this is Sync behavior and once code received, it's executes respective balance code and going to next loop if(gr2). until system get the HTTP code from first-loop system not jumping to next loop if(gr2). its wasting of huge time/ latency issues coming .

 

So How to over come this situation , how can we make system to do process the second loop also 


how to change API call's will go out  concurrently , Please share you inputs .


5 REPLIES 5

tharun_kumar_m
Mega Guru

Hi Supriya,

 

Please ensure that in your business rule, the "When to run" field is also async.

 

Could you try running that block of code at the end of the script?

 

Example: In GR1 condition after the line response=r.executeAsync();  you can add a flag variable (flagApi1=true)
In GR2 condition after the line response=r.executeAsync();  you can add a flag variable (flagApi2=true).

 

At the end of your script, you can do the below,

 

if(flag1){
responseBodyApi1 = responseApi1.getBody();
httpstatusApi1 = responseApi1.getStatusCode();
}
if(flag2){
responseBodyApi2 = responseApi2.getBody();
httpstatusApi2 = responseApi2.getStatusCode();
}

 

 

Please ensure all these variables are accessible within this script (declaring global in the script).

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumbs up.

 

Best regards,

Tharun Kumar

hi, Thanks for reply,

So this is the format you are suggesting ?

Field name : Requesting User input  
var gr1="";

var gr2="";

var r ="";

var flag1=false;

var flag2=false;

if(current .Requesting User input  =="VDI ")
{
 gr1 = true;
}else if(current .Requesting User input  =="NON VDI ")
{
 gr2 = true;
}else if(current .Requesting User input  ==" ")
{
 gr1 = true;
 gr2 = true;
}


if(gr1)
{
r= new sn_ws.RESTMessageV2(........);
response=r.executeAsync();  // it is Asysnc Method
responseBoby = response.getBody();

flag1=true;

}

if(gr2)
{
r= new sn_ws.RESTMessageV2(........);
response=r.executeAsync();  // it is Asysnc Method
responseBoby = response.getBody();

flag2=true;

}

 

if(flag2){
httpstatus = response.getStatusCode(); // it is Sysnc Method
if(httpstatus==200){
some code will execute
}

}

 

if(flag1){
httpstatus = response.getStatusCode(); // it is Sysnc Method
if(httpstatus==200){
some code will execute
}
}

Hi Supriya,

 

Yes. A little tweak on the variables. Please find the below format.

 

 

 

Field name : Requesting User input  
var gr1="";
var gr2="";

var flag1=false;
var flag2=false;

var response1 = "";
var response2 = "";

if(current .Requesting User input  =="VDI ")
{
 gr1 = true;
}else if(current .Requesting User input  =="NON VDI ")
{
 gr2 = true;
}else if(current .Requesting User input  ==" ")
{
 gr1 = true;
 gr2 = true;
}

if(gr1)
{
r1= new sn_ws.RESTMessageV2(........);
response1=r1.executeAsync();  // it is Async Method
flag1=true;
}

if(gr2)
{
r2= new sn_ws.RESTMessageV2(........);
response2=r2.executeAsync();  // it is Async Method
flag2=true;

}

if(flag2){
responseBody2 = response2.getBody();
httpstats2 = response2.getStatusCode();  // it is Sync Method
if(httpstatus2==200){
some code will execute
}
}

if(flag1){
responseBody1 = response1.getBody();
httpstatus1 = response1.getStatusCode(); // it is Sync Method
if(httpstatus1==200){
some code will execute
}
}

 

 

 

  

If my answer has helped with your question, please mark my answer as accepted solution and give a thumbs up.

 

Best regards,

Tharun Kumar

Concern is about to avoid much latency as much as possible in code execution . Can we avoid much latency form the above proposal  and execute code 

if this above proposal method  works smart way, thanks for that ,  But  Kindly let me know is there any other smartest way to trigger more-than 2 REST API calls at a time ( parallelly/Concurrently )  .