將訊息發布至 Google Cloud Pub/Sub

本快速入門課程將說明如何透過 SAP BTP 版的 ABAP SDK for Google Cloud,使用 Pub/Sub API 建立程式,將「Hello World!」訊息發布至 Pub/Sub 主題。

事前準備

執行本快速入門前,請確認您或管理員已完成下列先決條件:

  • 將 IAM 角色 roles/pubsub.publisher 授予服務帳戶。

  • 請確認您已在 Google Cloud 專案中啟用 Pub/Sub API。

    前往 API 程式庫

  • 建立 Pub/Sub 主題 SAMPLE_TOPIC_01,並新增拉取訂閱項目 SAMPLE_SUB_TOPIC_01。詳情請參閱「建立主題」和「建立訂閱項目」。

建立 ABAP 類別,將訊息發布至 Pub/Sub 主題

  1. 建立套件:

    1. 在 ADT 中前往「Project Explorer」。
    2. ZLOCAL 套件上按一下滑鼠右鍵,然後依序選取「New」>「ABAP Package」
    3. 請輸入下列包裹詳細資料:

      • 「Name」:輸入 ZABAPSDK_TEST
      • 說明:輸入 ABAP SDK Test Package
    4. 點選「下一步」

    5. 在「Select a Transport Request」對話方塊中,選取「Create a new request」核取方塊。

    6. 輸入傳輸要求的說明。

    7. 按一下「完成」

  2. 建立 ABAP 類別來呼叫 Pub/Sub API:

    1. 在 ABAP 套件上按一下滑鼠右鍵,然後依序選取「New」>「ABAP Class」
    2. 請為 ABAP 類別輸入下列詳細資料:

      • 「Name」:輸入 ZGOOG_CL_QS_PUBSUB
      • 說明:輸入 Quick start for Pub/Sub API
    3. 點選「下一步」

    4. 選取運送要求,然後按一下「完成」

  3. 在程式碼編輯器中,將預設程式碼替換為以下程式碼片段:

    " --------------------------------------------------------------------
    "  Copyright 2024 Google LLC                                         -
    "                                                                    -
    "  Licensed under the Apache License, Version 2.0 (the "License");   -
    "  you may not use this file except in compliance with the License.  -
    "  You may obtain a copy of the License at                           -
    "      https://www.apache.org/licenses/LICENSE-2.0                   -
    "  Unless required by applicable law or agreed to in writing,        -
    "  software distributed under the License is distributed on an       -
    "  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,      -
    "  either express or implied.                                        -
    "  See the License for the specific language governing permissions   -
    "  and limitations under the License.                                -
    " --------------------------------------------------------------------
    CLASS zcl_qs_publish_messages DEFINITION
      PUBLIC FINAL
      CREATE PUBLIC.
    
      PUBLIC SECTION.
        INTERFACES if_oo_adt_classrun.
    ENDCLASS.
    
    
    
    CLASS ZCL_QS_PUBLISH_MESSAGES IMPLEMENTATION.
    
    
      METHOD if_oo_adt_classrun~main.
    
        DATA ls_input         TYPE /goog/cl_pubsub_v1=>ty_023.
        DATA lo_pubsub        TYPE REF TO /goog/cl_pubsub_v1.
        DATA lv_p_projects_id TYPE string.
        DATA lv_p_topics_id   TYPE string.
    
        TRY.
            " Open HTTP connection
            " The client key DEMO_PUBSUB is an example, replace this with actual value
            lo_pubsub = NEW /goog/cl_pubsub_v1( iv_key_name = 'DEMO_PUBSUB' ).
    
            " Pass the relevant input parameters
            " The value shown below is an example. Replace it with actual value
            lv_p_topics_id = 'SAMPLE_TOPIC_01'.
    
            lv_p_projects_id = lo_pubsub->gv_project_id.
            APPEND VALUE #( data = cl_http_utility=>encode_base64( 'Hello World!' ) )
                   TO ls_input-messages.
    
            " Call the API
            lo_pubsub->publish_topics( EXPORTING iv_p_projects_id = lv_p_projects_id
                                                 iv_p_topics_id   = lv_p_topics_id
                                                 is_input         = ls_input
                                       IMPORTING
                                                 es_output        = DATA(ls_output)
                                                 ev_ret_code      = DATA(lv_ret_code)
                                                 ev_err_text      = DATA(lv_err_text)
                                                 es_err_resp      = DATA(ls_err_resp) ).
    
            " Handle the output
            IF lo_pubsub->is_success( lv_ret_code ).
              out->write( 'Message was published!' ).
            ELSE.
              out->write( 'Message was not published!' ).
            ENDIF.
    
            " Close the HTTP Connection
            lo_pubsub->close( ).
    
          CATCH /goog/cx_sdk INTO DATA(lo_exception).
            " Handle exception here
        ENDTRY.
      ENDMETHOD.
    ENDCLASS.
    

    DEMO_PUBSUB 替換為用戶端金鑰名稱。

  4. 儲存並啟用變更。

  5. 執行應用程式:

    1. 選取 ABAP 類別 ZGOOG_CL_QS_PUBSUB
    2. 依序點選「執行」>「以身分執行」>「ABAP 應用程式 (控制台)」。或者按下 F9
  6. 如要驗證結果,請按照下列步驟操作:

    1. 在 Google Cloud 控制台中,前往「Pub/Sub」

    2. 選取訂閱項目 SAMPLE_SUB_TOPIC_01,然後前往「訊息」分頁。

    3. 使用 PULL 功能,確認「Hello World!」訊息是否已發布至主題。

後續步驟