This series of blogs intends to show in a step-by-step way the different possibilities to use RAP Business Events with Advanced Event Mesh.
Overview
In this fifth blog, we’re going to look at consuming RAP business events locally. Specifically, we’ll be creating a local event handler for the Business Partner Changed event. While the topic of this blog is completely independent of Advanced Event Mesh, I still included it to give a complete overview of RAP business events.
Note: This blog focuses on S/4HANA On Premise 2023. With this release many events are only accessible through non-released interfaces. As such the ABAP language version “ABAP for Cloud Development” can not be used for the content of this blog.
Consuming a local business event
To consume a local event only an event handler class is required. As the event is only accessible through the non-released interface (I_BusinessPartnerTP_2) you’ll need to use the ABAP language version “Standard ABAP”. For that create a new package and select “HOME” as software component.
Within this package create a new ABAP class. Right click the newly created package and select “New” > “ABAP Class”. I’ve named mine “Z_LOCAL_BP_EVENT_CONSUMPTION”. Change the class definition from “DEFINITION PUBLIC FINAL CREATE PUBLIC” to “DEFINITION PUBLIC ABSTRACT FINAL FOR EVENTS OF I_BUSINESSPARTNERTP_2”.
CLASS z_local_bp_event_consumption DEFINITION PUBLIC ABSTRACT FINAL FOR EVENTS OF I_BusinessPartnertp_2 .
PUBLIC SECTION.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS z_local_bp_event_consumption IMPLEMENTATION.
ENDCLASS.
In the local types add a class that inherits from “cl_abap_behavior_event_handler”and a method for each event that should be consumed. One ABAP class can be used to create event handlers for all events that are defined in one behavior definition. As “I_BusienssPartnerTP_2” not only holds the “Changed” event, but also the “Created” event two different methods within the same ABAP class can be used for the respective events. For now, let’s just add a new method for the “BusinessPartner~Changed” event. This method can be defined as follows:
METHODS consume_changed FOR ENTITY EVENT changed_instances FOR BusinessPartner~Changed.
Within the method implementation loop over “changed_instances” to react to each event. The complete code should then look something like this:
CLASS lcl_abap_behv_event_handler DEFINITION INHERITING FROM cl_abap_behavior_event_handler.
PRIVATE SECTION.
METHODS consume_changed FOR ENTITY EVENT changed_instances FOR BusinessPartner~Changed.
ENDCLASS.
CLASS lcl_abap_behv_event_handler IMPLEMENTATION.
METHOD consume_changed.
LOOP AT changed_instances INTO DATA(changed_instance).
cl_demo_output=>write( ‘Event consumed’ ).
ENDLOOP.
ENDMETHOD.
ENDCLASS.
After activating this class, the defined method is executed every time a Business Partner Changed event is triggered. To test this behavior set a break point in the method and go into transaction BP. Once there select a business partner and edit it. After saving the business partner the program should pause at the breakpoint indicating that it is being executed.
This series of blogs intends to show in a step-by-step way the different possibilities to use RAP Business Events with Advanced Event Mesh. OverviewIn this fifth blog, we’re going to look at consuming RAP business events locally. Specifically, we’ll be creating a local event handler for the Business Partner Changed event. While the topic of this blog is completely independent of Advanced Event Mesh, I still included it to give a complete overview of RAP business events.Note: This blog focuses on S/4HANA On Premise 2023. With this release many events are only accessible through non-released interfaces. As such the ABAP language version “ABAP for Cloud Development” can not be used for the content of this blog.Consuming a local business eventTo consume a local event only an event handler class is required. As the event is only accessible through the non-released interface (I_BusinessPartnerTP_2) you’ll need to use the ABAP language version “Standard ABAP”. For that create a new package and select “HOME” as software component.Within this package create a new ABAP class. Right click the newly created package and select “New” > “ABAP Class”. I’ve named mine “Z_LOCAL_BP_EVENT_CONSUMPTION”. Change the class definition from “DEFINITION PUBLIC FINAL CREATE PUBLIC” to “DEFINITION PUBLIC ABSTRACT FINAL FOR EVENTS OF I_BUSINESSPARTNERTP_2”. CLASS z_local_bp_event_consumption DEFINITION PUBLIC ABSTRACT FINAL FOR EVENTS OF I_BusinessPartnertp_2 .
PUBLIC SECTION.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS z_local_bp_event_consumption IMPLEMENTATION.
ENDCLASS. In the local types add a class that inherits from “cl_abap_behavior_event_handler”and a method for each event that should be consumed. One ABAP class can be used to create event handlers for all events that are defined in one behavior definition. As “I_BusienssPartnerTP_2” not only holds the “Changed” event, but also the “Created” event two different methods within the same ABAP class can be used for the respective events. For now, let’s just add a new method for the “BusinessPartner~Changed” event. This method can be defined as follows:METHODS consume_changed FOR ENTITY EVENT changed_instances FOR BusinessPartner~Changed.Within the method implementation loop over “changed_instances” to react to each event. The complete code should then look something like this: CLASS lcl_abap_behv_event_handler DEFINITION INHERITING FROM cl_abap_behavior_event_handler.
PRIVATE SECTION.
METHODS consume_changed FOR ENTITY EVENT changed_instances FOR BusinessPartner~Changed.
ENDCLASS.
CLASS lcl_abap_behv_event_handler IMPLEMENTATION.
METHOD consume_changed.
LOOP AT changed_instances INTO DATA(changed_instance).
cl_demo_output=>write( ‘Event consumed’ ).
ENDLOOP.
ENDMETHOD.
ENDCLASS. After activating this class, the defined method is executed every time a Business Partner Changed event is triggered. To test this behavior set a break point in the method and go into transaction BP. Once there select a business partner and edit it. After saving the business partner the program should pause at the breakpoint indicating that it is being executed. Read More Technology Blogs by SAP articles
#SAP
#SAPTechnologyblog