Get Example source ABAP code based on a different SAP table
ABAP_ADF - Creating and Using an ABAP Daemon This example demonstrates an ABAP Daemon>.
ABAP_SOURCE_CODE DEMO CL_DEMO_ABAP_DAEMON_DEMO
ABAP_DESCRIPTION The class demonstrates the most important aspects of an ABAP daemon and consists of the class above and two further classes.
Class CL_DEMO_ABAP_DAEMON_DEMO> This class makes it possible to start an ABAP daemon interactively and then perform various actions using the daemon. The checkboxes have the following meaning:
START_DAEMON> Attempt to start an ABAP daemon. Only one ABAP daemon of the used ABAP daemon class may exist. When the class is executed, a version number is passed in PCP> format. If an ABAP daemon already exists, this daemon is used.
GET_INFORMATION> Gets information about the ABAP daemon and displays it.
SEND_MESSAGE> Sends a text message to the ABAP daemon in PCP> format.
TRIGGER_AMC> Sends a PCP> message to the ABAP daemon instructing the daemon to send an AMC> message. The execution waits until the message arrives at a dedicated AMC receiver.
TRIGGER_EXCEPTION> Sends a PCP> message to the ABAP daemon, which raises an exception there. The exception produces a short dump> that can be viewed in transaction ST22>> and the ABAP daemon is restarted automatically. This raises the version number by 1.
TRIGGER_BLOCKING> Sends a PCP> message to the ABAP daemon instructing it to create a statement forbidden in non-blocking mode>. This raises an exception and short dump>, which can be viewed in transaction ST22>>. The ABAP daemon is then restarted automatically. This raises the version number by 1.
TRIGGER_RESTART> Sends a PCP> message to the ABAP daemon that restarts the daemon. This raises the version number by 1.
TRIGGER_RELOCATION> Sends a PCP> message to the ABAP daemon instructing the daemon to create a new instance of its class on a different ABAP_ASINSTANCE and delete the previous instance.
TRIGGER_STOP> Sends a PCP> message to the ABAP daemon instructing it to stop.
STOP_DAEMON> Stops the daemon. The class creates an output that displays the individual actions in both the class and in the ABAP daemon. To do this, the daemon writes entries to the export/import table > DEMO_INDX_BLOB>, which are then read by the class after a short wait time. If the current system is slow, this wait time may not be long enough to collect all actions of the daemon. The ABAP daemon on the current ABAP_ASINSTANCE can be monitored in parallel to the execution of the class using the transaction SMDAEMON>>.
Class CL_DEMO_ABAP_DAEMON_BROKER> The class CL_DEMO_ABAP_DAEMON_DEMO> does not work directly with the class CL_ABAP_DAEMON_CLIENT_MANAGER>> and any reads performed on this class are wrapped in the class CL_DEMO_ABAP_DAEMON_BROKER>> instead. This is because CL_ABAP_DAEMON_CLIENT_MANAGER> can only be used to access an ABAP daemon in the program in which the daemon was started. It must be possible for the daemon itself to also create an instance of its class in this example, which means the reads must be moved to a program that can be accessed by the ABAP program and the ABAP daemon. To enable this, CL_DEMO_ABAP_DAEMON_BROKER> contains the following methods:
CHECK_DAEMON> METH CL_DEMO_ABAP_DAEMON_BROKER=>CHECK_DAEMON This method determines whether a daemon of the ABAP daemon class CL_DEMO_ABAP_DAEMON> already exists and saves its ID for further use. The class CL_DEMO_ABAP_DAEMON_DEMO> starts a daemon of the class CL_DEMO_ABAP_DAEMON> only if no daemon exists. The method shown here for creating an ABAP daemon as a singleton is not really secure. It is possible that further daemons are created in the same class in the time between starting the daemon and it being returned by the method GET_DAEMON_INFO>. However, safe handling would exceed the scope of this simple example.
START_DAEMON> METH CL_DEMO_ABAP_DAEMON_BROKER=>START_DAEMON This method wraps the method START> of the class CL_ABAP_DAEMON_CLIENT_MANAGER> and starts an ABAP daemon of the ABAP daemon class CL_DEMO_ABAP_DAEMON>. Any callers are checked to ensure that the method is only used in the class CL_DEMO_ABAP_DAEMON_DEMO> and in the ABAP daemon class. The ID of the started daemon is saved for further use. If the ABAP daemon class accepts the start, the return value is not initial.
GET_DAEMON_INFO> METH CL_DEMO_ABAP_DAEMON_BROKER=>GET_DAEMON_INFO This method wraps the identically named method of the class CL_ABAP_DAEMON_CLIENT_MANAGER> and returns information about the ABAP daemons of the ABAP daemon class CL_DEMO_ABAP_DAEMON>.
ATTACH_DAEMON> METH CL_DEMO_ABAP_DAEMON_BROKER=>ATTACH_DAEMON This method wraps the method ATTACH> of the class CL_ABAP_DAEMON_CLIENT_MANAGER>. The returned reference to the ABAP daemon handle> is stored in the private attribute DAEMON_HANDLE>.
SEND_MESSAGE> METH CL_DEMO_ABAP_DAEMON_BROKER=>SEND_MESSAGE This method is used to send PCP> messages to the ABAP daemon. To do this, the method SEND> of the ABAP daemon handle> is used, which is referenced in the private attribute DAEMON_HANDLE>.
STOP_DAEMON> METH CL_DEMO_ABAP_DAEMON_BROKER=>STOP_DAEMON This method wraps the method STOP> of the class CL_ABAP_DAEMON_CLIENT_MANAGER> and is used to stop the ABAP daemon. The name of the ABAP daemon and the ABAP daemon class are defined as constants of the class CL_DEMO_ABAP_DAEMON_BROKER>.
Class CL_DEMO_ABAP_DAEMON> The class CL_DEMO_ABAP_DAEMON>> is a subclass of CL_ABAP_DAEMON_EXT_BASE> and the ABAP daemon class for this example. It implements the most important methods of the interface IF_ABAP_DAEMON_EXTENSION>> and further standalone helper methods. It also implements the interface IF_ABAP_TIMER_HANDLER>> so that it can be an ABAP Timer handler> for an ABAP Timer>. Although daemons should never be stopped, daemons created by this example should be deleted automatically after an hour, if it is not stopped explicitly first.
IF_ABAP_DAEMON_EXTENSION~ON_ACCEPT> METH CL_DEMO_ABAP_DAEMON=>IF_ABAP_DAEMON_EXTENSION~ON_ACCEPT Before the daemon is started, this method checks whether the calling program is the class pool of the class CL_DEMO_ABAP_DAEMON_BROKER>>. Only in this case is the output parameter E_SETUP_MODE> set so that the daemon can be started.
IF_ABAP_DAEMON_EXTENSION~ON_START> METH CL_DEMO_ABAP_DAEMON=>IF_ABAP_DAEMON_EXTENSION~ON_START Directly after the daemon is actually started, this method calls the helper method SET_CONTEXT>, which saves context information and starts a timer. An example of context information here is the version number passed by the caller when the daemon is started.
IF_ABAP_DAEMON_EXTENSION~ON_MESSAGE> METH CL_DEMO_ABAP_DAEMON=>IF_ABAP_DAEMON_EXTENSION~ON_MESSAGE This method evaluates the inbound PCP> messages and performs the corresponding actions directly or calls helper methods from the ABAP daemon class.
IF_ABAP_DAEMON_EXTENSION~ON_ERROR> METH CL_DEMO_ABAP_DAEMON=>IF_ABAP_DAEMON_EXTENSION~ON_ERROR IF_ABAP_DAEMON_EXTENSION~ON_RESTART> METH CL_DEMO_ABAP_DAEMON=>IF_ABAP_DAEMON_EXTENSION~ON_RESTART These methods call the helper method SET_CONTEXT> to set the context information again after a restart.
IF_ABAP_DAEMON_EXTENSION~ON_SERVER_SHUTDOWN> METH CL_DEMO_ABAP_DAEMON METH =>IF_ABAP_DAEMON_EXTENSION~ON_SERVER_SHUTDOWN When the current ABAP_ASINSTANCE is shut down, this method calls the helper method RELOCATE> to move the daemon to a different ABAP_ASINSTANCE .
IF_ABAP_TIMER_HANDLER~ON_TIMEOUT> METH CL_DEMO_ABAP_DAEMON=>IF_ABAP_TIMER_HANDLER~ON_TIMEOUT In the case of a timeout event of the ABAP Timer> started in SET_CONTEXT>, this method stops the daemon.
SET_CONTEXT> METH CL_DEMO_ABAP_DAEMON=>SET_CONTEXT This method sets an attribute of the class to the context object and uses its method SET_APPLICATION_PARAMETER> to store the current version number in the ABAP daemon memory> in PCP> format. If SET_CONTEXT> is called after a restart, the previous version number is fetched from the ABAP daemon memory and raised by 1. Furthermore, SET_CONTEXT> initializes an ABAP Timer> to which the method IF_ABAP_TIMER_HANDLER~ON_TIMEOUT> of the current daemon reacts.
SEND_AMC> METH CL_DEMO_ABAP_DAEMON=>SEND_AMC This method sends an AMC> message.
RELOCATE> METH CL_DEMO_ABAP_DAEMON=>RELOCATE This method attempts to move the current daemon to a different ABAP_ASINSTANCE . To do this, an ABAP_ASINSTANCE is selected at random from a list in the current AS ABAP. This application server is then used as a destination for starting a daemon of the current ABAP daemon class using CL_DEMO_ABAP_DAEMON_BROKER>. Here, the current context information from the ABAP daemon memory> is passed directly as start parameters in PCP> format. The current daemon is then stopped.
WRITE_LOG> METH CL_DEMO_ABAP_DAEMON=>WRITE_LOG This method writes log entries to the export/import table> DEMO_INDX_BLOB> as a string. It is called by the other methods to log the actions of the daemon for the output of the class CL_DEMO_ABAP_DAEMON_DEMO>. In this example, the remaining methods of the interface IF_ABAP_DAEMON_EXTENSION>> only write log entries.
Latest notes: This simple example does not guarantee that an ABAP daemon of the ABAP daemon class CL_DEMO_ABAP_DAEMON> is a system-wide singleton. Due to parallel accesses that cause restarts, multiple unwanted daemons may occur unintentionally. This applies in particular during the relocation of daemons to another ABAP_ASINSTANCE . Much more effort is required to create a real singleton. See the class CL_AD_EXT_SIMPLE_DAEMON>>, which can be used by the program RS_ABAP_DAEMON_TEST>>. NON_V5_HINTS ABAP_HINT_END