Skip to Content
Technical Articles

SAP Cloud Foundry To On-Premise RFC Connection Using JCo

The SAP Java Connector (SAP JCo) is a development library that enables development of SAP compatible components in Java. It can be used to communicate with on-premise SAP systems via SAP’s RFC protocol.

(Source: https://support.sap.com/en/product/connectors/jco.html)

In this tutorial, we are going to learn how can we call a function module in an on-premise ABAP system by SAP JCo (Inbound).

Let’s go step by step.

  • Step 1: As a starting point, I build a maven web project with the following code:
package com.test.jco;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoRepository;

 
@WebServlet("/ConnectRFC/*")

public class ConnectRFC extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter responseWriter = response.getWriter();

        try {

            // access the RFC Destination "Test"
            JCoDestination destination = JCoDestinationManager.getDestination("Test");

            // make an invocation of STFC_CONNECTION in the backend
            JCoRepository repo = destination.getRepository();
            JCoFunction stfcConnection = repo.getFunction("STFC_CONNECTION");

            JCoParameterList imports = stfcConnection.getImportParameterList();
            imports.setValue("REQUTEXT", "JCO successful");

            stfcConnection.execute(destination);

            JCoParameterList exports = stfcConnection.getExportParameterList();
            String echotext = exports.getString("ECHOTEXT");
            String resptext = exports.getString("RESPTEXT");

            responseWriter.println(echotext+":"+resptext);
        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

The explanation goes as follows:

  1. “Test” is the name of the destination which will be used after a few steps.
  2. “STFC_CONNECTION” is a standard ABAP function module, which I am using here to test the connectivity.

Following JCo dependency needs to be included in your project.

<dependencies>
  <dependency>
    <groupId>com.sap.cloud</groupId>
    <artifactId>neo-java-web-api</artifactId>
    <version>[3.71.8,4.0.0)</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

 

  • Step 2: Now we need to log into the SAP CF subaccount, where we want to deploy our application. Here we need to create and bind instances for the following three services:
    • Connectivity Service
    • Destination Service
    • XSUAA service

I have created these service instances and named them as following – jco_con , jco_dest, and jco_xs. Select the service plan as application and Provide the following configuration in parameters while creating xsuaa instance. Here “connect-rfc” is the app name by which I will deploy my java application in CF.

{
    "xsappname" : "connect-rfc",
    "tenant-mode": "dedicated",
    "scopes": [
        {
            "name": "$XSAPPNAME.all",
            "description": "all"
        }
    ]
}

 

  • Step 3: Now deploy the war file to the subaccount with the following manifest.yml configuration.
---

applications:

 

- name: connect-rfc

  buildpacks:

  - sap_java_buildpack

  env:

    SAP_JWT_TRUST_ACL: '[{"clientid":"*","identityzone":"*"}]'

    xsuaa_connectivity_instance_name: "jco_xs"

  services:

    - jco_xs

    - jco_con

    - jco_dest

 

  • Step 4: Create a node (approuter ) ** . Make necessary arrangements into xs-app.json (Point the URL to the destination). Finally, Deploy this in the subaccount with the following manifest.yml configuration.
    ---
    
    applications:
    
     
    
    - name: approuter-jco
    
      path: ./
    
      buildpacks:
    
      - nodejs_buildpack
    
      memory: 120M
    
      env:
    
        NODE_TLS_REJECT_UNAUTHORIZED: 0
    
        destinations: >
    
          [
    
             {"name":"dest", "url" :"https://<your java application url>/ConnectRFC", "forwardAuthToken": true }
    
          ]
    
      services:
    
        - jco_xs​

    When choosing the application route, you are requested to login. Provide the credentials known by the IdP you configured in Roles & Trust. After successful login, you are routed to the java application which is then executed. It prompts an error that destination “Test” does not exist, which is obvious as we didn’t create a destination till now.

  • Step 5: Create an RFC destination named “Test” in your destination instance.
  • Step 6: Configure the system mapping and the function module in the Cloud Connector. Provide access to the function module “STFC_CONNECTION”.

cloud%20connector

cloud connector

SAP%20Cloud%20Platform%20-%20CF

SAP Cloud Platform – CF

 

  • Step 7: Run approuter, provide the credentials. This time you will receive the output of your Servlet. I received the following output.
JCO successful:SAP R/3 Rel. 752   Sysid: XXX      Date: 20200725   Time: 110012   Logon_Data: 100/XXXX/E

 

Note : In this tutorial, I used approuter for authentication purpose. You can use AppToAppSSO to propagate authentication token. Token is required for On-Premise connectivity.

 

**Follow the steps in Application Router or use the demo file approuter.zip (download).

With this, I would like to conclude the tutorial. For more details, follow the below links :

  1. https://support.sap.com/en/product/connectors/jco.html
  2. https://help.sap.com/viewer/cca91383641e40ffbe03bdc78f00f681/Cloud/en-US/e54cc8fbbb571014beb5caaf6aa31280.html
  3. https://help.sap.com/viewer/cca91383641e40ffbe03bdc78f00f681/Cloud/en-US/7e306250e08340f89d6c103e28840f30.html

 

Be the first to leave a comment
You must be Logged on to comment or reply to a post.