Learn SAP — Parallel Processing in ABAP programing

 
*&———————————————————————*
*& Report  Z_TEST_PARALLEL
*&
*&———————————————————————*
*&
*& In this parallel processing we select 100K and process 10K parallel
* and come back and select the next 100K
*&———————————————————————*
REPORT  z_test_parallel.

DATA:
      lt_but000 TYPE TABLE OF but000,
      ls_but000 TYPE but000,
      ls_last_but000 TYPE but000,
      l_spbt_initialize TYPE char1,
      srv_grp TYPE rfcgr,
      max_wps TYPE i,
      taskname(20),
      task_counter TYPE i,
      returned TYPE i,
      returned_bp TYPE TABLE OF but000,
      free_wps TYPE i,
      wa_packet LIKE but000 OCCURS 0 WITH HEADER LINE,
      l_con_counts TYPE int2,
      max_jobs  TYPE i VALUE 10,
      proc_num_limit TYPE i VALUE 10,
      pack_siz        TYPE i VALUE 10000,
      maxpack TYPE i VALUE 10000,
      running TYPE i,
wa_but000 TYPE but000.

* Select 100K at a time
DO.
  SELECT * INTO TABLE lt_but000 UP TO 10000 ROWS
    FROM but000 WHERE partner > ls_last_but000-partner
    ORDER BY partner.

  IF NOT lt_but000 IS INITIAL.
* Read the last record.
    DESCRIBE TABLE lt_but000.
    READ TABLE lt_but000 INTO ls_but000 INDEX sy-tfill.
    PERFORM process_bp_parallel.
  ELSE.
*   No records exit
    EXIT.
  ENDIF.

ENDDO.
*&———————————————————————*
*&      Form  PROCESS_BP_PARALLEL
*&———————————————————————*
*       text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM process_bp_parallel .

  FIELD-SYMBOLS: <lt_but000> TYPE but000.
  DATA:
      items_in_packet TYPE i.

  IF l_spbt_initialize IS INITIAL.

    CALL FUNCTION ‘SPBT_INITIALIZE’
      EXPORTING
        group_name                     = srv_grp
      IMPORTING
        max_pbt_wps                    = max_wps
        free_pbt_wps                   = free_wps
      EXCEPTIONS
        invalid_group_name             = 1
        internal_error                 = 2
        pbt_env_already_initialized    = 3
        currently_no_resources_avail   = 4
        no_pbt_resources_found         = 5
        cant_init_different_pbt_groups = 6
        OTHERS                         = 7.
    CASE sy-subrc.
      WHEN 0.
      WHEN OTHERS.
        WRITE: / ‘Error  Please check ‘.
    ENDCASE.
    l_spbt_initialize = ‘X’.
  ELSE.
    CALL FUNCTION ‘SPBT_GET_CURR_RESOURCE_INFO’
      IMPORTING
        max_pbt_wps                 = max_wps
        free_pbt_wps                = free_wps
      EXCEPTIONS
        internal_error              = 1
        pbt_env_not_initialized_yet = 2
        OTHERS                      = 3.

    CASE sy-subrc.
      WHEN 0.
      WHEN 1.
        WRITE: / ‘Internal Error'(090), srv_grp.
        EXIT.
      WHEN 2.
        WRITE / ‘PBT environment not initialized for group.'(091).
        EXIT.
      WHEN 3.
        WRITE: / ‘Fatal Error:Others.'(092).
        EXIT.
    ENDCASE.

  ENDIF.
  DESCRIBE TABLE lt_but000 LINES l_con_counts.

  IF max_jobs IS INITIAL.
    proc_num_limit = free_wps.
    pack_siz = l_con_counts / free_wps.
  ELSE.
    proc_num_limit = max_jobs.
    pack_siz = l_con_counts / max_jobs.
  ENDIF.
  IF pack_siz > maxpack AND maxpack NE 0.
    pack_siz = maxpack.
  ENDIF.

  IF pack_siz = 0.
    pack_siz = l_con_counts.
  ENDIF.

*Loop until all data is placed into a work packets and assigned to aRFC process
  LOOP AT lt_but000 ASSIGNING <lt_but000>.
*if maximum number has been reached, wait until some running processes
*have completed and released
    IF running >= proc_num_limit.
      WAIT UNTIL running < proc_num_limit.
    ENDIF.
*fill wa_packet with contract data and spawn aRFC process
    MOVE-CORRESPONDING <lt_but000> TO wa_packet.
    APPEND wa_packet.
    ADD 1 TO items_in_packet.
    IF items_in_packet => pack_siz.
      PERFORM spawn_arfc_bp_process TABLES wa_packet.
      CLEAR: items_in_packet, wa_packet, wa_packet[].
    ENDIF.
  ENDLOOP.

  IF items_in_packet > 0. ” Any remaining data
    PERFORM spawn_arfc_bp_process TABLES wa_packet.
    CLEAR: items_in_packet, wa_packet, wa_packet[].
  ENDIF.
  FREE: lt_but000, wa_packet.

  WAIT UNTIL running = 0. ” Wait until all the process is completed

ENDFORM.                    ” PROCESS_BP_PARALLEL

*&———————————————————————*
*&      Form  SPAWN_ARFC_BP_PROCESS
*&———————————————————————*
*       text
*———————————————————————-*
*      –>P_WA_PACKET  text
*———————————————————————-*
FORM spawn_arfc_bp_process  TABLES   p_wa_packet STRUCTURE wa_packet.
  DATA: rfcdest LIKE rfcdes-rfcdest,
        v_servername LIKE pbtresourc-servername.
  CLEAR taskname.

  ADD 1 TO task_counter.
  taskname = ‘abcd_’ + task_counter.
  DO.
    CALL FUNCTION ‘Z_TEST_PARALLEL_FUNC’
      STARTING NEW TASK taskname
      DESTINATION IN GROUP srv_grp
      PERFORMING return_back_this_task
      ON END OF TASK
      EXPORTING
        lt_but000             = p_wa_packet
      EXCEPTIONS
        resource_failure      = 1
        system_failure        = 2
        communication_failure = 3
        OTHERS                = 4.

   if sy-subrc = 0.

    running = running + 1.

  endif.
  ENDDO.

ENDFORM.                    ” SPAWN_ARFC_BP_PROCESS
*&———————————————————————*
*&      Form  RETURN_BACK_THIS_TASK
*&———————————————————————*
*       text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM return_back_this_task  USING taskname.

  returned = returned + 1.
  running = running – 1.
*$*$   RECEIVE RESULTS FROM aRFC calls
  RECEIVE RESULTS FROM FUNCTION ‘Z_TEST_PARALLEL_FUNC’
      IMPORTING
        lt_but000_o             = returned_bp
      EXCEPTIONS
      resource_failure = 1
      system_failure   = 2
      communication_failure = 3
      OTHERS = 4.
  CASE sy-subrc.
    WHEN 0.
      ” This task is scussess.
    WHEN 1.

      proc_num_limit = running.
    WHEN 2.

    WHEN 3.

    WHEN OTHERS.
      ” Error
  ENDCASE.

ENDFORM.                    ” RETURN_BACK_THIS_TASK

Hope this example helps. Let us work on different example in another post.

Advertisements

10 thoughts on “Learn SAP — Parallel Processing in ABAP programing

  1. I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work. Look forward to reading more from you in the future.

  2. Pretty good post. I just came across your site and wanted to say that I’ve really enjoyed reading your posts. In any case I’ll be subscribing to your feed and I hope you will keep a good work!

  3. Hi,

    I am also an ABAP CRM developer and have received via friend indication your blog link.

    I really have appreciate your initiative to share with the globe what you have learned.

    Congratulations.

    Your blog is preatty cool !

    Regards,

    Rodolfo Miao
    Sao Paulo – Brazil.

  4. Hi, after read your code, I cant’t understand the variant of ‘running’
    you do not give value to it, but only use ‘running = running – 1.’
    how can this work?

  5. CALL FUNCTION ‘Z***(your function name)’
    STARTING NEW TASK taskname
    DESTINATION IN GROUP srv_grp
    PERFORMING return_back_this_task
    ON END OF TASK
    EXPORTING
    This is correct this is parallel processing as in the new task will start in a new LUW with the main program running in another LUW this method is mainly used to combine different functionalities to run simultaneously when they are unrelated …

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s