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을 만들고 풀 구독 SAMPLE_SUB_TOPIC_01을 동일한 주제에 추가합니다. 자세한 내용은 주제 만들기구독 만들기를 참조하세요.

Google Cloud에 메시지를 게시하는 프로그램 만들기

  1. SAP 시스템에서 SE38 트랜잭션을 사용하여 커스텀 네임스페이스(예: Z 또는 Y)에 실행 가능한 프로그램을 만듭니다.

    1. SAP GUI에서 트랜잭션 코드 SE38를 입력합니다.

    2. 프로그램 필드에 프로그램 이름을 입력합니다(예: ZDEMO_PUBSUB).

    3. 만들기를 클릭합니다.

    4. 프로그램 속성을 지정합니다.

      1. 제목 필드에 프로그램 제목을 입력합니다(예: Publish messages to a Pub/Sub topic).

      2. 유형 필드에서 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. 가져오기 기능을 사용하여 'Hello World!' 메시지가 주제에 게시되었는지 확인합니다.

다음 단계