SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Parallel Asynchronous Call of RFC Functions



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Mon Feb 25, 2008 12:47 pm    Post subject: Parallel Asynchronous Call of RFC Functions Reply with quote

Code:
REPORT ZEXP_ASYNC_RFC NO STANDARD PAGE HEADING.

****************************************************
* Test Report: Parallel Asynchronous Call of RFC Functions
*
*   This is freeware. Please read the terms of use.
*   Written by Urs Rohner, Rohner IT Consulting & Engineering
*
*  Contact Author: Urs Rohner
****************************************************
  TYPES:
    " task list
    BEGIN OF T_TASKLIST_E,
      NAME(4)  TYPE C,
      DEST     LIKE RFCSI-RFCDEST,
      T1       TYPE TIMESTAMPL,
      T2       TYPE TIMESTAMPL,
    END OF T_TASKLIST_E,
    T_TASKLIST TYPE T_TASKLIST_E OCCURS 0.

  TYPES:
    " globals aRFC data container
    BEGIN OF T_ARFC_GLOBAL,
      RCV_JOBS TYPE I,
      TASKS    TYPE T_TASKLIST,
    END OF T_ARFC_GLOBAL.

  DATA:
    " shared globals, needed for aRFC callback form
    _ARFC TYPE T_ARFC_GLOBAL.

*eject
*
* P B O ( )
*
  DATA:
    " just for display
    _TASK TYPE T_TASKLIST_E,
    _T1   TYPE TIMESTAMPL,
    _T2   TYPE TIMESTAMPL,
    _T    TYPE I,
    _TP   TYPE I,
    _TS   TYPE I.

  START-OF-SELECTION.
    " call functions
    GET TIME STAMP FIELD _T1.
    PERFORM M_DISPATCHER USING _ARFC.
    GET TIME STAMP FIELD _T2.
    _TS = ( _T2 - _T1 ) * 1000.

    " show results
    LOOP AT _ARFC-TASKS[] INTO _TASK.
      _T = ( _TASK-T2 - _TASK-T1 ) * 1000.
      ADD _T TO _TP.
      WRITE: / _TASK-NAME, _TASK-DEST, _T.
    ENDLOOP.
    WRITE / _TP.
    WRITE / _TS.

*eject
* call function asynchronously

  FORM M_DISPATCHER USING ARFC_ TYPE T_ARFC_GLOBAL.

    DATA:
      _T              TYPE TIMESTAMPL,
      _TASK           TYPE T_TASKLIST_E,
      _JOBS           TYPE I VALUE 10,      " number of parallel jobs
      _SND_JOBS       TYPE I VALUE 1,       " forked jobs
      _EXCP_FLAG(1)   TYPE C,               " rfc problem flag
      _TASKNAME(4)    TYPE N VALUE '0001'.  " task name creation

    DO. " loops _jobs times

      GET TIME STAMP FIELD _T.
      CALL FUNCTION 'RFC_SYSTEM_INFO'
        STARTING NEW TASK _TASKNAME DESTINATION IN GROUP DEFAULT
        PERFORMING M_RETURN_INFO ON END OF TASK
        EXCEPTIONS
          COMMUNICATION_FAILURE = 1
          SYSTEM_FAILURE        = 2
          RESOURCE_FAILURE      = 3.

      CASE SY-SUBRC.
        WHEN 0.
          " create new task
          _TASK-NAME = _TASKNAME.
          _TASK-T1   = _T.
          CLEAR _TASK-DEST.
          APPEND _TASK TO ARFC_-TASKS[].
          ADD 1 TO _TASKNAME.
          ADD 1 TO _SND_JOBS.
          SUBTRACT 1 FROM _JOBS.
          IF _JOBS IS INITIAL.
            EXIT.
          ENDIF.
        WHEN 1 OR 2.
          " error handling should be placed here
        WHEN 3.
          " not enough work processed
          IF _EXCP_FLAG IS INITIAL.
            _EXCP_FLAG = 'X'.
            " 1st retry
            WAIT UNTIL ARFC_-RCV_JOBS GE _SND_JOBS UP TO '1' SECONDS.
          ELSE.
            " 2nd retry
            WAIT UNTIL ARFC_-RCV_JOBS GE _SND_JOBS UP TO '5' SECONDS.
          ENDIF.
          IF SY-SUBRC IS INITIAL.
            CLEAR _EXCP_FLAG.
          ELSE.
            " did not get a response, handle error here
          ENDIF.
      ENDCASE.
    ENDDO.
    " wait for still pending tasks
    WAIT UNTIL ARFC_-RCV_JOBS GE _SND_JOBS.
  ENDFORM.

*eject
*
* aRFC Callback Form
*
  FORM M_RETURN_INFO USING TASKNAME_.

    DATA:
      _T    TYPE TIMESTAMPL,
      _X    LIKE SY-TABIX,
      _TASK TYPE T_TASKLIST_E,
      _INFO LIKE RFCSI.

    GET TIME STAMP FIELD _T.
    RECEIVE RESULTS FROM FUNCTION 'RFC_SYSTEM_INFO'
      IMPORTING  RFCSI_EXPORT = _INFO
      EXCEPTIONS
         COMMUNICATION_FAILURE = 1
         SYSTEM_FAILURE        = 2.

    ADD 1 TO _ARFC-RCV_JOBS.
    IF NOT SY-SUBRC IS INITIAL.
      " do error handling here
    ELSE.
      READ TABLE _ARFC-TASKS[] INTO _TASK " global access!
        WITH KEY NAME = TASKNAME_.
      IF SY-SUBRC IS INITIAL.
        _X = SY-TABIX.
        _TASK-T2   = _T.
        _TASK-DEST = _INFO-RFCDEST.
        MODIFY _ARFC-TASKS[] FROM _TASK INDEX _X.
      ENDIF.
    ENDIF.
  ENDFORM.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
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.