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.
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.
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.
gcloud pubsub topics create SAMPLE_TOPIC
gcloud pubsub subscriptions create SAMPLE_SUBSCRIPTION - topic=SAMPLE_TOPIC
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"
Logon to your SAP system and follow the below steps to create the subscriber program.
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.
This code is a simple example of how to receive events from Pub/Sub into SAP using the ABAP SDK. It does the following:
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.
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.
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:
Happy Learning! Happy Innovating!