Publish messages to Google Cloud Pub/Sub

This quickstart shows you how to create a program that publishes a "Hello World!" message to a Pub/Sub topic by using the Pub/Sub API through the SAP BTP edition of ABAP SDK for Google Cloud.

Before you begin

Before you run this quickstart, make sure that you or your administrators have completed the following prerequisites:

  • Grant the service account the IAM role roles/pubsub.publisher.

  • Make sure the Pub/Sub API is enabled in your Google Cloud project.

    Go to API library

  • Create a Pub/Sub topic SAMPLE_TOPIC_01 and add a pull subscription SAMPLE_SUB_TOPIC_01 to the same. For more information, see Create a topic and Create a subscription.

Create an ABAP class to publish messages to a Pub/Sub topic

  1. Create a package:

    1. In ADT, go to the Project Explorer.
    2. Right-click the package ZLOCAL, and select New > ABAP Package.
    3. Enter the following details for your package:

      • Name: enter ZABAPSDK_TEST.
      • Description: enter ABAP SDK Test Package.
    4. Click Next.

    5. In the Select a Transport Request dialog, select the Create a new request checkbox.

    6. Enter a description for the transport request.

    7. Click Finish.

  2. Create an ABAP class to call the Pub/Sub API:

    1. Right-click your ABAP package and select New > ABAP Class.
    2. Enter the following details for your ABAP class:

      • Name: enter ZGOOG_CL_QS_PUBSUB.
      • Description: enter Quick start for Pub/Sub API.
    3. Click Next.

    4. Select a transport request and click Finish.

  3. In the code editor, replace the default code with the following code snippet:

    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.
    

    Replace DEMO_PUBSUB with the client key name.

  4. Save and activate the changes.

  5. Run your application:

    1. Select the ABAP class ZGOOG_CL_QS_PUBSUB.
    2. Click Run > Run As > ABAP Application (Console). Alternatively, press F9.
  6. To validate the results, follow these steps:

    1. In the Google Cloud console, go to Pub/Sub.

    2. Select the subscription SAMPLE_SUB_TOPIC_01 and go to the Messages tab.

    3. Use the PULL feature to check whether the "Hello World!" message has been published to the topic.

What's next