Salesforce LWC學習(十四) Continuation進行異步callout獲取數據


本篇參考:

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_continuations

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Continuation.htm#apex_class_System_Continuation

 我們在項目中經常遇到會和后台apex進行交互通過SOQL/SOSL去獲取數據展示在前台。當然,有些場景下數據是存儲在外部系統,需要apex進行callout操作去獲取數據展示前端。lwc針對callout操作可以簡單的分成幾步走,我們這里以

一. Enable Remote Site

 針對外部系統的交互,我們第一步就是要先在salesforce系統中配置Remote Site,才可以訪問,否則會報錯。我們以https://th-apex-http-callout.herokuapp.com/這個trailhead提供的callout URL作為 remote site 的配置,這個URL返回的值為: {"trailhead":"is awesome."}

 二. 前后台構建

我們以前做callout通常通過HttpRequest,然后將設置對應的header, url , body等以后然后Http.sendRequest即可實現外部系統callout交互。在lwc中,我們需要使用 Continuation這個salesforce提供的類進行交互,具體使用和文檔可以查看最上方的鏈接。我們在lwc和apex交互需要設置 @AuraEnabled=true,這個同樣需要,在這個基礎上,需要設置continuation=true,如果請求數據是固定的,可以也設置cacheable=true從而增加效率,都聲明情況下寫法如下

@AuraEnabled(continuation=true cacheable=true)

除了這里的小變動,另外的改變就是不用Http.sendRequest方式來構建,而是使用 Continuation方式來構建。Continuation構造函數只有一個參數,用來設置time out時間,以秒為單位。他有幾個參數,continuationMethod用來設置訪問以后對應的回調函數。timeout用來設置超時時間,最多120秒。state設置用來當callout操作完成並且callback方法執行完成以后的狀態值。我們可以用這個狀態值來確定當前的callout操作是否執行完成。Continuation有三個常用的方法:

addHttpRequest/getRequests()/getResponse()這三個方法的詳情描述自行查看上方的API文檔。

ContinuationDemoController類描述如下:聲明startRequest方法用來callout指定的service URL,然后將response放在callback函數中進行返回。

public with sharing class ContinuationDemoController {
    // Callout endpoint as a named credential URL
    // or, as shown here, as the long-running service URL
    private static final String LONG_RUNNING_SERVICE_URL =
        'https://th-apex-http-callout.herokuapp.com/';
    
    // Action method
    @AuraEnabled(continuation=true cacheable=true)
    public static Object startRequest() {
      // Create continuation. Argument is timeout in seconds.
      Continuation con = new Continuation(40);
      // Set callback method
      con.continuationMethod='processResponse';
      // Set state
      con.state='Hello, World!';
      // Create callout request
      HttpRequest req = new HttpRequest();
      req.setMethod('GET');
      req.setEndpoint(LONG_RUNNING_SERVICE_URL);
      // Add callout request to continuation
      con.addHttpRequest(req);
      // Return the continuation
      return con;
    }
    
    // Callback method
    @AuraEnabled(cacheable=true)
    public static Object processResponse(List<String> labels, Object state) {
      // Get the response by using the unique label
      HttpResponse response = Continuation.getResponse(labels[0]);
      // Set the result variable
      String result = response.getBody();
      return result;
    }
}
continuationCmp.html:用來展示從遠程服務器端的內容
<template> 
    <div>
        service content: {formattedWireResult}
    </div>    
</template>

continuationCmp.js:寫法上和訪問apex class方法沒有任何不同

import { LightningElement,wire } from 'lwc';
import startRequest from '@salesforce/apexContinuation/ContinuationDemoController.startRequest';
export default class ContinuationComponent extends LightningElement {
    
    // Using wire service
    @wire(startRequest)
    wiredContinuation;

    get formattedWireResult() {
        return JSON.stringify(this.wiredContinuation);
    }

}

結果:將遠程服務器內容轉換成JSON字符串

 總結:篇中只是簡單介紹了Continuation的介紹,還有很多的細節的操作和限制沒有在本篇中說出,比如Continuation和DML操作前后關系等限制,相關的limitation等等。篇中有錯誤的地方歡迎指出,有不懂歡迎留言。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM