Friday 2 June 2017

ABAP OO Event handling (register/unregister)

OO Events is very imported and handling them is very easy in ABAP.

We need handler method to register event

METHODS : handle_event_raised FOR EVENT event_raised OF lcl_event_raiser.

then we need to set this method as handler

SET HANDLER me->handle_event_raised FOR i_raiser.
ABAP Developer using event handler like this example and many of them don’t care about unregistering the event. And why should we care about unregistering the Event?

I try to explain importance of event handling with very simple code

CLASS lcl_event_raiser DEFINITION FINAL.
  PUBLIC SECTION.
    EVENTS : event_raised.

    CLASS-METHODS : start.
  PRIVATE SECTION.
    CLASS-DATA : m_object TYPE REF TO lcl_event_raiser.

    METHODS : raise_event.
ENDCLASS.

CLASS lcl_event_handler DEFINITION FINAL.
  PUBLIC SECTION.
    METHODS : handle_event_raised FOR EVENT event_raised OF lcl_event_raiser.

    METHODS : constructor
      IMPORTING
        i_raiser TYPE REF TO lcl_event_raiser
        i_number TYPE i.

  PRIVATE SECTION.
    DATA : m_handler TYPE i.
ENDCLASS.

CLASS lcl_event_raiser IMPLEMENTATION.
  METHOD start.
    CREATE OBJECT m_object.

    DO 3 TIMES.
      DATA(l_handler) = NEW lcl_event_handler( i_raiser = m_object
                                               i_number = sy-index ).
      m_object->raise_event( ).
      FREE l_handler.
    ENDDO.

    m_object->raise_event( ).
  ENDMETHOD.

  METHOD raise_event.
    RAISE EVENT event_raised.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.
  METHOD constructor.
    SET HANDLER me->handle_event_raised FOR i_raiser.
    me->m_handler = i_number.
  ENDMETHOD.

  METHOD handle_event_raised.
    WRITE : / me->m_handler.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

  lcl_event_raiser=>start( ).

Here is the result :

SAP ABAP Tutorials and Materials, SAP ABAP Certifications

As you can see, first object is created and freed, but raise event still catch this object. Because free object is not enough to clear object for good. Once object event is registered, runtime is keeping object until event unregistered.

We can unregister event with simple code : ACTIVATION SPACE

We just add a little line of code to handler object.
........
* Definition part
METHODS : free.
........

........
* Implementation Part
METHOD free.
 SET HANDLER me->handle_event_raised FOR i_raiser ACTIVATION SPACE.
* Other data you want to clear, refresh or free
 CLEAR : me->m_handler.
ENDMETHOD.

And lets change start method like this:

...........  
METHOD start.
    CREATE OBJECT m_object.

    DO 3 TIMES.
      DATA(l_handler) = NEW lcl_event_handler( i_raiser = m_object
                                               i_number = sy-index ).
      m_object->raise_event( ).
      l_handler->free( ).
      FREE l_handler.
    ENDDO.

    m_object->raise_event( ).
 ENDMETHOD.
.........

Here is the result :

SAP ABAP Tutorials and Materials, SAP ABAP Certifications

So why should we care about unregistering the event?

Imagine this you want to deal with 2 or 3 ALV even more and you want to raise some events for these ALV. If you don’t unregister event, runtime allways find freed object and this can cause short dump, unwanted process etc. To avoid such unwanted case, we need to unregister events.

No comments:

Post a Comment