✒️ABAP Los Eventos
ABAP Los Eventos
Understanding ABAP Events
Introduction to Events
In ABAP objects, there are specific methods known as triggers and others known as handlers. Triggers are the methods that initiate an event, while handlers are the methods that execute when an event occurs.
Triggering an Event
To trigger an event:
- Declare the event in the declarative part.
- Trigger the event within one of its methods.
Events are declared in the declarative part of a class or in an interface. For instance, to declare events dependent on instances:
EVENTS <EVENT> EXPORTING ... VALUE (<IMPORT>) TYPE TYPE [OPTIONAL] ...
For static events, the following statement is used:
CLASS-EVENTS <EVENT> ...
To trigger an event within a method, use:
RAISE EVENT <EVENT> EXPORTING ... <IMPORT> = <FIELD> ...
It's crucial not to confuse these object-oriented programming events with ABAP runtime events such as LOAD OF PROGRAM, INITIALIZATION, START OF SELECTION, or END OF SELECTION. These are entirely different concepts.
Event Handlers
Events are used to execute a series of methods that:
- Are defined as event handlers for that event.
- Are registered at runtime for the event.
To declare an instance-dependent event handler method, use:
METHOD <METHOD> FOR EVENT <EVENT> OF <CLASS/INTERFACE> IMPORTING ... <IMPORT> ...
Registering Event Handler Methods
To allow an event handler method to react to an event, the trigger to which it will react must be determined at runtime. This is done with:
SET HANDLER ... <HANDLER> ... [FOR] ...
Event Types
- Instance-dependent events declared in a class
- Instance-dependent events declared in an interface
- Static events declared in a class
- Static events declared in an interface
The syntax and effect of the SET HANDLER statement depend on which of these four cases occurs.
For an instance-dependent event, the FOR addition must be used to specify the instance for which the handler should be registered. You can specify a single instance as a trigger using a referenced variable:
SET HANDLER ... <HANDLER> ... FOR <REF>.
You can also register for all instances triggering the event:
SET HANDLER ... <HANDLER> ... FOR ALL INSTANCES.
The FOR addition cannot be used for static events:
SET HANDLER ... <HANDLER> ...
Event Handling Coordination
After the RAISE EVENT statement, all registered handler methods are executed before the next statement is processed (synchronous event handling). If an event handler method triggers events, the corresponding handler methods are executed before the original handler method continues.
To avoid the possibility of an infinite loop, events are currently limited to nesting 64 levels.
Events follow the same visibility structure as other components of a class. Therefore, they can be public, protected, or private. Event handler methods also have visibility attributes. The visibility of an event determines who is authorized to control or handle the event. Public events can be used by anyone, protected events can only be used by users within the class or its subclasses, and private events can only be used within the class.
Example of Event Handling
*&---------------------------------------------------------------------*
*& Report ZEJERCICIO_EVENTOS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zejercicio_eventos.
*----------------------------------------------------------------------*
* CLASS contador DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS contador DEFINITION.
PUBLIC SECTION.
METHODS incrementar_contador.
EVENTS valor_critico EXPORTING VALUE(exceso) TYPE i.
PRIVATE SECTION.
DATA: cuenta TYPE i,
umbral TYPE i VALUE 10.
ENDCLASS. "contador DEFINITION
*----------------------------------------------------------------------*
* CLASS contador IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS contador IMPLEMENTATION.
METHOD incrementar_contador.
DATA diferencia TYPE i.
ADD 1 TO cuenta.
IF cuenta > umbral.
diferencia = cuenta - umbral.
RAISE EVENT valor_critico EXPORTING exceso = diferencia.
ENDIF.
ENDMETHOD. "incrementar_contador
ENDCLASS. "contador IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS manejador DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS manejador DEFINITION.
PUBLIC SECTION.
METHODS manejar_exceso FOR EVENT valor_critico OF contador
IMPORTING exceso.
ENDCLASS. "manejador DEFINITION
*----------------------------------------------------------------------*
* CLASS manejador IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS manejador IMPLEMENTATION.
METHOD manejar_exceso.
WRITE: / 'El Exceso es: ', exceso.
ENDMETHOD. "manejar_exceso
ENDCLASS. "manejador IMPLEMENTATION
DATA: cuenta TYPE REF TO contador,
manejador TYPE REF TO manejador.
START-OF-SELECTION.
CREATE OBJECT: cuenta, manejador.
SET HANDLER manejador->manejar_exceso FOR ALL INSTANCES.
DO 20 TIMES.
CALL METHOD cuenta->incrementar_contador.
ENDDO.
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 118 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez