Consuming Events from Pub/Sub into SAP using ABAP SDK for Google Cloud

In today’s interconnected digital landscape, organizations are constantly seeking ways to seamlessly integrate data and events from disparate sources. This integration is crucial for enabling real-time decision-making, automating business processes, and maintaining a holistic view of operations. One of the key challenges in this endeavor is bridging the gap between cloud-based event streaming platforms like Google Cloud Pub/Sub and on-premises SAP systems.

 

The Need for Integration

SAP systems serve as the backbone of many enterprise operations, managing critical business data and processes. However, these systems often operate in silos, limiting their ability to react to real-time events generated outside of SAP. This disconnect can hinder businesses from responding swiftly to market shifts, customer behavior, or external triggers.

Google Cloud Pub/Sub, on the other hand, excels in real-time event streaming, enabling efficient data exchange between various systems and applications. By integrating Pub/Sub with SAP, organizations can harness the power of both platforms to achieve a unified event-driven architecture.

 

Realizing Pub/Sub-SAP Integration: A Hands-on Example using the ABAP SDK

Kriti3294_4-1701075618565.png

Let’s take a look at a simple example of how you can receive events/messages from Google Cloud Pub/Sub into SAP using the ABAP SDK.

Prerequisites:

  • Install ABAP SDK for Google Cloud [Detailed steps here]
  • Create a topic with a pull subscription in Pub/Sub. You can use the below command to create a sample topic and subscription using gcloud commands
gcloud pubsub topics create SAMPLE_TOPIC
gcloud pubsub subscriptions create SAMPLE_SUBSCRIPTION - topic=SAMPLE_TOPIC
  • Create a service account and provide the required roles to the service account: Pub/Sub Subscriber
gcloud iam service-accounts create myServiceAccount \
- description="Service Account to consume Pub/Sub messages" \
- display-name="Test Service Account"

gcloud projects add-iam-policy-binding [Project-ID] \
- member="serviceAccount:myServiceAccount@[Project-ID].iam.gserviceaccount.com" \
- role="roles/pubsub.subscriber" \
- condition="None"

Step-by-Step Integration: Receiving Pub/Sub Events in SAP

Logon to your SAP system and follow the below steps to create the subscriber program.

  1. Configure the client key: Create the below entry in table /GOOG/CLIENT_KEY using the SPRO Configuration Node “Configure Client Key”
Kriti3294_6-1701075728662.pngKriti3294_8-1701075808411.png
  1. Create a SE38 program to read messages from the topic using the subscription. Refer to this github repo for code sample.
REPORT z_sample_subscriber.

DATA:
lv_p_projects_id TYPE string,
lv_p_subscriptions_id TYPE string,
ls_input TYPE /goog/cl_pubsub_v1=>ty_026,
ls_input_ack TYPE /goog/cl_pubsub_v1=>ty_001.

TRY.

* Open HTTP Connection
DATA(lo_client) = NEW /goog/cl_pubsub_v1( iv_key_name = 'MY_CLIENT_KEY' ).

* Populate relevant parameters
" Derive project id from the client object
lv_p_projects_id = lo_client->gv_project_id.
" Name of the subscription from where we want to pull data
lv_p_subscriptions_id = 'SAMPLE_SUBSCRIPTION'.
" Max number of messages that will be received in 1 API call
ls_input-max_messages = 1.

* Call API method
CALL METHOD lo_client->pull_subscriptions
EXPORTING
iv_p_projects_id = lv_p_projects_id
iv_p_subscriptions_id = lv_p_subscriptions_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).

IF /goog/cl_http_client=>is_success( lv_ret_code ).
IF ls_output-received_messages IS NOT INITIAL.
"Messages published to Pub/Sub should be base-64 encoded, hence in order to get the exact message, we need to decode the data field.
"However, attributes published to Pub/Sub should be accessible without any additional logic.
DATA(lv_msg) = | Message Received: { cl_http_utility=>decode_base64( encoded = ls_output-received_messages[ 1 ]-message-data ) }|.
APPEND ls_output-received_messages[ 1 ]-ack_id TO ls_input_ack-ack_ids.

* Call API method: pubsub.projects.subscriptions.acknowledge
"Acknowledge the messages so it is not pulled again.
CALL METHOD lo_client->acknowledge_subscriptions
EXPORTING
iv_p_projects_id = lv_p_projects_id
iv_p_subscriptions_id = lv_p_subscriptions_id
is_input = ls_input_ack
IMPORTING
es_output = DATA(ls_output_ack)
ev_ret_code = lv_ret_code
ev_err_text = lv_err_text
es_err_resp = ls_err_resp.

IF lo_client->is_success( lv_ret_code ).
MESSAGE lv_msg TYPE 'S'.
ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.
ELSE.
MESSAGE 'No Messages were received!' TYPE 'S'.
ENDIF.
ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.

* Close HTTP Connection
lo_client->close( ).

CATCH /goog/cx_sdk INTO DATA(lo_exception).
MESSAGE lo_exception->get_text( ) TYPE 'E'.
ENDTRY.

About the code:

This code is a simple example of how to receive events from Pub/Sub into SAP using the ABAP SDK. It does the following:

  • Establish HTTP Connection: The code initiates by establishing an HTTP connection to the Pub/Sub API using the NEW keyword to create an instance of the /goog/cl_pubsub_v1 class. This class provides the necessary methods for interacting with the Pub/Sub API.
  • Gather Input Parameters: Before invoking the API methods, the code gathers relevant parameters, including the project ID, subscription ID, and maximum number of messages to retrieve. These parameters are essential for identifying the specific Pub/Sub subscription and controlling the data retrieval process.
  • Pull Messages from Subscription: The core of the integration lies in the CALL METHOD statement, which invokes the pull_subscriptions API method. This method retrieves messages from the specified subscription, returning a response object containing the received messages.
  • Process and Acknowledge Messages: For each received message, the code decodes the base64-encoded message data and extracts the message attributes. This ensures that the message content is accessible for further processing. Additionally, the code constructs an acknowledgment message containing the IDs of the received messages.
  • Acknowledge Message Receipt: To prevent messages from being pulled again, the code invokes the acknowledge_subscriptions API method, passing the acknowledgment message. This informs Pub/Sub that the messages have been successfully received and processed.
  • Handle Exceptions: Encapsulated within a TRY-CATCH block, the code gracefully handles any exceptions that may arise during the integration process. This ensures that the application remains stable and resilient in the face of unforeseen errors.

Putting It All Together: Witnessing Real-time Event Integration

To witness the power of real-time event integration, let’s put our theoretical knowledge into practice by sending a message to the SAMPLE_TOPIC in Pub/Sub and observing the response in our SAP program.

Step 1: Publish a Message to Pub/Sub

Open a terminal window and execute the following command, replacing SAMPLE_TOPIC with the actual name of your Pub/Sub topic:

gcloud pubsub topics publish SAMPLE_TOPIC - message=my-sample-message

Step 2: Execute the SAP Program

Open the SAP program that you developed earlier and execute it. The program should seamlessly receive the “my-sample-message” sent from Pub/Sub, demonstrating the successful integration between the two platforms.

Kriti3294_3-1701075539360.png

 

Conclusion: Unleashing the Potential of Real-time Data Integration

The integration of Pub/Sub events into SAP using the ABAP SDK has opened up a world of possibilities for organizations seeking to embrace real-time data integration. By bridging the gap between cloud-based event streaming and on-premises SAP systems, businesses can harness the power of real-time event data to enhance decision-making, streamline processes, and unlock new levels of efficiency.

With the ABAP SDK as your toolkit, you can seamlessly connect your SAP systems to the ever-flowing stream of real-time events, enabling your organization to stay ahead of the curve and thrive in today’s dynamic data-driven world.

 

Next Steps

Embrace the dynamic ABAP SDK for Google Cloud Community, a thriving ecosystem where fellow ABAP developers leveraging Google Cloud converge to share knowledge, collaborate, and explore. Join us as we shape the future of ABAP SDK for Google Cloud and unleash a universe of possibilities.

Embark on your innovation journey with us:

Uncover insightful content on Medium: Follow the medium tag for ABAP SDK for Google Cloud

Happy Learning! Happy Innovating!

 

Contributors
Version history
Last update:
‎11-27-2023 01:07 AM
Updated by: