將訊息發布至 Google Cloud Pub/Sub

本快速入門說明如何使用 Pub/Sub API 建立程式,將「Hello World!」訊息發布至 Pub/Sub 主題。

事前準備

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

  • 已設定驗證方法,以便存取 Google Cloud API。瞭解如何設定驗證

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

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

    前往 API 程式庫

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

建立用來將訊息發布至 Google Cloud的程式

  1. 在 SAP 系統中,使用交易 SE38 在自訂命名空間 (例如 Z 或 Y) 中建立可執行的程式。

    1. 在 SAP GUI 中輸入交易代碼 SE38

    2. 在「Program」欄位中輸入計畫名稱,例如 ZDEMO_PUBSUB

    3. 按一下 [建立]。

    4. 指定節目屬性:

      1. 在「Title」欄位中,輸入節目名稱,例如 Publish messages to a Pub/Sub topic

      2. 在「Type」欄位中,選擇「Executable Program」。

      3. 按一下 [儲存]

    5. 將程式儲存為本機物件

    6. ABAP 編輯器中,新增下列程式碼:

      **********************************************************************
      *  Copyright 2023 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.                                *
      **********************************************************************
      
      REPORT zr_qs_publish_messages.
      
      * Data Declaration
      DATA:
        lv_p_projects_id TYPE string,
        lv_p_topics_id   TYPE string,
        ls_input         TYPE /goog/cl_pubsub_v1=>ty_023.
      
      TRY.
      * Instantiate the client stub
          DATA(lo_pubsub) = NEW /goog/cl_pubsub_v1( iv_key_name = 'DEMO_PUBSUB' ).
      
      * Pass the relevant input parameters
          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
          CALL METHOD 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 ).
            MESSAGE 'Message was published!' TYPE 'S'.
          ELSE.
            MESSAGE 'Message was not published!' TYPE 'E'.
          ENDIF.
      
      * Close the HTTP Connection
          lo_pubsub->close( ).
      
        CATCH /goog/cx_sdk INTO DATA(lo_exception).
          MESSAGE lo_exception->get_text( ) TYPE 'E'.
      ENDTRY.
      

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

  2. SE38 中執行應用程式。

  3. 如要驗證結果,請按照下列步驟操作:

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

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

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

後續步驟