Enterprises running SAP often need to send changes in SAP business data to external systems. For example, when a material description is changed in SAP, an external system may want the updated description to change a label. In SAP terminology, these are called “Business Events”.
Over the years, SAP has changed the way how Business Events are raised and how these events can be consumed.
Pub/Sub works as a messaging middleware for traditional service integration or a simple communication medium for modern microservices. ABAP SDK for Google Cloud provides client library classes that allow SAP applications to natively call over 55 Google APIs including Pub/Sub.
While an external system can use pull subscription to read messages published to a Pub/Sub topic, a push subscription can deliver events to serverless webhooks on Cloud Functions, App Engine, Cloud Run, or Application Integration. This versatility makes Pub/Sub an ideal choice for sending SAP Business Events to external systems.
In this blog post, I will show how you can send a RAP Business Event to a Pub/Sub topic.
Let’s first understand how RAP Business Events work and how these events can be handled by RAP event handler class.
Using demo CDS entity demo_rap_event_m (available in newer S/4HANA systems) as an example:
Business Events can be handled in a RAP Event handler class. For example, class cl_demo_rap_event_handler handles events of entity demo_rap_event_m
CLASS cl_demo_rap_event_handler
DEFINITION PUBLIC ABSTRACT FINAL
FOR EVENTS OF demo_rap_event_m.
...
ENDCLASS.
The implementation of the event handler methods is done in CCIMP include by defining a local class that inherits from CL_ABAP_BEHAVIOR_EVENT_HANDLER.
CLASS lhe_event DEFINITION INHERITING FROM cl_abap_behavior_event_handler.
PRIVATE SECTION.
METHODS on_created FOR ENTITY EVENT created FOR demo_rap_event_m~created.
....
ENDCLASS.
You can run class cl_demo_rap_events to see the event handler in action.
Key Notes / Observations:
Now that we know how to create handlers for RAP events, let’s create one to send the event information to a Pub/Sub topic using ABAP SDK.
If you are new to ABAP SDK for Google Cloud, please use public documentation links to set up ABAP SDK and Pub/Sub resources. We will use the following values to publish messages to Pub/Sub topic:
Lets create an event handler class zcl_event_handler_pubsub for the CDS entity demo_rap_event_m
CLASS zcl_event_handler_pubsub
DEFINITION PUBLIC ABSTRACT FINAL
FOR EVENTS OF demo_rap_event_m.
...
ENDCLASS.
In the local types (CCIMP include) of zcl_event_handler_pubsub, add the below logic to publish the message to Pub/Sub topic using ABAP SDK for Google Cloud.
CLASS lhe_event DEFINITION INHERITING FROM cl_abap_behavior_event_handler.
PRIVATE SECTION.
METHODS on_created FOR ENTITY EVENT
created FOR DEMO_RAP_EVENT_M~created.
ENDCLASS.
CLASS lhe_event IMPLEMENTATION.
METHOD on_created.
TRY.
DATA(lo_pubsub) = NEW /goog/cl_pubsub_v1( iv_key_name = 'PUBSUB' ).
DATA(ls_input) = VALUE /goog/cl_pubsub_v1=>ty_023( ).
LOOP at created ASSIGNING FIELD-SYMBOL(<ls_created>).
APPEND VALUE #(
data = cl_web_http_utility=>encode_base64(
/ui2/cl_json=>serialize( data = <ls_created> ) ) )
to ls_input-messages.
ENDLOOP.
lo_pubsub->publish_topics(
EXPORTING
iv_p_projects_id = CONV string( lo_pubsub->gv_project_id )
iv_p_topics_id = 'SAP_DEMO_RAP_EVENT'
is_input = ls_input
IMPORTING
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(ls_err_resp) ).
CATCH /goog/cx_sdk INTO data(lo_sdk_error).
ENDTRY.
"Add error handling logic
ENDMETHOD.
ENDCLASS.
By running class CL_DEMO_RAP_EVENTS, our event handler class zcl_event_handler_pubsub is triggered and the message is published to the Pub/Sub topic
These messages can now be consumed by external systems using pull / push subscriptions.
As SAP is adopting RAP for all newer applications built on S/4HANA On-Prem and Public Cloud, the above approach can be used to stream SAP business events to external applications.
You can find standard RAP Events by looking at the behavior definitions in ADT. You can search behavior definitions using the Property Filter type:bdef in the ADT search bar.
When you open the behavior definition, you can find the events which can be handled using the event handler.
What if a standard behavior does not have an event? Behavior Extensions to the rescue!
As long as the standard behavior definition is marked as extensible, you can :
As you can see, by using RAP Business Events along with ABAP SDK for Google Cloud & Pub/Sub, you can build modern, scalable event-driven integration between SAP and external systems.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 |