Для запуска задач часто приходится писать один и тот же код, поэтому решил реализовать класс обертку для переиспользования.
Все, что требуется, это указать имя отчета, выполняющего обработку, и данные, с которыми отчет будет работать.
Внутри основного метода есть вызов всех стандартных диалогов для настройки/планирования запускаемой задачи (background job).
Все protected методы доступны и могут быть переопределены (опционально) наследниками, это обеспечивает гибкий подход к процессу вызова.
Основной класс:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
class /Z/CL_MM_JOB_WRAPPER definition public create public . public section. types: BEGIN OF mty_s_job_buffer, id TYPE indx_srtfd, data TYPE REF TO data, END OF mty_s_job_buffer . types: mty_t_job_buffer TYPE STANDARD TABLE OF mty_s_job_buffer . types: BEGIN OF mty_s_job_params, progname TYPE progname, jobname TYPE tbtcjob-jobname, print_params TYPE pri_params, output_params TYPE tbtcstrt, count TYPE tbtcjob-jobcount, END OF mty_s_job_params . methods START_JOB importing !IV_REPORT_NAME type PROGNAME !IT_BUFFER_DATA type MTY_T_JOB_BUFFER optional returning value(RV_COUNT) type TBTCJOB-JOBCOUNT exceptions CANCELLED . methods GENERATE_JOB_NAME returning value(RV_JOB_NAME) type TBTCJOB-JOBNAME . protected section. methods RUN_REPORT importing !IS_JOB_PARAMETERS type MTY_S_JOB_PARAMS . methods WRITE_TO_CLDB importing !IT_BUFFER type MTY_T_JOB_BUFFER . methods FREE_CLDB importing !IT_BUFFER type MTY_T_JOB_BUFFER . methods GET_PRINT_PARAMETERS exporting !EV_COUNT type TBTCJOB-JOBCOUNT !EV_JOBNAME type TBTCJOB-JOBNAME !ES_PRINT_PARAMS type PRI_PARAMS exceptions CANCELLED . methods START_DATE_EDITOR returning value(RS_OUT_PARAMETERS) type TBTCSTRT exceptions ERROR . methods JOB_DELETE importing !IV_COUNT type TBTCJOB-JOBCOUNT !IV_JOBNAME type TBTCJOB-JOBNAME . methods JOB_CLOSE importing !IV_COUNT type TBTCJOB-JOBCOUNT !IV_JOBNAME type TBTCJOB-JOBNAME !IS_OUTPUT_PARAMS type TBTCSTRT exceptions ERROR . private section. constants MC_MODE_BATCH type SY-CALLR value 'BATCH' ##NO_TEXT. constants MC_DIALOG type BTCH0000-CHAR1 value 'Y' ##NO_TEXT. constants MC_START_NOW type CHAR1 value 'I' ##NO_TEXT. data MT_BUFFER type MTY_T_JOB_BUFFER . data MS_JOB_PARAMETERS type MTY_S_JOB_PARAMS . ENDCLASS. CLASS /Z/CL_MM_JOB_WRAPPER IMPLEMENTATION. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Protected Method /Z/CL_MM_JOB_WRAPPER->FREE_CLDB * +-------------------------------------------------------------------------------------------------+ * | [--->] IT_BUFFER TYPE MTY_T_JOB_BUFFER * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD free_cldb. *--------------------------------------------------------------------* * FREE_CLDB *--------------------------------------------------------------------* * Зачистка буфера *--------------------------------------------------------------------* LOOP AT it_buffer ASSIGNING FIELD-SYMBOL(<ls_buffer>). /Z/cl_mm_clustdb_api=>free( <ls_buffer>-id ). ENDLOOP. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method /Z/CL_MM_JOB_WRAPPER->GENERATE_JOB_NAME * +-------------------------------------------------------------------------------------------------+ * | [<-()] RV_JOB_NAME TYPE TBTCJOB-JOBNAME * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD generate_job_name. *--------------------------------------------------------------------* * GENERATE_JOB_NAME *--------------------------------------------------------------------* * Генерация имени задачи *--------------------------------------------------------------------* CONSTANTS: lc_gnd TYPE string VALUE `_`, lc_space TYPE string VALUE ` `. TYPES: BEGIN OF lty_s_job_name, tcode TYPE char20, user TYPE char12, END OF lty_s_job_name. DATA(ls_job_key) = VALUE lty_s_job_name( ). ls_job_key-tcode = sy-tcode. ls_job_key-user = sy-uname. REPLACE ALL OCCURRENCES OF lc_space IN ls_job_key WITH lc_gnd. rv_job_name = ls_job_key. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Protected Method /Z/CL_MM_JOB_WRAPPER->GET_PRINT_PARAMETERS * +-------------------------------------------------------------------------------------------------+ * | [<---] EV_COUNT TYPE TBTCJOB-JOBCOUNT * | [<---] EV_JOBNAME TYPE TBTCJOB-JOBNAME * | [<---] ES_PRINT_PARAMS TYPE PRI_PARAMS * | [EXC!] CANCELLED * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD get_print_parameters. *--------------------------------------------------------------------* * GET_PRINT_PARAMETERS *--------------------------------------------------------------------* * Получаем параметры печати *--------------------------------------------------------------------* DATA(lv_valid) = VALUE abap_bool( ). CALL FUNCTION 'GET_PRINT_PARAMETERS' EXPORTING report = sy-cprog mode = mc_mode_batch IMPORTING out_parameters = es_print_params valid = lv_valid. IF lv_valid = abap_false. RAISE cancelled. ENDIF. ev_jobname = generate_job_name( ). DATA(lv_count) = VALUE tbtcjob-jobcount( ). CALL FUNCTION 'JOB_OPEN' EXPORTING jobname = ev_jobname sdlstrtdt = sy-datum sdlstrttm = sy-uzeit IMPORTING jobcount = ev_count EXCEPTIONS cant_create_job = 1 invalid_job_data = 2 jobname_missing = 3 OTHERS = 4. IF sy-subrc <> 0. RAISE cancelled. ENDIF. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Protected Method /Z/CL_MM_JOB_WRAPPER->JOB_CLOSE * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_COUNT TYPE TBTCJOB-JOBCOUNT * | [--->] IV_JOBNAME TYPE TBTCJOB-JOBNAME * | [--->] IS_OUTPUT_PARAMS TYPE TBTCSTRT * | [EXC!] ERROR * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD job_close. *--------------------------------------------------------------------* * job_close *--------------------------------------------------------------------* * Запуск выполнения JOB'a *--------------------------------------------------------------------* CASE is_output_params-startdttyp. WHEN mc_start_now. DATA(lv_start_immediate) = abap_true. ENDCASE. CALL FUNCTION 'JOB_CLOSE' EXPORTING jobcount = iv_count jobname = iv_jobname at_opmode_periodic = is_output_params-periodic calendar_id = is_output_params-calendarid event_id = is_output_params-eventid event_param = is_output_params-eventparm event_periodic = is_output_params-periodic laststrtdt = is_output_params-laststrtdt laststrttm = is_output_params-laststrttm prddays = is_output_params-prddays prdhours = is_output_params-prdhours prdmins = is_output_params-prdmins prdmonths = is_output_params-prdmonths prdweeks = is_output_params-prdweeks predjob_checkstat = is_output_params-checkstat pred_jobcount = is_output_params-predjobcnt pred_jobname = is_output_params-predjob sdlstrtdt = is_output_params-sdlstrtdt sdlstrttm = is_output_params-sdlstrttm startdate_restriction = is_output_params-prdbehav strtimmed = lv_start_immediate targetsystem = is_output_params-instname start_on_workday_not_before = is_output_params-notbefore start_on_workday_nr = is_output_params-wdayno workday_count_direction = is_output_params-wdaycdir time_zone = is_output_params-tmzone 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 invalid_time_zone = 9 OTHERS = 10. IF sy-subrc <> 0. RAISE error. ENDIF. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Protected Method /Z/CL_MM_JOB_WRAPPER->JOB_DELETE * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_COUNT TYPE TBTCJOB-JOBCOUNT * | [--->] IV_JOBNAME TYPE TBTCJOB-JOBNAME * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD job_delete. *--------------------------------------------------------------------* * JOB_DELETE *--------------------------------------------------------------------* * Удалить задачу *--------------------------------------------------------------------* CALL FUNCTION 'BP_JOB_DELETE' EXPORTING jobcount = iv_count jobname = iv_jobname EXCEPTIONS cant_delete_event_entry = 1 cant_delete_job = 2 cant_delete_joblog = 3 cant_delete_steps = 4 cant_delete_time_entry = 5 cant_derelease_successor = 6 cant_enq_predecessor = 7 cant_enq_successor = 8 cant_enq_tbtco_entry = 9 cant_update_predecessor = 10 cant_update_successor = 11 commit_failed = 12 jobcount_missing = 13 jobname_missing = 14 job_does_not_exist = 15 job_is_already_running = 16 no_delete_authority = 17 OTHERS = 18. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Protected Method /Z/CL_MM_JOB_WRAPPER->RUN_REPORT * +-------------------------------------------------------------------------------------------------+ * | [--->] IS_JOB_PARAMETERS TYPE MTY_S_JOB_PARAMS * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD run_report. *--------------------------------------------------------------------* * RUN_REPORT *--------------------------------------------------------------------* * Запуск отчета *--------------------------------------------------------------------* TRY . SUBMIT (ms_job_parameters-progname) TO SAP-SPOOL SPOOL PARAMETERS ms_job_parameters-print_params WITHOUT SPOOL DYNPRO VIA JOB ms_job_parameters-jobname NUMBER ms_job_parameters-count AND RETURN. CATCH cx_root. ENDTRY. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Protected Method /Z/CL_MM_JOB_WRAPPER->START_DATE_EDITOR * +-------------------------------------------------------------------------------------------------+ * | [<-()] RS_OUT_PARAMETERS TYPE TBTCSTRT * | [EXC!] ERROR * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD start_date_editor. *--------------------------------------------------------------------* * START_DATE_EDITOR *--------------------------------------------------------------------* * Запуск диалога выбора даты *--------------------------------------------------------------------* DATA(ls_options) = VALUE tbtcstrt( ). DATA(lv_mod_flag) = VALUE btch0000-int4( ). DATA(ls_output) = VALUE tbtcstrt( ). CALL FUNCTION 'BP_START_DATE_EDITOR' EXPORTING stdt_dialog = mc_dialog stdt_input = ls_options stdt_opcode = 14 IMPORTING stdt_modify_type = lv_mod_flag stdt_output = rs_out_parameters EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0 OR lv_mod_flag = 2. RAISE error. ENDIF. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Public Method /Z/CL_MM_JOB_WRAPPER->START_JOB * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_REPORT_NAME TYPE PROGNAME * | [--->] IT_BUFFER_DATA TYPE MTY_T_JOB_BUFFER(optional) * | [<-()] RV_COUNT TYPE TBTCJOB-JOBCOUNT * | [EXC!] CANCELLED * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD start_job. *--------------------------------------------------------------------* * START_JOB *--------------------------------------------------------------------* * Запуск выполнения фонового задания *--------------------------------------------------------------------* CLEAR: mt_buffer[], ms_job_parameters. ms_job_parameters-progname = iv_report_name. "Получаем параметры печати get_print_parameters( IMPORTING es_print_params = ms_job_parameters-print_params ev_jobname = ms_job_parameters-jobname ev_count = ms_job_parameters-count EXCEPTIONS cancelled = 1 OTHERS = 2 ). IF sy-subrc <> 0. RAISE cancelled. ENDIF. "Сохраняем данные для запуска JOB'a APPEND LINES OF it_buffer_data[] TO mt_buffer[]. write_to_cldb( EXPORTING it_buffer = mt_buffer ). "Стартуем report run_report( EXPORTING is_job_parameters = ms_job_parameters ). "Получаем время запуска, и остальные параметры start_date_editor( RECEIVING rs_out_parameters = ms_job_parameters-output_params EXCEPTIONS error = 1 OTHERS = 2 ). IF sy-subrc <> 0. "Удаляем записи из INDX free_cldb( EXPORTING it_buffer = mt_buffer[] ). "Удаляем задачу job_delete( EXPORTING iv_count = ms_job_parameters-count iv_jobname = ms_job_parameters-jobname ). RAISE cancelled. ENDIF. "Запуск выполнения JOB'a job_close( EXPORTING iv_count = ms_job_parameters-count iv_jobname = ms_job_parameters-jobname is_output_params = ms_job_parameters-output_params EXCEPTIONS error = 1 " Ошибка при запуске задачи OTHERS = 2 ). IF sy-subrc <> 0. RAISE cancelled. ENDIF. "Возвращаем номер задачи rv_count = ms_job_parameters-count. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Instance Protected Method /Z/CL_MM_JOB_WRAPPER->WRITE_TO_CLDB * +-------------------------------------------------------------------------------------------------+ * | [--->] IT_BUFFER TYPE MTY_T_JOB_BUFFER * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD write_to_cldb. *--------------------------------------------------------------------* * WRITE_TO_CLDB *--------------------------------------------------------------------* * Записать данные в кластер БД *--------------------------------------------------------------------* FIELD-SYMBOLS: <ls_any> TYPE any. LOOP AT it_buffer ASSIGNING FIELD-SYMBOL(<ls_buffer>). ASSIGN <ls_buffer>-data->* TO <ls_any>. /Z/cl_mm_clustdb_api=>import( EXPORTING iv_id = <ls_buffer>-id ia_data = <ls_any> ). UNASSIGN <ls_any>. ENDLOOP. ENDMETHOD. ENDCLASS. |
Класс для передачи данных через кластер:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
class /Z/CL_MM_CLUSTDB_API definition public final create public . public section. class-methods IMPORT importing !IV_ID type INDX_SRTFD !IA_DATA type ANY returning value(RV_ERROR) type ABAP_BOOL . class-methods EXPORT importing !IV_ID type INDX_SRTFD exporting !EA_DATA type ANY returning value(RV_ERROR) type ABAP_BOOL . class-methods FREE importing !IV_ID type INDX_SRTFD returning value(RV_ERROR) type ABAP_BOOL . protected section. private section. constants MC_AREA type RELID value 'Z0' ##NO_TEXT. constants MC_TABNAME type CHAR4 value 'INDX' ##NO_TEXT. ENDCLASS. CLASS /Z/CL_MM_CLUSTDB_API IMPLEMENTATION. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Static Public Method /Z/CL_MM_CLUSTDB_API=>EXPORT * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_ID TYPE INDX_SRTFD * | [<---] EA_DATA TYPE ANY * | [<-()] RV_ERROR TYPE ABAP_BOOL * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD export. *--------------------------------------------------------------------* * EXPORT *--------------------------------------------------------------------* * Экспорт данных из кластера *--------------------------------------------------------------------* IMPORT iv_id TO ea_data FROM DATABASE indx(z0) ID iv_id. rv_error = xsdbool( sy-subrc <> 0 ). ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Static Public Method /Z/CL_MM_CLUSTDB_API=>FREE * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_ID TYPE INDX_SRTFD * | [<-()] RV_ERROR TYPE ABAP_BOOL * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD free. *--------------------------------------------------------------------* * FREE *--------------------------------------------------------------------* * Удалить данные из кластера *--------------------------------------------------------------------* STATICS: mr_db TYPE REF TO cl_abap_expimp_db. IF mr_db IS NOT BOUND. mr_db = NEW #( ). ENDIF. TRY . mr_db->delete( EXPORTING tabname = mc_tabname client = sy-mandt area = mc_area id = iv_id client_specified = abap_true ). CATCH cx_sy_client. rv_error = abap_true. CATCH cx_sy_generic_key. rv_error = abap_true. CATCH cx_sy_incorrect_key. rv_error = abap_true. CATCH cx_root. rv_error = abap_true. ENDTRY. ENDMETHOD. * <SIGNATURE>---------------------------------------------------------------------------------------+ * | Static Public Method /Z/CL_MM_CLUSTDB_API=>IMPORT * +-------------------------------------------------------------------------------------------------+ * | [--->] IV_ID TYPE INDX_SRTFD * | [--->] IA_DATA TYPE ANY * | [<-()] RV_ERROR TYPE ABAP_BOOL * +--------------------------------------------------------------------------------------</SIGNATURE> METHOD import. *--------------------------------------------------------------------* * IMPORT *--------------------------------------------------------------------* * Импорт в кластер данных *--------------------------------------------------------------------* EXPORT iv_id = ia_data TO DATABASE indx(z0) ID iv_id. rv_error = xsdbool( sy-subrc <> 0 ). ENDMETHOD. ENDCLASS. |
Пример использования
Старт задачи:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
REPORT ZRUN_REPORT. CLASS lcl_job DEFINITION INHERITING FROM /z/cl_mm_job_wrapper. PROTECTED SECTION. METHODS: generate_job_name REDEFINITION. ENDCLASS. CLASS lcl_job IMPLEMENTATION. METHOD generate_job_name. rv_job_name = 'CUSTOM_JOB'. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA: lr_job_wrapper TYPE REF TO lcl_job. DATA: lt_buffer_data TYPE /z/cl_mm_job_wrapper=>mty_t_job_buffer. DATA: lt_mara TYPE TABLE OF mara. lt_mara = VALUE #( ( matnr = 1 ) ). APPEND VALUE #( id = 'ZMARA' data = REF #( lt_mara ) ) TO lt_buffer_data. lr_job_wrapper = NEW #( ). lr_job_wrapper->start_job( EXPORTING iv_report_name = 'ZTEST_SUBMIT' it_buffer_data = lt_buffer_data RECEIVING rv_count = DATA(lv_job_number) EXCEPTIONS cancelled = 1 OTHERS = 2 ). CHECK sy-subrc = 0. WRITE : / |Запуск фонового задания { lv_job_number }|. |
Обработка:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
REPORT ZTEST_SUBMIT. DATA: lt_mara TYPE TABLE OF mara. /z/cl_mm_clustdb_api=>export( EXPORTING iv_id = 'ZMARA' IMPORTING ea_data = lt_mara ). LOOP AT lt_mara ASSIGNING FIELD-SYMBOL(<ls_mara>). "... ENDLOOP. |