*INCLUDE WHICH CONTAINS ALL THE SUBROUTINE
*&---------------------------------------------------------------------*
*& Include          ZPARATEST_FORM
*&---------------------------------------------------------------------*
FORM file_help .
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4' "get file name on F4 help
* EXPORTING
*   PROGRAM_NAME        = SYST-REPID
*   DYNPRO_NUMBER       = SYST-DYNNR
*   FIELD_NAME          = ' '
*   STATIC              = ' '
*   MASK                = ' '
*   FILEOPERATION       = 'R'
*   PATH                =
    CHANGING
      file_name = p_file
*     LOCATION_FLAG       = 'P'
* EXCEPTIONS
*     MASK_TOO_LONG       = 1
*     OTHERS    = 2
    .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CREATE_MATERIAL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM create_material .
  DATA : lv_msg TYPE char80.
  DATA: lv_last_used_server TYPE rfcdes-rfcdest.
  DATA: lv_error TYPE xfeld.
  DATA: lv_sys_failure TYPE xfeld.
  DATA: lv_counter TYPE i.
  gv_task_name = gv_task_name + 1.
  DO.
    CALL FUNCTION 'ZPARALLEL_WRAPTER_FM'
      STARTING NEW TASK gv_task_name
      DESTINATION IN GROUP 'parallel_generators'
      PERFORMING result_capture ON END OF TASK
      EXPORTING
        im_mara_400           = lt_mara_temp
      EXCEPTIONS
        communication_failure = 1 MESSAGE lv_msg
        system_failure        = 2 MESSAGE lv_msg
        resource_failure       = 3 "
        custom_exception      = 4.
    CASE sy-subrc.
      WHEN 1 or 2 .
        lv_error = 'X'.
        " TO IDENTIFY THE SERVER LAST USED
        CALL FUNCTION 'SPBT_GET_PP_DESTINATION'
          IMPORTING
            rfcdest = lv_last_used_server.
        DATA lv_last_server TYPE  pbtresourc-servername.
        lv_last_server  = lv_last_used_server.
        "To Exclude the server from server group
        CALL FUNCTION 'SPBT_DO_NOT_USE_SERVER'
          EXPORTING
            server_name                 = lv_last_server
          EXCEPTIONS
            invalid_server_name         = 1
            no_more_resources_left      = 2
            pbt_env_not_initialized_yet = 3
            OTHERS                      = 4.
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.
      WHEN 3.
        lv_error = 'X'.
        "Exception Handling
        IF lv_sys_failure IS INITIAL.
          lv_sys_failure = 'X'.
          WAIT UNTIL gv_rec_task >= gv_send_task UP TO 1 SECONDS.
        ELSE.
          WAIT UNTIL gv_rec_task >= gv_send_task UP TO 3 SECONDS.
          IF sy-subrc EQ 0. " If all the tasks are recieved then sy-subrc is 0. i.e. it's temeporary failure.
            CLEAR lv_sys_failure.
          ELSE.
            "Create some counter and check for some predefined times and then come out from the loop and come out from the program.
            lv_counter = lv_counter + 1.
            IF lv_counter = 5.
              EXIT.
              DATA(gv_error) = abap_true.
            ENDIF.
          ENDIF.
        ENDIF.
      WHEN 4.
        lv_error = 'X'.
      WHEN 0.
        gv_send_task = gv_send_task + 1.
        CLEAR lv_error.
    ENDCASE.
    IF lv_error IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.
ENDFORM.
FORM result_capture USING gv_task_name.
  DATA: lt_msg TYPE bapiret2_t.
  RECEIVE RESULTS FROM FUNCTION 'ZPARALLEL_WRAPTER_FM'
        IMPORTING et_msg = lt_msg
          EXCEPTIONS
            communication_failure = 1
            system_failure = 2.
  gv_rec_task = gv_rec_task + 1.
  IF sy-subrc EQ 0.
    APPEND LINES OF lt_msg TO gt_msg.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form INTIALIZE_SERVER_GROUP
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM intialize_server_group .
  CALL FUNCTION 'SPBT_INITIALIZE'
    EXPORTING
      group_name                     = 'parallel_generators'
    IMPORTING
      max_pbt_wps                    = gv_tot_wp
      free_pbt_wps                   = gv_avial_wp
    EXCEPTIONS
      invalid_group_name             = 1
      internal_error                 = 2
      pbt_env_already_initialized    = 3
      currently_no_resources_avail   = 4
      no_pbt_resources_found         = 5 "MINIMUM 3 WP
      cant_init_different_pbt_groups = 6 "WONT' BE ABLE TO INITIALIZE ANOTHER SERVER GROUP
      OTHERS                         = 7.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.