Newer Version Available
非同期コールアウトのチェーニング
コールアウトの順序が重要な場合、またはコールアウトが別のコールアウトの応答に基づいて実行される場合、コールアウト要求をチェーニングできます。コールアウトをチェーニングすると、前のコールアウトから応答が返った後にのみ次のコールアウトが実行されます。たとえば、コールアウトのチェーニングが必要なケースとして、保証サービスから保証期限が切れたことを示す応答が返った後に保証延長情報を取得する場合などが考えられます。チェーニングできるのは最大 3 個のコールアウトです。
次の Visualforce および Apex の例は、コールアウトを別のコールアウトにチェーニングする方法を示します。Visualforce ページが最初に表示されます。Visualforce ページには、コントローラのアクションメソッド invokeInitialRequest を呼び出すボタンが含まれます。Visualforce プロセスは、継続が返されるたびに一時停止します。Visualforce プロセスは、各応答が返された後に再開し、outputPanel コンポーネントに各応答を表示します。
1<apex:page controller="ChainedContinuationController" showChat="false" showHeader="false">
2 <apex:form >
3 <!-- Invokes the action method when the user clicks this button. -->
4 <apex:commandButton action="{!invokeInitialRequest}" 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 ページのコントローラクラスを示します。invokeInitialRequest メソッドが最初の継続を作成します。コールバックメソッド (processInitialResponse) が最初のコールアウトの応答を処理します。応答が特定の条件に適合すると、メソッドは 2 つ目の継続を返して別のコールアウトをチェーニングします。チェーニングされた継続の応答が返されると、2 つ目のコールバックメソッド (processChainedResponse) が呼び出されて 2 つ目の応答を処理します。
1public with sharing class ChainedContinuationController {
2
3 // Unique label for the initial callout request
4 public String requestLabel1;
5 // Unique label for the chained callout request
6 public String requestLabel2;
7 // Result of initial callout
8 public String result1 {get;set;}
9 // Result of chained callout
10 public String result2 {get;set;}
11 // Endpoint 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 invokeInitialRequest() {
19 // Create continuation with a timeout
20 Continuation con = new Continuation(60);
21 // Set callback method
22 con.continuationMethod='processInitialResponse';
23
24 // Create first callout request
25 HttpRequest req = new HttpRequest();
26 req.setMethod('GET');
27 req.setEndpoint(LONG_RUNNING_SERVICE_URL1);
28
29 // Add initial callout request to continuation
30 this.requestLabel1 = con.addHttpRequest(req);
31
32 // Return the continuation
33 return con;
34 }
35
36 // Callback method for initial request
37 public Object processInitialResponse() {
38 // Get the response by using the unique label
39 HttpResponse response = Continuation.getResponse(this.requestLabel1);
40 // Set the result variable that is displayed on the Visualforce page
41 this.result1 = response.getBody();
42
43 Continuation chainedContinuation = null;
44 // Chain continuation if some condition is met
45 if (response.getBody().toLowerCase().contains('expired')) {
46 // Create a second continuation
47 chainedContinuation = new Continuation(60);
48 // Set callback method
49 chainedContinuation.continuationMethod='processChainedResponse';
50
51 // Create callout request
52 HttpRequest req = new HttpRequest();
53 req.setMethod('GET');
54 req.setEndpoint(LONG_RUNNING_SERVICE_URL2);
55
56 // Add callout request to continuation
57 this.requestLabel2 = chainedContinuation.addHttpRequest(req);
58 }
59
60 // Start another continuation
61 return chainedContinuation;
62 }
63
64 // Callback method for chained request
65 public Object processChainedResponse() {
66 // Get the response for the chained request
67 HttpResponse response = Continuation.getResponse(this.requestLabel2);
68 // Set the result variable that is displayed on the Visualforce page
69 this.result2 = response.getBody();
70
71 // Return null to re-render the original Visualforce page
72 return null;
73 }
74}