How to consume ABAP RAP Events : A Comprehensive Guide

0
396

ABAP RESTful Application Programming (RAP) is a powerful framework for building RESTful services in SAP. One key aspect of RAP is the ability to handle RAP events. By understanding how to work with RAP events, you can create dynamic and responsive applications. In this blog post I will provide examples to illustrate how to effectively consume ABAP RAP events in your custom program/RAP applications.

How to Consume ABAP RAP Events in SAP S/4HANA
How to Consume ABAP RAP Events in SAP S/4HANA

ABAP RAP Event

ABAP RAP supports event-driven architecture to support asynchronous messaging patterns. This enables decoupled communication between event publishers and subscribers.This approach ensures event consumers independently handle follow-up processes, enabling loosely coupled, asynchronous workflows.

RAP business events raised by RAP business objects can be consumed either locally or remotely. RAP Events can be consume in two ways:

1. Local Consumption

RAP Events generated within the same system can be processed locally through event handler classes. These classes enable the system to react to events in real-time. To be consumed locally, an event must be exposed in a C1 released RAP BO interface.

2. Remote Consumption

RAP Events generated within the source system can be consumed remotely using SAP Event Mesh. By subscribing to specific messaging queues, the target system can receive and process these events remotely.

In this blog post I will focus on local consumption of ABAP RAP Events within the same S/4HANA system.

Consume ABAP RAP Events Locally

RAP events can be consume locally within the system through an event handler class. The event handler class is global class with addition FOR EVENTS OF clause in the class definition. This class can then implement methods to handle specific events in the CCIMP include, which are executed asynchronously.

The FOR EVENTS OF addition ensures a clear and structured association between the event provider and the handling logic, making it easier to manage and extend event-driven processes in the RAP framework.

CLASS zcl_so_events_handler DEFINITION PUBLIC [ABSTRACT] [FINAL]
                            FOR EVENTS OF bdef.
  ...
ENDCLASS.

CLASS zcl_so_events_handler IMPLEMENTATION.
ENDCLASS.

The bdef specifies the RAP BO root entity’s name, where events must be defined. It can also refer to an interface bdef that exposes an event. This syntax explicitly binds the class to handle events triggered by a specific RAP business object.

Actual logic to handle RAP business events is available in the CCIMP include of the class. Below is the syntax for local class implemented in CCIMP include of the event handler class.

CLASS lhe_event DEFINITION
  INHERITING FROM CL_ABAP_BEHAVIOR_EVENT_HANDLER [ ABSTRACT] [FINAL].
  PRIVATE SECTION.
    METHODS <method_name> FOR ENTITY EVENT
       <event_name> FOR FOR bdef~evt.

ENDCLASS.

CLASS lhe_event IMPLEMENTATION

    METHOD <method_name>.
     "do something after event trigger
   ENDMETHOD.

ENDCLASS.

A local class that inherits from CL_ABAP_BEHAVIOR_EVENT_HANDLER implemented in the CCIMP include of a RAP event handler class to locally consume RAP business events. The events are handled by RAP event handler methods that are defined and implemented in this local class.

RAP Sales Order Event Consumption Demo

Now you know about event handler class and how to consume ABAP RAP events, below is the event handler class for Sales Order RAP events in SAP S/4HANA.

CLASS ycl_so_rap_events_handler DEFINITION 
      PUBLIC ABSTRACT FINAL FOR EVENTS OF R_SALESORDERTP.

  PUBLIC SECTION.
  PROTECTED SECTION.
  PRIVATE SECTION.

ENDCLASS.

CLASS ycl_so_rap_events_handler IMPLEMENTATION.

ENDCLASS.
CLASS lhe_event DEFINITION INHERITING FROM cl_abap_behavior_event_handler.
  PRIVATE SECTION.
    METHODS on_created FOR ENTITY EVENT
       created FOR SalesOrder~created.

    "Methods to add messages to the business application log
    METHODS log RETURNING VALUE(log) TYPE REF TO if_xco_cp_bal_log.

    METHODS add_message IMPORTING log TYPE REF TO if_xco_cp_bal_log
                                  message TYPE string.

    DATA: bal_log TYPE REF TO if_xco_cp_bal_log.
ENDCLASS.



CLASS lhe_event IMPLEMENTATION.
  "Note: For this example, messages are created for the individual RAP BO 
  "instances that are imported into the event handler methods.
  "These messages are added to the business application log to demonstrate 
  "that event handler methods are called after raising an event.
  METHOD on_created.

    bal_log = log( ).
    LOOP AT created ASSIGNING FIELD-SYMBOL(<created>).
      "Note: The event is specified with a parameter in the BDEF.
      add_message( 
         log     = bal_log
         message = |Key: "{ <created>-SalesOrder }" / SO Event CREATED handled| 
      ).
    ENDLOOP.
  ENDMETHOD.


  METHOD log
    log =  xco_cp_bal=>for->database( )->log->create(
      iv_object      = 'DEMO_LOG'
      iv_subobject   = 'DEMO_SUB'
      iv_external_id = 'SO'
    ).
  ENDMETHOD.

  METHOD add_message.
    log->add_text( xco_cp=>string( message ) ).
  ENDMETHOD.
ENDCLASS.

Create a sales order using Sales Order BDEF through ABAP EML or classic BAPI. The event handler class will trigger, and the output of SLG1 is shown below, where we logged the sales order number when the sales order event CREATED was triggered.

AD 4nXcar7JDk1Nbjay0G ZwvVqAt 1CU3hQs4tvsF6R 0Ff1hIwHbrGJJnIR4g5iC R6lUyA49QhnVYGOHXn45yTqdXtIN0XBi93i37WZt76mZPa6PrU9JD4ot2ii4l59eBWqwS2kNJ g?key=anj8O9gghk389ODoo2GrT O

Key points to remember

  • Form based editor(SE24) is not supported to create entity event handler class, use ADT.
  • There can be more than one event handler class for a RAP BO.
  • The event handler methods implemented in the CCIMP include of the event handler class are called asynchronously, through bgRFC.
  • Within an event handler class, an event can only be handled by a single method. However, handling the same event across multiple handler classes is possible.
  • A single RAP event handler method can handle multiple events.

Conclusion

Congratulations, you have learned how to consume ABAP RAP events in SAP S/4HANA. Try this in you next project and let me know your feedback.