Skip to Content

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 :

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.
PRIVATE SECTION.
DATA : m_raiser TYPE REF TO lcl_event_raiser.
........

........
METHOD constructor.
.......
 me->m_raiser = i_raiser.
 SET HANDLER me->handle_event_raised FOR me->m_raiser.
.......
ENDMETHOD.

* Implementation Part
METHOD free.
 SET HANDLER me->handle_event_raised FOR me->m_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 :

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.

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Xie Zhanli

    Thanks for the nice article.

    I got one question here for under which case we will choose to introduce the ABAP Event?

    Could you please give me some examples for me to better understand the Event-concept.

     

    Thanks you very much.

    Zac

    (0) 
    1. Tolga POLAT Post author

      Hi Xie Zhanli ,

      Thank you for comment. But your question is very hard 🙂

      First you need to decide your program algorithm. You can read this article for general concept : OO Events

      ABAP Development environment has very easy event handling concept comparing to other languages.

      I can give you one easy example :

      Imagine, you have button object and screen object. when button pressed you want to change something in screen object. Classical way of doing this, in button object you can call directly screen object and you can do your stuff. Or you can raise button_pressed event and screen object can handle it inside itself.

      But if you google “oo event design” you can find more document. I think, coding languages are not important for concept and design. It’s OO development at the end 🙂

      Cheers

      Tolga

      (0) 

Leave a Reply