关于QRFC我也没细研究,有兴趣的可以看看几个标准demo程序:


RSTRFCT0

RSTRFCT1

RSTRFCQ4

report rstrfct0 NO STANDARD PAGE HEADING.

 

TYPE-POOLS:

  abap.

 

TYPES number_type TYPE c LENGTH 5.

 

PARAMETERS: qname   TYPE trfcqout-qname default 'BASIS_TEST_Q1',

            rfcdest TYPE rfcdes-rfcdest default 'TRAN_TEST',

            ncall   TYPE number_type default '00001',

            nluw    TYPE number_type default '00001',

            nosend  TYPE abap_bool default abap_false,

            separate TYPE abap_bool default abap_false,

            updtask  TYPE abap_bool default abap_false.

 

PARAMETERS: display  TYPE abap_bool default abap_true no-DISPLAY.

 

TYPES: tcpicLineTab TYPE STANDARD TABLE OF abaptext.

 

TYPES: BEGIN OF displayLine,

        dest TYPE rfcdest,

        tid  TYPE arfctid,

        xcall TYPE i,        " the x.luw

       END OF displayLine.

 

TYPES: displayLineTab TYPE STANDARD TABLE OF displayLine.

 

*----------------------------------------------------------------------*

*       CLASS lcl_main DEFINITION

*----------------------------------------------------------------------*

CLASS lcl_main DEFINITION FINAL .

 

  PUBLIC SECTION.

 

    CLASS-METHODS: run.

 

    METHODS: constructor

              IMPORTING i_qname TYPE trfcqout-qname

                        i_dest  TYPE rfcdes-rfcdest

                        i_ncall TYPE number_type

                        i_nluw  TYPE number_type

                        i_nosend TYPE abap_bool

                        i_separate TYPE abap_bool

                        i_updtask TYPE abap_bool

                        i_display TYPE abap_bool,

 

             create_luws_on_commit.

 

  PRIVATE SECTION.

 

    DATA: l_qname TYPE trfcqout-qname,

          l_dest  TYPE rfcdes-rfcdest,

          l_ncall TYPE number_type,

          l_nluw  TYPE number_type,

          l_nosend TYPE abap_bool,

          l_separate TYPE abap_bool,

          l_updtask TYPE abap_bool,

          l_display TYPE abap_bool.

 

    DATA: lt_result TYPE TABLE OF displayLine .

 

    METHODS: fill_tcpic_table

              IMPORTING i_len type I

                        i_qname type trfcqout-qname

              CHANGING  it_tcpic type tcpicLineTab,

 

              get_tid

                IMPORTING i_dest TYPE rfcdest

                RETURNING VALUE(e_tid) TYPE arfctid,

 

              append_to_result_tab

                IMPORTING x_call type sy-index,

 

              display_result

                IMPORTING x_luw TYPE sy-index

                          i_subrc TYPE abap_bool

                          i_display type abap_bool.

ENDCLASS.

 

*----------------------------------------------------------------------*

*       CLASS lcl_main IMPLEMENTATION

*----------------------------------------------------------------------*

CLASS lcl_main IMPLEMENTATION.

 

 

*  **********************************************************************

*   METHOD    :  run

*   Created   :  07.11.2007 13:14:07

*  *********************************************************************

  METHOD run .

 

    DATA: l_appl TYPE REF TO lcl_main.

 

    CREATE OBJECT l_appl

      EXPORTING i_qname = qname

                i_dest  = rfcdest

                i_ncall = ncall

                i_nluw  = nluw

                i_nosend = nosend

                i_separate = separate

                i_updtask = updtask

                i_display = display.

 

    l_appl->create_luws_on_commit( ).

 

  ENDMETHOD. "run

 

*  **********************************************************************

*   METHOD    :  constructor

*   Created   :  07.11.2007 15:38:04

*  *********************************************************************

  METHOD constructor .

 

*   initial the destination

    me->l_qname    = i_qname.

    me->l_dest     = i_dest.

    me->l_ncall    = i_ncall.

    me->l_nluw     = i_nluw.

    me->l_nosend   = i_nosend.

    me->l_separate = i_separate.

    me->l_updtask  = i_updtask.

    me->l_display = i_display.

 

  ENDMETHOD. "constructor

 

 

*  **********************************************************************

*   METHOD    :  fill_tcpic_table

*   Created   :  07.11.2007 15:38:31

*  the method is used to initial the table which will be used in the function call

*  *********************************************************************

  METHOD fill_tcpic_table .

 

    DATA: linenr type number_type,

          etext TYPE abaptext value '          Line No.:           '. "#EC NOTEXT

 

    if i_len = 0.

      RETURN.

    endif.

 

    etext = i_qname.

    etext 40(32) = '12345678901234567890123456789012'.

 

    do i_len TIMES.

 

      linenr = linenr   1.

      etext 20(6) = linenr.

      APPEND etext to it_tcpic.

 

    enddo.

 

  ENDMETHOD. "fill_tcpic_table

 

 

* **********************************************************************

*   METHOD    :  create_luws_commit1

*   Created   :  07.11.2007 15:32:18

*   Call TRFC_QUEUE_INITIALIZE is neccessary because working with

*   "Mischbetrieb" (QRFC and TRFC in one LUW) and the call is a TRFC-call

*  *********************************************************************

  METHOD create_luws_on_commit .

 

    DATA: l_len    TYPE I VALUE 1.

 

    DATA: lt_tcpic TYPE tcpicLineTab.

 

    DATA: r_subrc TYPE abap_bool.

 

    r_subrc = abap_true.

 

    fill_tcpic_table( EXPORTING i_len    = l_len

                                i_qname  = me->l_qname

                      CHANGING  it_tcpic = lt_tcpic ).

 

    DO me->l_nluw  TIMES.

 

*     Update task and qRFC in a commit work

      if me->l_updtask = abap_true.

 

        CALL FUNCTION 'STFC_CALL_QRFC' IN UPDATE TASK

          EXPORTING

            QNAME         = me->l_qname

            DEST          = me->l_dest

            NCALL         = 1

            NOSEND        = me->l_nosend

            QMESS         = space .

 

      endif.

 

      DO me->l_ncall  TIMES.

 

*       Set Queue Name for the next qRFC-call

        CALL FUNCTION 'TRFC_SET_QUEUE_NAME'

          EXPORTING

            QNAME                    = me->l_qname

            NOSEND                   = me->l_nosend

         EXCEPTIONS

           INVALID_QUEUE_NAME       = 1

           OTHERS                   = 2 .

 

        if sy-subrc <> 0.

          r_subrc = abap_false.

          exit.

        endif.

 

*       The call has the same TID like the TID of the previous call.

        if me->l_separate = abap_false.

 

          CALL FUNCTION 'STFC_WRITE_TO_TCPIC'

            in BACKGROUND TASK

            DESTINATION me->l_dest

            TABLES

              TCPICDAT            = lt_tcpic.

 

        else.

*         The call will be assigned with a new TID

          CALL FUNCTION 'STFC_WRITE_TO_TCPIC'

            in BACKGROUND TASK

            AS SEPARATE UNIT

            DESTINATION me->l_dest

            TABLES

              TCPICDAT            = lt_tcpic.

 

        endif.

 

*       append the created QRFC luw in the result table

        append_to_result_tab( x_call = sy-index ).

 

      ENDDO.

 

      COMMIT WORK.

 

      display_result( x_luw = sy-index

                      i_subrc = r_subrc

                      i_display = me->l_display ).

 

 

    ENDDO.

 

  ENDMETHOD. "create_luws_commit1

 

 

*  **********************************************************************

*   METHOD    :  get_tid

*   Created   :  07.11.2007 15:07:31

*   get the TID of the created LUW

*  *********************************************************************

  METHOD get_tid .

 

    CALL FUNCTION 'ID_OF_BACKGROUNDTASK'

     EXPORTING

       DEST          = i_dest

     IMPORTING

       TID           = e_tid .

 

  ENDMETHOD. "get_tid

 

 

*  **********************************************************************

*   METHOD    :  display_result

*   Created   :  07.11.2007 15:37:49

*  *********************************************************************

  METHOD display_result .

 

    FIELD-SYMBOLS  TYPE displayLine.

 

    if i_display = abap_true.

 

      if i_subrc = abap_false.

        WRITE: / 'Error while calling of TRFC_SET_QUEUE_NAME'.   "#EC NOTEXT

        RETURN.

      endif.

 

      write: / 'LUW-No. :', x_luw. "#EC NOTEXT

      write: / 'No. of Calls :', me->l_ncall, '  Queue Name:', me->l_qname. "#EC NOTEXT

      skip 1.

 

      WRITE: at /3(32) 'Destination',  36(24)'TID', 66 'x.call'.   "#EC NOTEXT

      WRITE: / '-----------------------------------------------------------------------------'.

      LOOP AT me->lt_result ASSIGNING  .

 

        if  IS ASSIGNED.

          WRITE: AT /3(32) -dest, 36(24) -tid, 66 -xcall.

        endif.

 

      ENDLOOP.

      refresh me->lt_result.

      SKIP 2.

 

    else.

 

      SORT lt_result BY tid xcall.

*     export the result table, thus the unittest can retrieve it and compare it with the database result

      EXPORT

        ex_tab = me->lt_result

        to MEMORY ID '%_RSTRFCT0'.

 

    endif.

 

  ENDMETHOD. "display_result

 

 

*  **********************************************************************

*   METHOD    :  append_to_result_tab

*   Created   :  07.11.2007 15:03:25

*  *********************************************************************

  METHOD append_to_result_tab .

 

    data: l_result TYPE displayLine.

 

    l_result-dest = me->l_dest.

    l_result-tid  = get_tid( i_dest = me->l_dest ).

    l_result-xcall = x_call.

    APPEND l_result to lt_result.

 

  ENDMETHOD. "append_to_result_tab

 

 

ENDCLASS.

 

 

**************************************************************

* this class is used for unittest

**************************************************************

CLASS lcl_unittest DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION MEDIUM FINAL.

 

  PRIVATE SECTION.

 

    CONSTANTS: qname TYPE trfcqout-qname VALUE 'RSTRFCT0'.

 

    DATA: dest TYPE rfcdes-rfcdest VALUE 'RSTRFCT0'.

 

    METHODS: do_unittest1 FOR TESTING,

 

             get_luws_in_db CHANGING ct_result_db TYPE displayLineTab.

 

ENDCLASS.

 

*----------------------------------------------------------------------*

*       CLASS lcl_unittest IMPLEMENTATION

*----------------------------------------------------------------------*

CLASS lcl_unittest IMPLEMENTATION.             "lcl_unittest DEFINITION

 

  method do_unittest1.

 

    DATA: db_tab TYPE displayLineTab,

          rep_tab TYPE displayLineTab.

 

*   delete old queue

    CALL FUNCTION 'TRFC_QOUT_DELETE_QUEUE'

      EXPORTING

        QNAME                 = qname

        DEST                  = dest .

 

    SUBMIT RSTRFCT0 with qname = qname

                    WITH rfcdest = dest

                    WITH nosend = 'X'

                    WITH display = abap_false

                    AND RETURN.

 

 

*   import the result table from the memory.

    IMPORT

      ex_tab = rep_tab[]

      FROM MEMORY ID '%_RSTRFCT0'.

 

    if sy-subrc = 0.

      FREE MEMORY ID '%_RSTRFCT0'.

    endif.

 

*   get the LUWs from the QRFC tables

    get_luws_in_db( CHANGING ct_result_db = db_tab ).

 

    cl_aunit_assert=>assert_equals(

        exp = rep_tab

        act = db_tab ).

 

*   delete the created queue

    CALL FUNCTION 'TRFC_QOUT_DELETE_QUEUE'

      EXPORTING

        QNAME                 = qname

        DEST                  = dest .

 

  endmethod.                                     "do_unittest1

 

*  **********************************************************************

*   METHOD    :  get_luws_in_db

*   Created   :  08.11.2007 15:51:16

*  *********************************************************************

  METHOD get_luws_in_db  .

 

    DATA: lt_states TYPE STANDARD TABLE OF arfcsstate,

          l_tid TYPE arfctid,

          l_result TYPE displayLine.

 

    FIELD-SYMBOLS:  TYPE arfcsstate.

 

    SELECT * FROM arfcsstate

      APPENDING TABLE lt_states

      WHERE arfcdest = me->dest

      AND   arfcreturn = 'O'.

 

    LOOP at lt_states ASSIGNING .

 

      l_result-dest = -arfcdest.

      l_tid-arfcipid = -arfcipid.

      l_tid-arfcpid = -arfcpid.

      l_tid-arfctime = -arfctime.

      l_tid-arfctidcnt = -arfctidcnt.

      l_result-tid = l_tid.

      l_result-xcall = -arfcluwcnt.

 

      APPEND l_result to ct_result_db.

 

    ENDLOOP.

 

    SORT ct_result_db BY dest xcall.

 

  ENDMETHOD. "get_luws_in_db

 

ENDCLASS.                    "lcl_unittest IMPLEMENTATION

 

START-OF-SELECTION.

 

  lcl_main=>run( ).


欢迎关注订阅号:SAP学习记录 (SAPlearning )

 

PO <wbr><wbr>text <wbr><wbr>copy <wbr><wbr>rules <wbr><wbr>:copy <wbr><wbr>PR <wbr><wbr>item <wbr><wbr>text <wbr><wbr>to <wbr><wbr>PO