継続を使用した長時間コールアウトの実行
Apex で Continuation クラスを使用して、外部 Web サービスへの長時間要求を行います。コールバックメソッドで応答を処理します。継続を使用して実行される非同期コールアウトは、実行時間が 5 秒を超える要求を同時に実行できる数である Apex 制限の 10 件にはカウントされません。
Apex クラスでの継続の処理
Apex クラスで継続を処理するためのステップは次の通りです。
- 外部サービスをコールする前に、Salesforce ユーザインターフェース内の認証されたリモートサイトのリストにそのリモートサイトを追加する必要があります。[設定] から、[クイック検索] ボックスに「リモートサイトの設定」と入力します。[リモートサイトの設定] を選択して、[新規リモートサイト] をクリックします。次の Apex クラスの継続の例に示すように、LONG_RUNNING_SERVICE_URL に対応するコールアウトの URL を追加します。
コールアウトでエンドポイントとして指定ログイン情報を指定する場合、リモートサイト設定を定義する必要はありません。指定ログイン情報では、1 つの定義にコールアウトエンドポイントの URL と必要な認証パラメータを指定します。コードに、長時間サービスの URL ではなく、指定ログイン情報 URL を指定します。
- 長時間のコールアウトを実行するには、Continuation オブジェクトを返す Apex メソッドを定義します。(@AuraEnabled アノテーションの属性については後ほど説明します。)
1@AuraEnabled(continuation=true cacheable=true) 2public static Object startRequest() { 3 // Create continuation. Argument is timeout in seconds. 4 Continuation con = new Continuation(40); 5 // more to come here 6 return con; 7} - コールアウトの完了後に Apex コールバックメソッドが呼び出されるように Continuation オブジェクトの continuationMethod プロパティで設定します。この例のコールバックメソッドは processResponse です。コールバックメソッドは、同じ Apex クラスに属している必要があります。
1con.continuationMethod='processResponse'; -
HttpRequest オブジェクトを Continuation オブジェクトに追加して、コールアウトのエンドポイントを設定します。1 つの Continuation オブジェクトに最大 3 個のコールアウトを含めることができます。各コールアウトには、リモートサイトまたは指定ログイン情報が [設定] で定義されている必要があります。
1HttpRequest req = new HttpRequest(); 2req.setMethod('GET'); 3req.setEndpoint(LONG_RUNNING_SERVICE_URL); 4con.addHttpRequest(req); - コールバックメソッドに渡すデータを Continuation オブジェクトの state プロパティで設定します。state プロパティにはオブジェクト型があるため、Apex でサポートされているすべてのデータ型を渡すことができます。
1con.state='Hello, World!'; - Apex コールバックでロジックをコーディングします。Continuation オブジェクトに設定されているすべてのコールアウトが完了すると、Apex コールバックメソッドの processResponse が呼び出されます。このコールバックメソッドには 2 つのアクセス可能なパラメータがあります。
1public static Object processResponse(List<String> labels, Object state)- labels — 継続に含まれる各リクエストのラベルのリスト。ラベルは自動的に作成されます。
- state — Continuation オブジェクトの state プロパティで設定した状態。
- 継続に含まれる各要求に対する応答。次に例を示します。
1HttpResponse response = Continuation.getResponse(labels[0]); - 結果を JavaScript コントローラに返します。
継続を使用した完全な Apex クラスの例
上記のすべてのステップを 1 つにまとめた完全な Apex クラスの例を示します。
1public with sharing class SampleContinuationClass {
2 // Callout endpoint as a named credential URL
3 // or, as shown here, as the long-running service URL
4 private static final String LONG_RUNNING_SERVICE_URL =
5 '<insert your callout URL here>';
6
7 // Action method
8 @AuraEnabled(continuation=true cacheable=true)
9 public static Object startRequest() {
10 // Create continuation. Argument is timeout in seconds.
11 Continuation con = new Continuation(40);
12 // Set callback method
13 con.continuationMethod='processResponse';
14 // Set state
15 con.state='Hello, World!';
16 // Create callout request
17 HttpRequest req = new HttpRequest();
18 req.setMethod('GET');
19 req.setEndpoint(LONG_RUNNING_SERVICE_URL);
20 // Add callout request to continuation
21 con.addHttpRequest(req);
22 // Return the continuation
23 return con;
24 }
25
26 // Callback method
27 @AuraEnabled(cacheable=true)
28 public static Object processResponse(List<String> labels, Object state) {
29 // Get the response by using the unique label
30 HttpResponse response = Continuation.getResponse(labels[0]);
31 // Set the result variable
32 String result = response.getBody();
33 return result;
34 }
35}