转自:http://scnblogs.techweb.com.cn/tcsapbw/archives/374.html
在ABAP中,我们有时候需要通过ABAP来定义JOB并执行它,也就是说,我们不使用SM36的GUI来创建Job。
自己亲身经历的一个case:我们有一个程序performance“很差”(并不是程序的问题,而是传递给其的数据量巨大),也就是说,我们外面有一个程序需要传递一个特别”巨大”的XML string流给该程序去解析,然而这个程序耗时也很可观,在运行时,”貌试”SAP死掉。所以,最后,solution方案就是通过一个ABAP操作Job,当然有数据的传递,使用SAP Memory.
一. JOB相关信息
例如,作为例子,我们需要一个job:去执行ZTEST_JOB_STEP1与ZTEST_JOB_STEP2这两个ABAP程序。
ZTEST_JOB_STEP1的程序:
REPORT ZTEST_JOB_STEP1. PARAMETERS: p_user type sy-uname. write:/ ‘job step 1′. WRITE:/ ‘Proceed User:’, p_user. |
其中效果为:带有一个variant供我们后面job用。
结果:
ZTEST_JOB_STEP2的程序:
REPORT ZTEST_JOB_STEP2 NO STANDARD PAGE HEADING. write:/ ‘job step 2′. |
其中效果为:
二. 使用ABAP操作JOB
我们的JOB-ABAP代码实现。
*&———————————————————————* *& Report ZTEST_ABAP_JOB *& *&———————————————————————* *& test using ABAP to trigger Job and manipulate Job *& *&———————————————————————* REPORT ZTEST_ABAP_JOB. DATA: w_jobid type tbtcjob-jobcount, w_stepnm type TBTCJOB-STEPCOUNT. PARAMETERS: p_job_nm type tbtcjob-jobname OBLIGATORY, p_var_st type raldb-variant. START-OF-SELECTION. *1. using JOB_OPEN to open job clear:w_jobid. CALL FUNCTION ‘JOB_OPEN’ EXPORTING * DELANFREP = ’ ’ * JOBGROUP = ’ ’ JOBNAME = p_job_nm * SDLSTRTDT = NO_DATE * SDLSTRTTM = NO_TIME * JOBCLASS = IMPORTING JOBCOUNT = w_jobid * CHANGING * RET = EXCEPTIONS CANT_CREATE_JOB = 1 INVALID_JOB_DATA = 2 JOBNAME_MISSING = 3 OTHERS = 4 . IF SY-SUBRC <> 0. write:/ ‘JOB_OPEN faild!’. else. write:/ ‘JOB_OPEN Successed!’. write:/ ‘———–Job Name:’, p_job_nm. write:/ ‘———–Job Count:’, w_jobid. ENDIF. *2. using JOB_SUMMIT to insert step clear:w_stepnm. CALL FUNCTION ‘JOB_SUBMIT’ EXPORTING * ARCPARAMS = AUTHCKNAM = sy-uname * COMMANDNAME = ’ ’ * OPERATINGSYSTEM = ’ ’ * EXTPGM_NAME = ’ ’ * EXTPGM_PARAM = ’ ’ * EXTPGM_SET_TRACE_ON = ’ ’ * EXTPGM_STDERR_IN_JOBLOG = ’X’ * EXTPGM_STDOUT_IN_JOBLOG = ’X’ * EXTPGM_SYSTEM = ’ ’ * EXTPGM_RFCDEST = ’ ’ * EXTPGM_WAIT_FOR_TERMINATION = ’X’ JOBCOUNT = w_jobid JOBNAME = p_job_nm * LANGUAGE = SY-LANGU * PRIPARAMS = ’ ’ REPORT = ‘ZTEST_JOB_STEP1′ VARIANT = p_var_st IMPORTING STEP_NUMBER = w_stepnm EXCEPTIONS BAD_PRIPARAMS = 1 BAD_XPGFLAGS = 2 INVALID_JOBDATA = 3 JOBNAME_MISSING = 4 JOB_NOTEX = 5 JOB_SUBMIT_FAILED = 6 LOCK_FAILED = 7 PROGRAM_MISSING = 8 PROG_ABAP_AND_EXTPG_SET = 9 OTHERS = 10. IF SY-SUBRC <> 0. write:/ ‘JOB_SUBMIT faild!’. else. write:/ ‘JOB_SUBMIT Successed!’. write:/ ‘———–Step:’, w_stepnm. ENDIF. clear:w_stepnm. CALL FUNCTION ‘JOB_SUBMIT’ EXPORTING * ARCPARAMS = AUTHCKNAM = sy-uname * COMMANDNAME = ’ ’ * OPERATINGSYSTEM = ’ ’ * EXTPGM_NAME = ’ ’ * EXTPGM_PARAM = ’ ’ * EXTPGM_SET_TRACE_ON = ’ ’ * EXTPGM_STDERR_IN_JOBLOG = ’X’ * EXTPGM_STDOUT_IN_JOBLOG = ’X’ * EXTPGM_SYSTEM = ’ ’ * EXTPGM_RFCDEST = ’ ’ * EXTPGM_WAIT_FOR_TERMINATION = ’X’ JOBCOUNT = w_jobid JOBNAME = p_job_nm * LANGUAGE = SY-LANGU * PRIPARAMS = ’ ’ REPORT = ‘ZTEST_JOB_STEP2′ * VARIANT = IMPORTING STEP_NUMBER = w_stepnm EXCEPTIONS BAD_PRIPARAMS = 1 BAD_XPGFLAGS = 2 INVALID_JOBDATA = 3 JOBNAME_MISSING = 4 JOB_NOTEX = 5 JOB_SUBMIT_FAILED = 6 LOCK_FAILED = 7 PROGRAM_MISSING = 8 PROG_ABAP_AND_EXTPG_SET = 9 OTHERS = 10. IF SY-SUBRC <> 0. write:/ ‘JOB_SUBMIT faild!’. else. write:/ ‘———–Step:’, w_stepnm. ENDIF. *3. using JOB_CLOSE to close job CALL FUNCTION ‘JOB_CLOSE’ EXPORTING * AT_OPMODE = ’ ’ * AT_OPMODE_PERIODIC = ’ ’ * CALENDAR_ID = ’ ’ * EVENT_ID = ’ ’ * EVENT_PARAM = ’ ’ * EVENT_PERIODIC = ’ ’ JOBCOUNT = w_jobid JOBNAME = p_job_nm * LASTSTRTDT = NO_DATE * LASTSTRTTM = NO_TIME * PRDDAYS = 0 * PRDHOURS = 0 * PRDMINS = 0 * PRDMONTHS = 0 * PRDWEEKS = 0 * PREDJOB_CHECKSTAT = ’ ’ * PRED_JOBCOUNT = ’ ’ * PRED_JOBNAME = ’ ’ * SDLSTRTDT = NO_DATE * SDLSTRTTM = NO_TIME * STARTDATE_RESTRICTION = BTC_PROCESS_ALWAYS STRTIMMED = ‘X’ * TARGETSYSTEM = ’ ’ * START_ON_WORKDAY_NOT_BEFORE = SY-DATUM * START_ON_WORKDAY_NR = 0 * WORKDAY_COUNT_DIRECTION = 0 * RECIPIENT_OBJ = * TARGETSERVER = ’ ’ * DONT_RELEASE = ’ ’ * TARGETGROUP = ’ ’ * DIRECT_START = * IMPORTING * JOB_WAS_RELEASED = * CHANGING * RET = EXCEPTIONS CANT_START_IMMEDIATE = 1 INVALID_STARTDATE = 2 JOBNAME_MISSING = 3 JOB_CLOSE_FAILED = 4 JOB_NOSTEPS = 5 JOB_NOTEX = 6 LOCK_FAILED = 7 INVALID_TARGET = 8 OTHERS = 9 . IF SY-SUBRC <> 0. write:/ ‘JOB_CLOSE faild!’. else. write:/ ‘JOB_CLOSE Successed!’. ENDIF. |
说明:
JOB_OPEN: 意思为:declare一个JOB并返回这个job的“内存ID”。
JOB_SUBMIT: 意思为:insert step for job。注意,这里需要为每一个step进行submit(如上面是两个program).
JOB_Close: 意思为:Release刚创建的job.然后可以在SM37中查看.
然后,我们执行:
最后结果:
然后,我们去SM37中可以check:
可以再看一下spool:
如其中ZTEST_JOB_STEP1的spool :
而ZTEST_JOB_STEP2的spool :