将消息发布到 Google Cloud Pub/Sub

本快速入门介绍了如何创建一个程序,以使用 Pub/Sub API 通过 SAP BTP 版本的 ABAP SDK for Google Cloud 将“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,然后选择新建 > ABAP 软件包
    3. 为软件包输入以下详细信息:

      • 名称:输入 ZABAPSDK_TEST
      • 说明:输入 ABAP SDK Test Package
    4. 点击下一步

    5. 选择传输请求对话框中,选中创建新请求复选框。

    6. 输入传输请求的说明。

    7. 点击完成

  2. 创建 ABAP 类以调用 Pub/Sub API:

    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. 使用拉取功能检查“Hello World!”消息是否已发布到主题。

后续步骤