Newer Version Available
Making Multiple Asynchronous Callouts
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. The Visualforce page is shown first. The Visualforce page contains a button that invokes the action method startRequestsInParallel in the controller. When the Visualforce process resumes, the outputPanel component is rendered again. This panel displays the responses of the two asynchronous callouts.
1<apex:page controller="MultipleCalloutController" showChat="false" showHeader="false">
2 <apex:form >
3 <!-- Invokes the action method when the user clicks this button. -->
4 <apex:commandButton action="{!startRequestsInParallel}" value="Start Request" reRender="panel"/>
5 </apex:form>
6
7 <apex:outputPanel id="panel">
8 <!-- Displays the response body of the initial callout. -->
9 <apex:outputText value="{!result1}" />
10
11 <br/>
12 <!-- Displays the response body of the chained callout. -->
13 <apex:outputText value="{!result2}" />
14 </apex:outputPanel>
15
16</apex:page>This example shows the controller class for the Visualforce page. The startRequestsInParallel method adds two requests to the Continuation. After all callout responses are returned, the callback method (processAllResponses) is invoked and processes the responses.
1public with sharing class MultipleCalloutController {
2
3 // Unique label for the first request
4 public String requestLabel1;
5 // Unique label for the second request
6 public String requestLabel2;
7 // Result of first callout
8 public String result1 {get;set;}
9 // Result of second callout
10 public String result2 {get;set;}
11 // Endpoints of long-running service
12 private static final String LONG_RUNNING_SERVICE_URL1 =
13 '<Insert your first service URL>';
14 private static final String LONG_RUNNING_SERVICE_URL2 =
15 '<Insert your second service URL>';
16
17 // Action method
18 public Object startRequestsInParallel() {
19 // Create continuation with a timeout
20 Continuation con = new Continuation(60);
21 // Set callback method
22 con.continuationMethod='processAllResponses';
23
24 // Create first callout request
25 HttpRequest req1 = new HttpRequest();
26 req1.setMethod('GET');
27 req1.setEndpoint(LONG_RUNNING_SERVICE_URL1);
28
29 // Add first callout request to continuation
30 this.requestLabel1 = con.addHttpRequest(req1);
31
32 // Create second callout request
33 HttpRequest req2 = new HttpRequest();
34 req2.setMethod('GET');
35 req2.setEndpoint(LONG_RUNNING_SERVICE_URL2);
36
37 // Add second callout request to continuation
38 this.requestLabel2 = con.addHttpRequest(req2);
39
40 // Return the continuation
41 return con;
42 }
43
44 // Callback method.
45 // Invoked only when responses of all callouts are returned.
46 public Object processAllResponses() {
47 // Get the response of the first request
48 HttpResponse response1 = Continuation.getResponse(this.requestLabel1);
49 this.result1 = response1.getBody();
50
51 // Get the response of the second request
52 HttpResponse response2 = Continuation.getResponse(this.requestLabel2);
53 this.result2 = response2.getBody();
54
55 // Return null to re-render the original Visualforce page
56 return null;
57 }
58}