Events

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Sometimes one object has to tell other objects that something happened, without knowing who is listening. A claim is approved, and the mail service, the audit log and a budget alarm all want to react, but the claim itself should not contain code for any of them. ABAP offers events for this. An event is a signal that an object can send, and a handler is a method of another class that has been signed up to receive it. A newcomer meets events in old class libraries and in graphical controls, and a developer uses them when one class must stay independent of the classes that react to it. After reading this page you can declare an event, raise it with parameters, write a handler method, register and deregister handlers, read the sender of an event and explain what the order and the lifetime rules mean for your code. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). The statements below are part of the core class model and apply in ABAP Cloud, in classic Standard ABAP, on premise and in the Public Edition alike. They are not the same thing as RAP business events, which announce a change of a business object to other systems and are a different mechanism.

Before you read

Key terms

  • Event: a signal declared in a class that the class can send to any number of listeners.

  • Event handler: a method that is registered for an event and runs when the event is raised.

  • Raiser: the object or class whose method raises the event.

  • Sender: the implicit parameter that gives a handler a reference to the object that raised the event.

Declaring an event

An event is a component of a class, like an attribute or a method. You declare it in the definition part with EVENTS for an instance event, which belongs to each object, or CLASS-EVENTS for a static event, which belongs to the class. An event can have output parameters, written after EXPORTING; they must be passed by value, and they describe what the handlers receive. The event has no code of its own and no return value.

Every instance event has one more parameter that you never declare: sender. When the event is raised, ABAP fills it with a reference to the raising object, so a handler that serves many claims can tell which one fired. The static type of sender is the class named after FOR EVENT ... OF in the handler, and its dynamic type is the class of the raising object. Static events have no sender.

Raising an event

RAISE EVENT evt EXPORTING p1 = a1 is allowed only inside a method. An instance event can be raised only by an instance method of the class that declares it, of a subclass, or of a class that implements the interface that declares it. A static event can be raised by a static method or by an instance method. Every non-optional parameter of the event must get a value; you cannot set sender yourself.

When the statement runs, ABAP calls every handler that is registered for the event, one after another, and only then continues after RAISE EVENT. So an event is not asynchronous: the raiser waits for its listeners. Four more facts matter in practice:

  • The order in which the handlers run is not defined and can change while the program runs. If two reactions must run in a fixed order, register one handler that calls both.

  • If a handler ends with an exception, event handling stops. For a class-based exception, control then returns to the method that raised the event.

  • A handler may raise further events. To avoid endless recursion, at most 1023 further events can be raised inside event handling.

  • If nobody is registered, RAISE EVENT does nothing. The raiser never knows whether anyone listened.

Handler methods and SET HANDLER

A handler is an ordinary method whose definition says which event it serves: METHODS on_approved FOR EVENT approved OF lcl_claim IMPORTING amount sender. CLASS-METHODS declares a static handler. The IMPORTING list may name any of the event’s parameters, and sender for an instance event, but only those it needs. The handler’s parameters get their types from the event, so you do not type them again.

A handler does nothing until it is registered. The statement SET HANDLER does that, and it has two forms:

  • SET HANDLER h FOR oref registers an instance event handler for the events of one object, and SET HANDLER h FOR ALL INSTANCES for the events of every object of the class.

  • SET HANDLER h registers a static event handler, for a static event.

The addition ACTIVATION abap_false deregisters the handler instead. sy-subrc is 0 when all handlers were registered or deregistered, 4 when a handler could not be registered because it already is, and 8 when one could not be deregistered because it was not registered.

Registering an instance method puts a reference to its object into a system table. For the garbage collector that reference counts like any other, so a handler object stays alive as long as it is registered. If the raising object is deleted, its registrations disappear with it. Deregister handlers you no longer need, or the objects they belong to cannot be freed.

Example: a claim is approved

In the expense claims example, a claim announces approved with the amount. A mail handler reports the claim number, using sender, and an audit handler records the amount. A third handler is deregistered to show that a deregistered handler hears nothing, and a static event at the end shows CLASS-EVENTS. The claim class has no code for mail or audit: the report that wires the objects together decides who listens.

sequenceDiagram participant R as Report participant C as lcl_claim participant M as lcl_mailer participant A as lcl_audit R->>C: SET HANDLER (mailer, audit) R->>C: approve( 120 ) C->>C: RAISE EVENT approved C-->>M: on_approved( amount, sender ) C-->>A: on_approved( amount ) C-->>R: approve returns

The listing below ran in the local playground (open-abap, a community transpiler that runs a subset of ABAP in Node.js). The mail line and the audit line may come in either order in real ABAP, because the order of handlers is not defined.

REPORT zevents.

CLASS lcl_claim DEFINITION.
  PUBLIC SECTION.
    EVENTS approved EXPORTING VALUE(amount) TYPE i.
    METHODS constructor IMPORTING id TYPE string.
    METHODS approve IMPORTING amount TYPE i.
    DATA id TYPE string READ-ONLY.
ENDCLASS.

CLASS lcl_claim IMPLEMENTATION.
  METHOD constructor.
    me->id = id.
  ENDMETHOD.
  METHOD approve.
    RAISE EVENT approved EXPORTING amount = amount.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_mailer DEFINITION.
  PUBLIC SECTION.
    METHODS on_approved FOR EVENT approved OF lcl_claim
      IMPORTING amount sender.
ENDCLASS.

CLASS lcl_mailer IMPLEMENTATION.
  METHOD on_approved.
    WRITE / |mail: claim { sender->id } approved for { amount }|.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_audit DEFINITION.
  PUBLIC SECTION.
    METHODS on_approved FOR EVENT approved OF lcl_claim
      IMPORTING amount.
ENDCLASS.

CLASS lcl_audit IMPLEMENTATION.
  METHOD on_approved.
    WRITE / |audit: amount { amount } recorded|.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_alarm DEFINITION.
  PUBLIC SECTION.
    CLASS-EVENTS limit_reached.
    CLASS-METHODS check IMPORTING total TYPE i.
    CLASS-METHODS on_limit FOR EVENT limit_reached OF lcl_alarm.
ENDCLASS.

CLASS lcl_alarm IMPLEMENTATION.
  METHOD check.
    IF total > 1000.
      RAISE EVENT limit_reached.
    ENDIF.
  ENDMETHOD.
  METHOD on_limit.
    WRITE / 'alarm: limit reached'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA(claim) = NEW lcl_claim( `C-1001` ).
  DATA(other) = NEW lcl_claim( `C-1002` ).
  DATA(mailer) = NEW lcl_mailer( ).
  DATA(audit) = NEW lcl_audit( ).

  SET HANDLER mailer->on_approved FOR claim.
  SET HANDLER audit->on_approved FOR ALL INSTANCES.
  claim->approve( 120 ).
  other->approve( 80 ).

  SET HANDLER audit->on_approved FOR ALL INSTANCES ACTIVATION abap_false.
  SET HANDLER mailer->on_approved FOR claim ACTIVATION abap_false.
  claim->approve( 50 ).
  WRITE / 'after deactivation: nothing above'.

  SET HANDLER lcl_alarm=>on_limit.
  lcl_alarm=>check( 500 ).
  lcl_alarm=>check( 1500 ).

Output:

mail: claim C-1001 approved for 120
audit: amount 120 recorded
audit: amount 80 recorded
after deactivation: nothing above
alarm: limit reached

Read the output line by line. The first approval of claim C-1001 reaches both handlers, and the mailer reads the claim number from sender. The approval of C-1002 reaches only the audit handler, because the mailer was registered for claim alone while the audit handler was registered for all instances. After both registrations are switched off, the third approval prints nothing, and the raiser did not notice. The static event needs no object: a handler registered with SET HANDLER lcl_alarm=>on_limit hears the alarm only when the total is above 1000.

Events in interfaces

An interface can declare events too, and every class that implements the interface can raise them. A handler then names the interface event in its definition, for example FOR EVENT lif_notifier~sent OF lcl_mailbox, and the same event parameters and sender apply. This lets a handler work with any class that implements the interface, which is the usual way to keep a raiser and its listeners apart. The listing below requires an SAP system; not run here (the local playground does not call handlers of events declared in an interface).

INTERFACE lif_notifier.
  EVENTS sent EXPORTING VALUE(text) TYPE string.
ENDINTERFACE.

CLASS lcl_mailbox DEFINITION.
  PUBLIC SECTION.
    INTERFACES lif_notifier.
    METHODS send IMPORTING text TYPE string.
ENDCLASS.

CLASS lcl_mailbox IMPLEMENTATION.
  METHOD send.
    RAISE EVENT lif_notifier~sent EXPORTING text = text.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_printer DEFINITION.
  PUBLIC SECTION.
    METHODS print FOR EVENT lif_notifier~sent OF lcl_mailbox
      IMPORTING text sender.
ENDCLASS.

CLASS lcl_printer IMPLEMENTATION.
  METHOD print.
    WRITE / |printed: { text }|.
  ENDMETHOD.
ENDCLASS.

When to use events

  • Use an event when the raiser should not know its listeners and several listeners may come and go, as with the claim, the mail service and the audit log.

  • Use a plain method call when exactly one known class must react and the caller needs a result, because an event gives no return value and no guarantee that anyone listens.

  • Do not rely on the order of handlers, and do not put logic in a handler that the raiser’s caller depends on to have run, unless you know the handler is registered.

  • Deregister handlers with ACTIVATION abap_false when their object is no longer needed, because registration keeps the object alive.

Summary

  • An event is a signal declared with EVENTS or CLASS-EVENTS; it has output parameters passed by value and, for an instance event, an implicit sender.

  • RAISE EVENT works only inside a method and runs all registered handlers before the method continues; the order of the handlers is not defined.

  • A handler is a method declared with FOR EVENT ... OF and does nothing until SET HANDLER registers it; ACTIVATION abap_false deregisters it.

  • Registration keeps the handler object alive until it is deregistered, and sy-subrc reports duplicate or missing registrations.

  • An exception in a handler stops event handling, and events are not RAP business events.