You will learn
- How to create and run an ABAP Daemon
- How to send PCP messages to an ABAP Daemon
Introduction to ABAP Daemons
ABAP Daemons are provided by the ABAP Daemon Framework (ADF). They are used to handle events in a reliable way by running in sessions with unlimited lifetime. ABAP programs can communicate with the daemons by sending messages to them using ABAP Messaging Channels and message type Push Channel Protocol.
You can make yourself more familiar with the ABAP Daemon Framework by reading the official documentation.
Push Channel Protocol (PCP) is a communications format similar to a simple HTTP message. It consists of header fields (name-value pairs) for metadata and a message body. For further information, have a look at the Specification of the Push Channel Protocol.
Step 1: Create a new ABAP Daemon class
ABAP Daemons are instances of an ABAP Daemon class that extend the base class CL_ABAP_DAEMON_EXT_BASE
.
First, create a new ABAP class ZCL_TUTORIAL_SIMPLE_DAEMON
and set CL_ABAP_DAEMON_EXT_BASE
as its superclass.
As you can see there is an error in line 1 since the necessary abstract methods have not been implemented yet. Click on the light bulb next to the line number and select Add 9 unimplemented methods to resolve this:
Log on to answer question
Step 2: Implement ON_ACCEPT method
Before a new instance of the ABAP Daemon class can be created, its ON_ACCEPT
method is called to determine if the daemon should start.
Replace your ON_ACCEPT
method with the following code. It accepts all start requests from your own class and rejects all requests from any other program. This is achieved by checking the calling program that initiated the start.
abap
Copy
METHOD if_abap_daemon_extension~on_accept.
TRY.
DATA lv_program_name TYPE program.
lv_program_name = cl_oo_classname_service=>get_classpool_name( 'ZCL_TUTORIAL_SIMPLE_DAEMON' ).
IF i_context_base->get_start_caller_info( )-program = lv_program_name.
e_setup_mode = co_setup_mode-accept.
ELSE.
e_setup_mode = co_setup_mode-reject.
ENDIF.
CATCH cx_abap_daemon_error.
" to do: error handling, e.g. write error log!
e_setup_mode = co_setup_mode-reject.
ENDTRY.
ENDMETHOD.
Log on to answer question
Step 3: Implement ON_START method
To start the ABAP Daemon, the ON_START
method is executed.
You can include a PCP message containing arbitrary st