Google Cloud Pub/Sub にメッセージをパブリッシュする

このクイックスタートでは、Pub/Sub API を使用して「Hello World!」メッセージを Pub/Sub トピックにパブリッシュするプログラムの作成方法について説明します。

始める前に

このクイックスタートを実行する前に、自身または管理者によって次の事前準備が完了していることを確認してください。

  • サービス アカウントに IAM ロール roles/pubsub.publisher を付与する。

  • Google Cloud プロジェクトで Pub/Sub API が有効になっていることを確認する。

    API ライブラリに移動

  • Pub/Sub トピック SAMPLE_TOPIC_01 を作成し、pull サブスクリプション SAMPLE_SUB_TOPIC_01 を追加する。詳細については、トピックの作成サブスクリプションを作成するをご覧ください。

Google Cloud にメッセージをパブリッシュするプログラムを作成する

  1. SAP システムで、トランザクション SE38 を使用して、カスタム名前空間(Z や Y など)に実行可能プログラムを作成します。

    1. SAP GUI で、トランザクション コード SE38 を入力します。

    2. [プログラム] フィールドに、プログラムの名前を入力します(例: ZDEMO_PUBSUB)。

    3. [作成] をクリックします。

    4. プログラムの属性を指定します。

      1. [Title] フィールドに、プログラムのタイトル(例: Publish messages to a Pub/Sub topic)を入力します。

      2. [Type] フィールドで Executable Program を選択します。

      3. [Save] をクリックします。

    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!」メッセージがトピックにパブリッシュされているかどうかを確認します。

次のステップ