Modern SAP applications often need to execute time-consuming operations such as external service calls, heavy calculations, or mass data creation. Running such logic sequentially can slow down user interaction and block dialog processes. ABAP Cloud offers a clean and powerful way to distribute workload using parallel processing via the class CL_ABAP_PARALLEL.
In this blog, we will see:
A parallel class is a normal ABAP class that implements the interface IF_ABAP_PARALLEL.
This interface provides the method: IF_ABAP_PARALLEL-DO
which represents the logic that will run in a separate work process. Each instance of this class becomes an independent task executed by the framework.
The advantages are:
Step 1: Create parallel task class
This class:
CLASS zsrt_cl_parallel_task DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_serializable_object . " Required for parallel execution
INTERFACES if_abap_parallel . " Enables parallel processing logic
METHODS :
constructor IMPORTING iv_material TYPE string, " Receive input material
get_result RETURNING VALUE(rt_result) TYPE string. " Return generated UUID
PRIVATE SECTION.
DATA: mv_material TYPE string. " Stores material passed to task
DATA mv_result TYPE string. " Stores generated Task UUID
ENDCLASS.
CLASS zsrt_cl_parallel_task IMPLEMENTATION.
METHOD constructor.
mv_material = iv_material. " Initialize material for this task instance
ENDMETHOD.
METHOD get_result.
RETURN mv_result. " Return UUID result to caller
ENDMETHOD.
METHOD if_abap_parallel~do.
DATA:
lt_task_create TYPE TABLE FOR CREATE zsrt_i_task_head, " Input structure for CREATE
ls_task_create LIKE LINE OF lt_task_create,
lt_reported TYPE RESPONSE FOR REPORTED EARLY zsrt_i_task_head,
lt_mapped TYPE RESPONSE FOR MAPPED EARLY zsrt_i_task_head, " Holds generated values (UUID)
lt_failed TYPE RESPONSE FOR FAILED EARLY zsrt_i_task_head. " Holds failed entries
" Prepare Task data using material
lt_task_create = VALUE #(
(
%cid = 'MY_CID_1' " Correlation ID
taskid = |TASK-{ mv_material }| " Dynamic Task ID
title = |New task for { mv_material }| " Task title
description = 'Test Description'
overallstatus = 'O'
%control = VALUE #( " Mark fields for update
taskid = if_abap_behv=>mk-on
title = if_abap_behv=>mk-on
description = if_abap_behv=>mk-on
overallstatus = if_abap_behv=>mk-on )
)
).
" Create Task record using RAP EML
MODIFY ENTITIES OF zsrt_i_task_head
ENTITY zsrt_i_task_head
CREATE FROM lt_task_create
MAPPED lt_mapped " Contains generated UUID
FAILED lt_failed
REPORTED lt_reported.
" Process only if creation is successful
IF lt_failed IS INITIAL.
COMMIT ENTITIES. " Commit inside parallel task (separate LUW)
" Read generated UUID from mapped structure
READ TABLE lt_mapped-zsrt_i_task_head INTO DATA(ls_map) INDEX 1.
IF sy-subrc = 0.
mv_result = ls_map-taskuuid. " Store UUID for returning
ENDIF.
ENDIF.
ENDMETHOD.
ENDCLASS.
Key Points:
Step 2: Calling the Parallel Class
To keep the example simple, we call it from a local class implementing IF_OO_ADT_CLASSRUN.
Execution Flow
CLASS zsrt_cl_process DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun. " Entry point for execution in ADT
ENDCLASS.
CLASS zsrt_cl_process IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA:
lt_processes TYPE cl_abap_parallel=>t_in_inst_tab, " Holds parallel task instances
lt_results TYPE STANDARD TABLE OF sysuuid_x16. " Stores returned UUID results
" Read all Billing Items from DB table
SELECT *
FROM zsrt_bill_item
INTO TABLE @DATA(lt_items).
" Create one parallel task instance per item
LOOP AT lt_items INTO DATA(ls_item).
INSERT NEW zsrt_cl_parallel_task(
CONV #( ls_item-materialid ) " Pass material to parallel class
) INTO TABLE lt_processes.
ENDLOOP.
" Execute all tasks in parallel (max 5 at a time)
NEW cl_abap_parallel( p_num_tasks = 5 )->run_inst(
EXPORTING
p_in_tab = lt_processes " Input: task instances
IMPORTING
p_out_tab = DATA(lt_finished) " Output: completed instances
).
" Collect UUID results from each parallel execution
LOOP AT lt_finished INTO DATA(ls_finished).
DATA(lv_uuid) =
CAST zsrt_cl_parallel_task(
ls_finished-inst )->get_result( ). " Get UUID from each task
APPEND lv_uuid TO lt_results.
ENDLOOP.
DATA idx TYPE sy-tabix.
idx = 0.
" Update billing items with respective UUIDs
LOOP AT lt_items INTO ls_item.
idx = idx + 1.
READ TABLE lt_results INTO DATA(lv_uuid2) INDEX idx.
IF sy-subrc = 0.
MODIFY ENTITIES OF zsrt_i_bill_head
ENTITY zsrt_i_bill_item
UPDATE FROM VALUE #(
( billid = ls_item-billid " Key field
itemno = ls_item-itemno " Key field
description = lv_uuid2 " Store UUID
%control-description = if_abap_behv=>mk-on " Enable update
)
).
ENDIF.
ENDLOOP.
" Save all updates to database
COMMIT ENTITIES.
ENDMETHOD.
ENDCLASS.
Result:
Conclusion:
Parallel processing in ABAP Cloud enables faster and efficient handling of large data by executing tasks simultaneously. Using CL_ABAP_PARALLEL with RAP EML, we can process multiple records independently and improve performance. This approach ensures better scalability and optimal use of system resources for real-time business scenarios.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 73 | |
| 21 | |
| 21 | |
| 21 | |
| 20 | |
| 19 | |
| 18 | |
| 15 | |
| 15 | |
| 10 |