Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
kevin_wilson2
Contributor
0 Kudos

I will step through each area of the code describing what I'm doing. 

The situation: We have an internal table LT_DATA that is used to loop and build up BDC related data. At the end of VBELN we have all the data in our BDC table to be called. We need the program to wait and display the result of the called BDC.

STEP 1: The LC_PER constant stores the percentage value of unused work processes to make available to our report.
constants: lc_per TYPE numc2 value '75'.

data: lv_free_threads     TYPE i,
      lv_thread           TYPE numc2,
      lv_thread_count     TYPE i,
      gv_active_threads   TYPE i.

STEP 2: Get the number of free work processes
*** Get Number of Free Processes.
  CALL FUNCTION 'SPBT_INITIALIZE'
    IMPORTING
      free_pbt_wps                   = lv_free_threads
    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.

  IF sy-subrc <> 0.
*** Issue error message
    LEAVE LIST-PROCESSING.
  ENDIF.

STEP 3: Calculate how many sessions we'd like to run in parallel
*** Determine no of Free threads to be used. Based on % of available
  lv_thread_count =  lv_free_threads * lc_per / 100 .

  lv_thread         = 0.
  gv_active_threads = 0.

STEP 4: Loop through the data building your BDC data
*** Loop through data
LOOP at lt_data.

  perform build_BDC tables it_bdctab.

STEP 5: At the end of VBELN and at the time to call the BDC insert our code
*** Do your process at end of Order Number
  AT END OF VBELN.

STEP 6: Perform a DO loop. Since we don't know how many times we need to call it and since we need to control how many sessions we generate
        DO.

STEP 7: Increment the number of threads we are currently using
          ADD 1 TO gv_active_threads.

STEP 8: Check if the number of threads we are using is still within our limit.
          IF gv_active_threads <= lv_thread_count.

STEP 9: Give the thread a unique number and call the tRFC. Notice we perform a subroutine at the end of the task. In this subroutine we'll decrement the number of active threads we are using
            add 1 to lv_thread.

* Call remotely enable function in update task. In this function we call the normal BDC transaction.
            CALL FUNCTION 'ZISD_CALL_TRANSACTION'
              STARTING NEW TASK lv_thread
              DESTINATION IN GROUP DEFAULT
              PERFORMING update_order ON END OF TASK
              EXPORTING
                iv_transaction    = 'VA01'
                iv_mode           = 'N'
                iv_update         = 'A'
              TABLES
                it_bdctab         = it_bdctab
                et_bdcmsg         = it_bdcmsg
              EXCEPTIONS
                not_a_valid_tcode = 1
                bdc_data_empty    = 2
                OTHERS            = 3.

            IF sy-subrc <> 0.

STEP 10: If the tRFC fails then try again but first decrement the number of used active threads. Beware not to entire an infite loop if an error is issued. Only do this if it's a technical error otherwise you should exit the DO loop in this case as well. I received SY-SUBRC = 3 on occasion so I implemented this code to retry the execution which resolved the issue.
              SUBTRACT 1 FROM gv_active_threads.   "Try again if it fails

            ELSE.

              EXIT.

            ENDIF.

          ELSE.

            SUBTRACT 1 FROM gv_active_threads.

          ENDIF.

        ENDDO.


  ENDAT.

ENDLOOP.

IF sy-subrc = 0.

STEP 11: Wait until all active threads are completed. This number is decremented in the subroutine below and incremented each time the RFC is called.
* Wait till all threads are completed.
    WAIT UNTIL gv_active_threads = 0.

    PERFORM display_results.

    COMMIT WORK AND WAIT.

ENDIF.

STEP 12: On return of the call BDC we decrement the number of active threads counter. When it's zero the program will execute.
******* SUBROUTINES
FORM update_order USING name.

  DATA: lv_order TYPE vbeln.

* Get Response from Threads.
  RECEIVE RESULTS FROM FUNCTION 'ZISD_CALL_TRANSACTION'
     IMPORTING
      et_br       = lv_order.  "Now you have the order to display

  SUBTRACT 1 FROM gv_active_threads.

ENDFORM.                    " UPDATE_ORDER

2 Comments