+ Start a Discussion
Tena WolverTena Wolver 

Asynchronous web service calls and wait for response

I would like some advice.  I have a web service I need to get data from.  I am limited how much data I can get each time by date range so I call it 6 times.  Right now I am using synchronous process.  I call it and wait and then call again.  I have to get all the data back before I can move on because with the data I need to do some manipulation.

It is my understanding if I do the @future I can't wait for the response.  Is that right? 

Any suggestions on what to do?  I have to wait for the response and if any of them fail then it all fails. 

Thanks in advance
 
James LoghryJames Loghry
When you invoke a method from Apex, it's synchronous by nature.  For instance, it might take a bit of time, but given your date range limitation, I could loop through and synchronously get all 12 months of information back as an example.

Future works similarly, but it's detached from whatever kicks it off.  For instance, once I kick off an @future call, it spawns a thread of sorts that is non-blocking to the rest of the apex class.  However, anything inside that call will be synchronous.  So if you kick off a future call with 12 callouts to your webservice, the future call might take some time to process, but your Apex class should execute quickly.

In your case it's probably a balance.  For instance, how much can you get away with asynchronously to reduce impact on performance, versus what's necessary from a synchronous construct.  (For instance, the callouts that depend on each other will have to be synchronous).  

Future callouts are also tricky from a limitation perspective, so be weary of stuff like DML, nesting future calls, kicking off too many future calls, etc.

Good luck.