涉及到两个程序:
ZTEST_ZUMA02 (主程序)
ZTEST_ZUMA(被调用的程序,需要以后台job执行)
"ztest_zuma 的代码
DATA col TYPE i VALUE 0.
DO 8 TIMES.
MESSAGE 'JOB HERE' TYPE 'S'.
ENDDO.
程序ZTEST_ZUMA是在程序ZTEST_ZUMA02中以job的形式调用的,先来看看SM37里面ZTEST_ZUMA的执行日志,当然这些日志是ZTEST_ZUMA程序中通过message输出的:
到这里你应该有一个初步的认识了,那么关键看看ZTEST_ZUMA02是通过什么方式实现以JOB的形式调用ZTEST_ZUMA的呢,看一下代码的几处关键位置,job_open,submit,job_close.
简单解释一下,代码中也有注释。
job_open :开启一个后台job,job名任意命名,会生产一个job id.
submit :调用需要设置为后台执行的程序,使用via 参数设置job的相关参数.(这个时候可以看到SM37里面有一个job 状态为schedule).
job_close :把之前创建的job release 并执行(参数strtimmed = 'X' 表示立即执行,设置某一个时间执行还没有研究).
====================== REPORT ztest_zuma02. DATA: lv_jobnum TYPE btcjobcnt. DATA: gv_jobname TYPE btcjob VALUE 'ZUMATESTJOB'."Step1. open a job define a job_name(whatever you want), and return a jobnumber. CALL FUNCTION 'JOB_OPEN'EXPORTINGjobname = gv_jobnameIMPORTINGjobcount = lv_jobnumEXCEPTIONScant_create_job = 1invalid_job_data = 2jobname_missing = 3OTHERS = 4."Step2. Submit a program which you want to run in background as job. SUBMIT ztest_zuma VIA JOB gv_jobname NUMBER lv_jobnum AND RETURN. "ztest_zuma 的代码 "DATA col TYPE i VALUE 0. "DO 8 TIMES. " MESSAGE 'JOB HERE' TYPE 'S'. "ENDDO."Step3-Option. This is option step, for some special requirment modify job name instead of sy-uname. " in this step first call BP_JOB_READ to read the job information and then call BP_JOB_MODIFY to modify " the user name. PERFORM modify_job_name USING lv_jobnum gv_jobname.IF sy-subrc = 0. "Step4. To release the job which you were opened in step1.CALL FUNCTION 'JOB_CLOSE'EXPORTINGjobcount = lv_jobnumjobname = gv_jobnamestrtimmed = 'X'EXCEPTIONScant_start_immediate = 1invalid_startdate = 2jobname_missing = 3job_close_failed = 4job_nosteps = 5job_notex = 6lock_failed = 7OTHERS = 8.IF sy-subrc NE 0.MESSAGE 'Job error' TYPE 'E'.ELSE.MESSAGE 'Job success' TYPE 'S'.ENDIF. ENDIF. *&---------------------------------------------------------------------* *& Form MODIFY_JOB_NAME *&---------------------------------------------------------------------* * Modify user name for job owner *----------------------------------------------------------------------* * -->P_LV_JOBNUM text * -->P_GV_JOBNAME text *----------------------------------------------------------------------* FORM modify_job_name USING p_lv_jobnump_gv_jobname.DATA: ls_jobhead TYPE tbtcjob,lt_jobstep TYPE STANDARD TABLE OF tbtcstep.CALL FUNCTION 'BP_JOB_READ'EXPORTINGjob_read_jobcount = p_lv_jobnumjob_read_jobname = p_gv_jobnamejob_read_opcode = '20'IMPORTINGjob_read_jobhead = ls_jobheadTABLESjob_read_steplist = lt_jobstepEXCEPTIONSinvalid_opcode = 1job_doesnt_exist = 2job_doesnt_have_steps = 3OTHERS = 4.IF sy-subrc <> 0.ENDIF.ls_jobhead-sdluname = 'BATCH_CM'.CALL FUNCTION 'BP_JOB_MODIFY'EXPORTINGdialog = 'N'jobcount = p_lv_jobnumjobname = p_gv_jobnamenew_jobhead = ls_jobheadopcode = '16'IMPORTINGmodified_jobhead = ls_jobheadTABLESnew_steplist = lt_jobstepEXCEPTIONScant_derelease_job = 1cant_enq_job = 2cant_read_jobdata = 3cant_release_job = 4cant_set_jobstatus_in_db = 5cant_start_job_immediately = 6cant_update_jobdata = 7eventcnt_generation_error = 8invalid_dialog_type = 9invalid_new_jobdata = 10invalid_new_jobstatus = 11invalid_opcode = 12invalid_startdate = 13job_edit_failed = 14job_modify_canceled = 15job_not_modifiable_anymore = 16nothing_to_do = 17no_batch_on_target_host = 18no_batch_server_found = 19no_batch_wp_for_jobclass = 20no_modify_privilege_given = 21no_release_privilege_given = 22no_startdate_no_release = 23target_host_not_defined = 24tgt_host_chk_has_failed = 25invalid_targetgroup = 26conflicting_targets = 27OTHERS = 28.IF sy-subrc <> 0.ENDIF. ENDFORM.