Continuation Multiple Asynchronous Callouts
To make multiple callouts to a long-running service simultaneously from a Visualforce page, you can add up to three requests to the Continuation instance. An example of when to make simultaneous callouts is when you’re making independent requests to a service, such as getting inventory statistics for two products.When you’re making multiple callouts in the same continuation, the callout requests run in parallel and suspend the Visualforce request. Only after all callout responses are returned does the Visualforce process resume.The following Visualforce and Apex examples show how to make two asynchronous callouts simultaneously by using a single continuation.
public class MultipleContinutationController { // Unique label for the first request public String requestLabel1; // Unique label for the second request public String requestLabel2; // Result of first callout public String result1 {get;set;} // Result of second callout public String result2 {get;set;} // Endpoints of long-running service private static final String LONG_RUNNING_SERVICE_URL1 = 'http://inqstatsapi.inqubu.com?api_key=27b3c066fcedd91a&data=population&countries=us'; private static final String LONG_RUNNING_SERVICE_URL2 = 'http://inqstatsapi.inqubu.com?api_key=27b3c066fcedd91a&data=population&countries=gb'; public List<JSON2Apex> jsonApexURL1 {get;set;} public List<JSON2Apex> jsonApexURL2 {get;set;} // Action method public Object startRequestsInParallel() { // Create continuation with a timeout Continuation con = new Continuation(60); // Set callback method con.continuationMethod='processAllResponses'; // Create first callout request HttpRequest req1 = new HttpRequest(); req1.setMethod('GET'); req1.setEndpoint(LONG_RUNNING_SERVICE_URL1); // Add first callout request to continuation this.requestLabel1 = con.addHttpRequest(req1); // Create second callout request HttpRequest req2 = new HttpRequest(); req2.setMethod('GET'); req2.setEndpoint(LONG_RUNNING_SERVICE_URL2); // Add second callout request to continuation this.requestLabel2 = con.addHttpRequest(req2); // Return the continuation return con; } // Callback method. // Invoked only when responses of all callouts are returned. public Object processAllResponses() { // Get the response of the first request HttpResponse response1 = Continuation.getResponse(this.requestLabel1); this.result1 = response1.getBody(); jsonApexURL1 = (List<JSON2Apex>) System.JSON.deserialize(response1.getBody(), List<JSON2Apex>.class); // Get the response of the second request HttpResponse response2 = Continuation.getResponse(this.requestLabel2); this.result2 = response2.getBody(); jsonApexURL2 = (List<JSON2Apex>) System.JSON.deserialize(response2.getBody(), List<JSON2Apex>.class); // Return null to re-render the original Visualforce page return null; } public class JSON2Apex { public String countryCode{get;set;} public String countryName{get;set;} public List<Population> population{get;set;} } public class Population { public String year{get;set;} public String data{get;set;} } public static List<JSON2Apex> parse(String json) { return (List<JSON2Apex>) System.JSON.deserialize(json, List<JSON2Apex>.class); } }
<apex:page controller="MultipleContinutationController" showChat="false" showHeader="false"> <apex:form > <!-- Invokes the action method when the user clicks this button. --> <apex:commandButton action="{!startRequestsInParallel}" value="Start Request" reRender="panel"/> </apex:form> <apex:outputPanel id="panel"> <apex:pageBlock id="result" > <apex:pageBlockSection > Result 1 - {!jsonApexURL1} <br/> <br/> <br/> Result 2 - {!jsonApexURL2} </apex:pageBlockSection> </apex:pageBlock> </apex:outputPanel> </apex:page>