この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

複数の非同期コールアウトの実行

長時間のサービスに対して Visualforce ページから複数のコールアウトを同時に実行する場合、最大 3 つの要求を継続インスタンスに追加できます。たとえば、2 つの商品に関する在庫統計を取得するなど、サービスに対して独立した要求を実行するときに同時コールアウトを実行することがあります。

同じ継続内で複数のコールアウトを実行すると、コールアウト要求は並列に実行され、Visualforce 要求は一時停止されます。すべてのコールアウト応答が返された後にのみ、Visualforce プロセスは再開します。

次の Visualforce および Apex の例は、1 つの継続を使用して 2 つの非同期コールアウトを同時に実行する方法を示します。Visualforce ページが最初に表示されます。Visualforce ページには、コントローラのアクションメソッド startRequestsInParallel を呼び出すボタンが含まれます。Visualforce プロセスが再開すると、outputPanel コンポーネントが再度表示されます。次のパネルには、2 つの非同期コールアウトの応答が表示されます。

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>

次の例は、Visualforce ページのコントローラクラスを示します。startRequestsInParallel メソッドは 2 つの要求を継続に追加します。すべてのコールアウト応答が返された後、コールバックメソッド (processAllResponses) が呼び出されて応答を処理します。

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}