Posted: Sat Sep 13, 2008 7:31 pm Post subject: ABAP asynchronous RFC II - parallel processes
Author: Holger Pakirnus
Simple example for parallel processes - do NOT forget to check at FMs attributes
Code:
REPORT z_hp_test_711 NO STANDARD PAGE HEADING.
* DEMO asynchronous RFC II
* PARALLEL PROCESSEING
* define FUNCTION at the and seperately - check REMOTE at attrib.
* TYPES ****************************************************************
TYPES: BEGIN OF t_task_line,
task TYPE char8,
started TYPE syuzeit,
restart TYPE syuzeit,
ready TYPE syuzeit,
END OF t_task_line.
* DATA ************************************************
DATA: max_tasks TYPE i.
DATA: free_tasks TYPE i.
DATA: gl_no_of_tasks TYPE i.
DATA: gl_done TYPE xfeld.
DATA: gv_task TYPE char8.
DATA: save_tabix TYPE sytabix.
DATA: starttime TYPE syuzeit.
DATA: endtime TYPE syuzeit.
DATA: gt_tasks TYPE STANDARD TABLE OF t_task_line WITH HEADER LINE.
DATA: gt_out TYPE STANDARD TABLE OF t_task_line WITH HEADER LINE.
* SELECTION-SCREEN (PARAMETERS/SELECT OPTIONS) *************************
PARAMETERS: proc1 TYPE i DEFAULT 1.
PARAMETERS: proc2 TYPE i DEFAULT 2.
PARAMETERS: proc3 TYPE i DEFAULT 3.
PARAMETERS: proc4 TYPE i DEFAULT 4.
PARAMETERS: proc5 TYPE i DEFAULT 5.
PARAMETERS: maxtime TYPE i DEFAULT 9.
************************************************************************
START-OF-SELECTION.
starttime = sy-uzeit.
endtime = starttime + maxtime.
PERFORM rfc_info_get .
IF free_tasks LT 5.
MESSAGE a010(ad) WITH 'less than 5 processes available' .
* & & & &
ENDIF.
* free_tasks = free_tasks / 3.
* if free_tasks > p_maxtsk.
* free_tasks = p_maxtsk.
* endif.
* DO free_tasks time.
PERFORM process_start USING 'P1' proc1.
PERFORM process_start USING 'P2' proc2.
PERFORM process_start USING 'P3' proc3.
PERFORM process_start USING 'P4' proc4.
PERFORM process_start USING 'P5' proc5.
* ENDDO.
************************************************************************
END-OF-SELECTION.
WAIT UNTIL gl_no_of_tasks IS INITIAL..
SORT gt_out.
LOOP AT gt_out.
WRITE: / gt_out-task, gt_out-started,
gt_out-restart, gt_out-ready.
ENDLOOP.
************************************************************************
FORM handler USING value(p_taskname).
RECEIVE RESULTS FROM FUNCTION 'Z_DGF_WAIT_N'
IMPORTING e_done = gl_done
EXCEPTIONS
communication_failure = 1 "MESSAGE msg
system_failure = 2. "MESSAGE msg.
IF sy-subrc NE 0.
LEAVE PROGRAM.
ENDIF.
gl_no_of_tasks = gl_no_of_tasks - 1.
gv_task = p_taskname.
READ TABLE gt_tasks WITH KEY task = p_taskname.
gt_tasks-ready = sy-uzeit.
MOVE-CORRESPONDING gt_tasks TO gt_out.
APPEND gt_out.
CHECK sy-uzeit <= endtime.
gt_tasks-restart = sy-uzeit.
CASE p_taskname.
WHEN 'P1'.
PERFORM process_start USING 'P1' proc1.
WHEN 'P2'.
PERFORM process_start USING 'P2' proc2.
WHEN 'P3'.
PERFORM process_start USING 'P3' proc3.
WHEN 'P4'.
PERFORM process_start USING 'P4' proc4.
WHEN 'P5'.
PERFORM process_start USING 'P5' proc5.
ENDCASE.
* SET USER-COMMAND 'REFR'.
ENDFORM. " HANDLER
*&---------------------------------------------------------------------*
*& Form rfc_info_get
*&---------------------------------------------------------------------*
FORM rfc_info_get .
CALL FUNCTION 'SPBT_INITIALIZE'
* EXPORTING
* GROUP_NAME = ' '
IMPORTING
max_pbt_wps = max_tasks
free_pbt_wps = free_tasks
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. "ok
WHEN 3.
CALL FUNCTION 'SPBT_GET_CURR_RESOURCE_INFO'
IMPORTING
max_pbt_wps = max_tasks
free_pbt_wps = free_tasks
EXCEPTIONS
internal_error = 1
pbt_env_not_initialized_yet = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
WHEN OTHERS.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDCASE.
ENDFORM. " rfc_info_get
*&---------------------------------------------------------------------*
*& Form process_start
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0066 text
* -->P_PROC2 text
*----------------------------------------------------------------------*
FORM process_start USING value(p_name)
p_time.
DATA: txt40 TYPE text40.
CLEAR txt40.
CONCATENATE 'starting: ' p_name INTO txt40.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
* PERCENTAGE = 0
text = txt40.
CALL FUNCTION 'Z_DGF_WAIT_N'
STARTING NEW TASK p_name
DESTINATION IN GROUP DEFAULT
PERFORMING handler ON END OF TASK
EXPORTING
i_waittime = p_time
EXCEPTIONS
communication_failure = 1.
CHECK sy-subrc EQ 0.
gl_no_of_tasks = gl_no_of_tasks + 1.
READ TABLE gt_tasks WITH KEY task = p_name.
IF sy-subrc NE 0.
CLEAR gt_tasks.
gt_tasks-task = p_name.
gt_tasks-started = sy-uzeit.
APPEND gt_tasks.
ELSE.
save_tabix = sy-tabix.
gt_tasks-restart = sy-uzeit.
MODIFY gt_tasks INDEX save_tabix.
ENDIF.
ENDFORM. " process_start
*FUNCTION z_dgf_wait_n.
**"---------------------------------
**-------------------------------------
**"*"Lokale Schnittstelle:
**" IMPORTING
**" VALUE(I_WAITTIME) TYPE I DEFAULT 10
**" EXPORTING
**" VALUE(E_DONE) TYPE BOOLEAN
**"---------------------------------
**-------------------------------------
* TABLES indx.
* IF i_waittime GE 3600. "1h
* i_waittime = 1.
* ENDIF.
* IF i_waittime LE 1.
* i_waittime = 1.
* ENDIF.
*
* WAIT UP TO i_waittime SECONDS.
* e_done = 'X'.
*
*ENDFUNCTION.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.