Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ADF - ABAP Daemon Framework →  Examples for ABAP Daemons 

ADF, Mini ABAP Daemon

This example demonstrates a mini ABAP Daemon.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    TRY.
        DATA(pcp) = cl_ac_message_type_pcp=>create( ).
        pcp->set_text( `Hello Daemon!` ).
      CATCH cx_ac_message_type_pcp_error INTO DATA(pcp_exc).
        cl_demo_output=>display( pcp_exc->get_text( ) ).
        RETURN.
    ENDTRY.

    TRY.
        cl_abap_daemon_client_manager=>start(
          EXPORTING
            i_class_name = 'CL_DEMO_ABAP_MINI_DAEMON'
            i_name       = 'DemoMiniDaemon'
         IMPORTING
            e_instance_id = DATA(instance_id) ).

        cl_abap_daemon_client_manager=>attach(
          i_instance_id = instance_id )->send( pcp ).

        cl_abap_daemon_client_manager=>stop(
          i_instance_id = instance_id ).

      CATCH cx_abap_daemon_error INTO DATA(daemon_exc).
        cl_demo_output=>display( daemon_exc->get_text( ) ).
        RETURN.
    ENDTRY.

    DATA msg TYPE string.
    DATA(subrc) = 4.
    WHILE subrc = 4.
      IMPORT msg = msg FROM SHARED MEMORY demo_indx_blob(dm)
                       ID 'DemoMiniDaemon'.
      subrc = sy-subrc.
    ENDWHILE.
    ASSERT msg = `Hello Daemon!`.
    cl_demo_output=>display( 'Daemon has received the message' ).

Description

The class CL_DEMO_ABAP_MINI_DAEMON used by the program above demonstrates a mini ABAP Daemon class. It implements only the following methods of the interface IF_ABAP_DAEMON_EXTENSION:

  • ON_ACCEPT
METHOD if_abap_daemon_extension~on_accept.
  TRY.
      IF i_context_base->get_start_caller_info(
           )-program = 'DEMO_ABAP_MINI_DAEMON'.
        e_setup_mode = co_setup_mode-accept.
      ENDIF.
    CATCH cx_abap_daemon_error.
      RETURN.
  ENDTRY.
ENDMETHOD.
In this method, the return value E_SETUP_MODE must be set to ensure that the daemon is created. The method limits the creation of daemons to the program DEMO_ABAP_MINI_DAEMON.
  • ON_MESSAGE
METHOD IF_ABAP_DAEMON_EXTENSION~ON_MESSAGE.
  TRY.
      DATA(msg) = i_message->get_text( ).
    CATCH cx_ac_message_type_pcp_error.
      RETURN.
  ENDTRY.
  EXPORT msg = msg TO SHARED MEMORY demo_indx_blob(dm)
                   ID 'DemoMiniDaemon'.
ENDMETHOD.
This method indicates that the daemon is responding to message from a program. To enable this, the message is exported to the cross-transaction application buffer of the shared memory.

The program DEMO_ABAP_MINI_DAEMON uses ABAP Daemon Manager to do the following:

  • Start a daemon based on the class CL_DEMO_ABAP_MINI_DAEMON.
  • Stop the daemon immediately (not a typical step).

It then checks whether the daemon wrote the message to the shared memory.