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

このクイックスタートでは、ABAP SDK for Google Cloud の SAP BTP エディションで 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 を追加する。詳細については、トピックの作成サブスクリプションを作成するをご覧ください。

Pub/Sub トピックにメッセージをパブリッシュする ABAP クラスを作成する

  1. パッケージを作成します。

    1. ADT で、Project Explorer に移動します。
    2. パッケージ ZLOCAL を右クリックし、[新規] > [ABAP パッケージ] を選択します。
    3. パッケージの次の詳細情報を入力します。

      • 名前: 「ZABAPSDK_TEST」と入力します。
      • 説明: 「ABAP SDK Test Package」と入力します。
    4. [次へ] をクリックします。

    5. [トランスポート リクエストを選択] ダイアログで、[新しいリクエストを作成] チェックボックスをオンにします。

    6. トランスポート リクエストの説明を入力します。

    7. [完了] をクリックします。

  2. Pub/Sub API を呼び出す ABAP クラスを作成します。

    1. ABAP パッケージを右クリックし、[新規] > [ABAP クラス] を選択します。
    2. ABAP クラスの次の詳細情報を入力します。

      • 名前: 「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
            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
            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).
            MESSAGE lo_exception->get_text( ) TYPE 'E'.
        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!」メッセージがトピックにパブリッシュされているかどうかを確認します。

次のステップ