ABAP 通过sumbit调用另外一个程序使用job形式执行-简单例子
涉及到两个程序:
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输出的:
![](https://images2018.cnblogs.com/blog/1393281/201805/1393281-20180504200912858-1275019887.png)
到这里你应该有一个初步的认识了,那么关键看看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' EXPORTING jobname = gv_jobname IMPORTING jobcount = lv_jobnum EXCEPTIONS cant_create_job = 1 invalid_job_data = 2 jobname_missing = 3 OTHERS = 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' EXPORTING jobcount = lv_jobnum jobname = gv_jobname strtimmed = 'X' EXCEPTIONS cant_start_immediate = 1 invalid_startdate = 2 jobname_missing = 3 job_close_failed = 4 job_nosteps = 5 job_notex = 6 lock_failed = 7 OTHERS = 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_jobnum p_gv_jobname. DATA: ls_jobhead TYPE tbtcjob, lt_jobstep TYPE STANDARD TABLE OF tbtcstep. CALL FUNCTION 'BP_JOB_READ' EXPORTING job_read_jobcount = p_lv_jobnum job_read_jobname = p_gv_jobname job_read_opcode = '20' IMPORTING job_read_jobhead = ls_jobhead TABLES job_read_steplist = lt_jobstep EXCEPTIONS invalid_opcode = 1 job_doesnt_exist = 2 job_doesnt_have_steps = 3 OTHERS = 4. IF sy-subrc <> 0. ENDIF. ls_jobhead-sdluname = 'BATCH_CM'. CALL FUNCTION 'BP_JOB_MODIFY' EXPORTING dialog = 'N' jobcount = p_lv_jobnum jobname = p_gv_jobname new_jobhead = ls_jobhead opcode = '16' IMPORTING modified_jobhead = ls_jobhead TABLES new_steplist = lt_jobstep EXCEPTIONS cant_derelease_job = 1 cant_enq_job = 2 cant_read_jobdata = 3 cant_release_job = 4 cant_set_jobstatus_in_db = 5 cant_start_job_immediately = 6 cant_update_jobdata = 7 eventcnt_generation_error = 8 invalid_dialog_type = 9 invalid_new_jobdata = 10 invalid_new_jobstatus = 11 invalid_opcode = 12 invalid_startdate = 13 job_edit_failed = 14 job_modify_canceled = 15 job_not_modifiable_anymore = 16 nothing_to_do = 17 no_batch_on_target_host = 18 no_batch_server_found = 19 no_batch_wp_for_jobclass = 20 no_modify_privilege_given = 21 no_release_privilege_given = 22 no_startdate_no_release = 23 target_host_not_defined = 24 tgt_host_chk_has_failed = 25 invalid_targetgroup = 26 conflicting_targets = 27 OTHERS = 28. IF sy-subrc <> 0. ENDIF. ENDFORM.