The simplest qRFC calling program on ABAP server

qRFC is very useful way to call program many times chronologically.  I've checked how to program and process types.
# Strong points
Strong points of qRFC are as follows.
1. Process **asynchronously**
2. Process **chronologically**
3. When an error occurs, **stop the queue and succeeding processes**

#The simplest program to use qRFC
The following program generates outbound queue.  It's very simple!

```abap
Report YSIMPEST_QRFC

* Set an outbound queue name
  CALL FUNCTION 'TRFC_SET_QUEUE_NAME'
    EXPORTING
      qname              = 'YTEST01'
    EXCEPTIONS
      invalid_queue_name = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
    MESSAGE e001(00) WITH 'TRFC_SET_QUEUE_NAME error'.
  ENDIF.

* Call RFC module as qRFC
  CALL FUNCTION 'RFC_PING'
    IN BACKGROUND TASK
    AS SEPARATE UNIT.

  COMMIT WORK.
  IF sy-subrc <> 0.
    MESSAGE e001(00) WITH 'Commit error with return code:' sy-subrc.
  ENDIF.
```

# 3 ways to use qRFC
There are 3 ways to use qRFC. See the below picture.
![qRFC_Pattern.JPG](https://qiita-image-store.s3.amazonaws.com/0/134273/fad27ccc-99f0-aa90-7a4d-23c3aca61cdb.jpeg)

Please see help portal page ["qRFC Communication Model."](https://help.sap.com/saphelp_nw74/helpdata/en/48/9c44f92ab0062fe10000000a42189d/frameset.htm)

## Type A: Outbound queue only
The following program uses outbound queue only.

```abap
REPORT YQRFC_OUTBOUND.

* Number of queues to be created
PARAMETERS p_queue TYPE i DEFAULT 1000.

START-OF-SELECTION.
  DO p_queue TIMES.

*   Set queue name
    CALL FUNCTION 'TRFC_SET_QUEUE_NAME'
      EXPORTING
        qname              = 'YTEST01'
      EXCEPTIONS
        invalid_queue_name = 1
        OTHERS             = 2.
    IF sy-subrc <> 0.
      MESSAGE e001(00) WITH 'Function TRFC_SET_QUEUE_NAME error'.
    ENDIF.

*   call as qRFC
    CALL FUNCTION 'RFC_PING'
      IN BACKGROUND TASK
      AS SEPARATE UNIT.

  ENDDO.

* Commit is necessary
  COMMIT WORK.
  IF sy-subrc <> 0.
    MESSAGE e001(00) WITH 'Commit error with sy-subrc:' sy-subrc.
  ENDIF.
```

## Type B: Inbound queue only, Type C :Outbound and nbound queue
The below program generates only inbound queue or both outbound and inbound queue.  When rfc destination is 'NONE' or omitted, it's type B. 

```abap

REPORT  YQRFC_INBOUND.

PARAMETERS:
  p_num  TYPE i        DEFAULT 100,
  p_in   TYPE trfcqnam DEFAULT 'YTEST01_IN',
  p_out  TYPE trfcqnam DEFAULT 'YTEST01_OUT',
  p_rfc  TYPE rfcdest  DEFAULT 'NONE'.

START-OF-SELECTION.
  DO p_num TIMES.

    CALL FUNCTION 'TRFC_SET_QIN_PROPERTIES'
      EXPORTING
        qout_name          = p_out
        qin_name           = p_in
      EXCEPTIONS
        invalid_queue_name = 1
        OTHERS             = 2.
    IF sy-subrc <> 0.
      MESSAGE e001(00) WITH 'TRFC_SET_QIN_PROPERTIES error'.
    ENDIF.

    CALL FUNCTION 'RFC_PING'
      IN BACKGROUND TASK
      DESTINATION p_rfc
      AS SEPARATE UNIT.

  ENDDO.
  COMMIT WORK.
  IF sy-subrc <> 0.
    MESSAGE e001(00) WITH 'Commit error with return code:' sy-subrc.
  ENDIF.
```

As for type C, outbound queue is deleted when inbound queue is completely **generated**.  The outbound queue's lifespan is not up to if inbound queue is finished or not.