Launching an APD analysis process from an ABAP program
In the following article, I describe one possible way to start an APD process during the execution of some ABAP program.
The technique I use is based on event driven Job Scheduling. The principle is to raise an event within the ABAP program that will trigger the execution of the APD. The event is raised by FM (function module) BP_EVENT_RAISE.
Once the event has been raised, the program will wait for a certain amount of time for the APD to complete before continuing its execution.
Events raised by FM BP_EVENT_RAISE are background events, defined using transaction SM62. Such events have no direct relationships with workflow events or ABAP Objects events. The purpose of background events is to trigger released background jobs.
For an example, look at the system background jobs RDDIMPDP*. To do this, go into transaction SM37 and list jobs with status released with start condition “after event” “*” (for all events). You’ll see that job RDDIMPDP is started whenever background event SAP_TRIGGER_RDDIMPDP is raised (incidentally this is done by tp during the import of change requests).
You’ll also see that job RDDIMPDP_CLIENT_nnn (where nnn is a client of your system) is started whenever background event SAP_TRIGGER_RDDIMPDP_CLIENT is raised, but only if the event was raised with parameter value equal to “nnn” (again where nnn is the client).
So if you want to hook into such an event, you need to schedule an ABAP report to start when the corresponding background event is raised.
The job that will be scheduled, will execute the RSAN_PROCESS_EXECUTE ( Execute Analysis Process ) program with a variant specifying which Analysis Process is to be run.
To create this variant start by executing running RSAN_PROCESS_EXECUTE with SE38 and define the variant to use. The action to execute should be Execute Analysis Process.
In my example Variant I create ZLREGR01 to execute the Analysis Process ZLREGR01.
Background scheduling of the Analysis Process
One necessary prerequisite for this scenario to work is that prior to running the ABAP program, a job has to be scheduled to start on the specific event.
Before scheduling the process, create the User Event that will trigger the background job. To achieve this, call up transaction SM62 ( Display / Edit Events ).
Simply select the User Event Names Maintain checkbox. In my example I create Event ZLREGR01.
You now have all that is required to setup the background job. You can create the job manually or with the help of the Job Wizard tool of transaction SM36 ( Define Background Job ). Ill use the Job Wizard. Go through the screens of the wizard and fill the relevant fields as follows:
- Give a name to the job, again I use ZREGR01
- Define the initial (and unique) step as an ABAP program step
- Specify RSAN_PROGRAM_EXECUTE as ABAP Program Name and ZLREGR01 and Variant
- Do not select additional steps as the only step of the job is to run the APD
- Select After Event as Start Condition.
- Set ZLREGR01 as Event ID and no Parameter. Check the Period box.
Concerning the Periodic Job option, this is clearly necessary if you plan to call up the AnalysisProcess more than once. By selecting this option the job will be rescheduled to listen for the event after its execution.
Raising a background event with the BP_JOB_SELECT function module
Before looking at the ABAP program I describe the FM BP_JOB_SELECT that raises the Event. The parameters to pass to the FM are EVENTID, EVENTPARM and TARGET_INSTANCE. In our case only EVENTID is necessary.
CALL FUNCTION 'BP_EVENT_RAISE' EXPORTING eventid = 'TEST_PC' eventparm = 'START' target_instance = ' ' EXCEPTIONS OTHERS = 01.
Checking the status of a background job with function module BP_JOB_SELECT
To understand how BP_JOB_SELECT works, let us start by running it in dialog mode via transaction SE37.
The initial screen shows us that there are 5 import parameters.
Parameter JOBSELECT_DIALOG should be set to N (or to BTC_NO if you use include LBTCHDEF in your program). If you set it to yes / BTC_YES, you would end up in a selection screen much similar to the one of SM37.
The SELECTION parameter can have 3 values: AL, NC and NG. This SELECTION parameter makes only sense regarding jobs which are submitted by the Schedule Manager. The (default) value ‘AL’ will show all jobs, ‘NG’ only Jobswhich are not generated (?), ‘NC’ only Jobs which are not confirmed . So, if you’ve no Schedule Manager , ‘AL’ will be ok
The parameters ENDDATE and ENDTIME are similar (?) to the ENDDATE and ENDTIME of the JOBSEL_PARAM_IN structure. If left blank, only the ENDDATE and ENDTIME fields of JOBSEL_PARAM_IN are taken into account.
The JOBSEL_PARAM_IN contains all the selection criteria of SM37. In particular in our scenario we will specify JOBNAME, EVENTID.
When I execute the FM with JOBNAME = ZLREGR01 and EVENTID = ZLREGR01 I can see that the JOBSELECT_JOBLIST table returns 1 entry.
The STATUS field is set to S for Scheduled. Let us now look at the same job once it has been triggered and it has completed. There are now two entries in the JOBSELECT_JOBLIST table. The first entry is the triggered and finished job. Its status is F and several field have been filled such as SDLSTRTDT/TM, STARTDATE/TIME ENDDATE/TIME.
The second entry is there because the job has been defined as periodic. Thus it reschedules itself each time it terminates.
Test program Z_RUN_JOB_AND_WAIT
We now have all the elements to create the sample program that triggers the job and waits for its outcome. The sample program I developedis quite simple, it does not for instance handle the situation where the job is aborted during its execution. However I included a timeout to handle the case where the Analysis Process goes on forever or takes too long. As each execution of the job will add an entry to the log, I will scan through the returned entries to find the first one to have started after the time of my raise event call.
REPORT Z_RUN_JOB_AND_WAIT. INCLUDE LBTCHDEF. DATA : l_cnt TYPE i, l_status TYPE BTCSTATUS, l_reftim TYPE SYUZEIT, l_strtim TYPE SYUZEIT, l_endtim TYPE SYUZEIT, job_selparam_btcselect TYPE btcselect, jobselect_list TYPE tbtcjob OCCURS 10 WITH HEADER LINE, ret TYPE i. * call up process chain TEST_PC l_reftim = SY-UZEIT. CALL FUNCTION 'BP_EVENT_RAISE' EXPORTING eventid = 'ZLREGR01' eventparm = '' target_instance = ' ' EXCEPTIONS OTHERS = 01. * Loop l_cnt times on a WAIT of 2 seconds * Followed by a test of the Job status job_selparam_btcselect-JOBNAME = 'BI_PROCESS_TRIGGER'. job_selparam_btcselect-USERNAME = sy-uname. job_selparam_btcselect-EVENTID = 'TEST_PC'. l_cnt = 5. WHILE l_cnt > 0. WAIT UP To 2 SECONDS. CALL FUNCTION 'BP_JOB_SELECT' EXPORTING jobselect_dialog = btc_no jobsel_param_in = job_selparam_btcselect selection = 'AL' TABLES jobselect_joblist = jobselect_list CHANGING error_code = ret EXCEPTIONS no_jobs_found = 3 OTHERS = 7. IF ret = 0. * Select the 1st job started after l_reftim l_strtim = SY-UZEIT. LOOP AT jobselect_list WHERE ( STRTTIME > l_reftim ) AND ( STRTDATE = SY-DATUM ). IF jobselect_list-STRTTIME < l_strtim. l_endtim = jobselect_list-ENDTIME. l_status = jobselect_list-STATUS. l_strtim = jobselect_list-STRTTIME. ENDIF. ENDLOOP. IF l_status = 'F'. l_cnt = 0. ENDIF. ELSE. * MESSAGE ENDIF. l_cnt = l_cnt - 1. ENDWHILE.