簡體   English   中英

Java SAP 通信

[英]Java SAP Communication

我必須使用 JCo3.0 從獨立 Java 應用程序連接到 SAP R/3 系統。 根據我的理解,有兩種方式可以連接到 SAP 系統:

  • JCo客戶端
  • JCo服務器

現在我的應用程序從 SAP 系統發送和接收數據。 我需要使用 JCo 客戶端/服務器嗎?

通過 RFC 協議連接到 SAP 內部部署系統的兩種方法是:

  1. 入站 RFC 通信(作為 RFC 客戶端/Java 調用 ABAP)
  2. 出站 RFC 通信(作為 RFC 服務器/ABAP 調用 Java)

對於入站 RFC,您需要使用JCoDestination在 ABAP 端執行遠程功能模塊。 對於出站 RFC,您需要在 SAP 網關注冊一個JCoServer ,然后它將接收來自 ABAP 端的傳入請求,以便在 Java 端處理遠程功能模塊。 在兩個通信方向上都有一個請求,也可能有一個對該請求的響應,因此數據流通常是雙向的,無論您是在進行入站還是出站 RFC 通信。 入站和出站只是區分誰發起 RFC 調用。

使用 R3 和 sapjco3.jar 對我有用的解決方案如下:

package com.example.springsocial.sap;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

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.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider;

public class SAPrfc {
    static String   IP="192.168.1.1",
                    USER="userName",
                    PASSWORD="myPassword",
                    CLIENT="100",// Mandatory number
                    SYSNR="00", // instance number
                    LANG="es", // language (es or en)
                    DESTINATION_NAME = "SAPconnection";

    private static JCoDestination destination;
    private static JCoParameterList outputTableParameterList;
    private static Map<String, Object> inputParameter;

    private static JCoFunction function;
    public static String rfcName="";
    public static String tableName="";

    public SAPrfc() throws Exception {
        setConnection();
        initConnection();
        clearParameters();
    }

    public static void setConnection() {
        System.out.println("setConnection in SAPrfc class");
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST,   IP);
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,    SYSNR);
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT,   CLIENT);
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,     USER);
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,   PASSWORD);
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,     LANG);
        createDestinationDataFile(DESTINATION_NAME,connectProperties);
    }

    private static void createDestinationDataFile(String destinationName, Properties connectProperties){
        File destCfg = new File(destinationName+".jcoDestination");
        try{
            FileOutputStream fos = new FileOutputStream(destCfg, false);
            connectProperties.store(fos, "for tests only !");
            fos.close();
        }catch (Exception e){
            throw new RuntimeException("Unable to create the destination files", e);
        }
    }

    public static void initConnection() throws Exception {
        System.out.println("initConnection in SAPrfc class");
        destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
        System.out.println("Attributes:");
        System.out.println(destination.getAttributes());
        System.out.println();
        destination.ping();
    } 

    public static void setRfcName(String rfcNameParam) {rfcName= rfcNameParam; };
    public static void setTableName(String tableNameParam) {tableName= tableNameParam; };

    public static JCoParameterList getOutputTableParamList() { return outputTableParameterList;};
    public static JCoTable getOutputTable() { return outputTableParameterList.getTable(tableName);};

    public static void clearParameters() { inputParameter= new HashMap<>(); };
    public static void addParameter(String paramName, Object paramValue) { inputParameter.put(paramName, paramValue); }
    public static void setParameters() {
        if (inputParameter!=null)
            for(Map.Entry<String, Object> entry : inputParameter.entrySet()) {
                function.getImportParameterList().setValue(entry.getKey(), entry.getValue());
            }
    }

    public static void executeRFC() throws Exception {
        if (rfcName.equals(""))
            throw new Exception("Es requerido el nombre de una RFC");

        destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
        function = destination.getRepository().getFunction(rfcName);

        if (function == null)
            throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");

        setParameters();
        function.execute(destination);
        if (function.getExportParameterList().getFieldCount()==0) 
            throw new Exception("Error al obtener los datos");


        String resultado = function.getExportParameterList().getString("RESULTADO").toString();
        if (!resultado.equals("X"))// correct
            throw new Exception(function.getExportParameterList().getString("MENSAJE").toString());

        outputTableParameterList = function.getTableParameterList();
        setRfcName("");
        clearParameters();
    }

    public static void main(String[] args) {
        try {
            //RFC without parameters
            SAPrfc.setRfcName("ZMMF_CAT_ALMACENES");
            SAPrfc.clearParameters();
            SAPrfc.executeRFC();    
            SAPrfc.setTableName("TI_ALMACENES");
            JCoTable table = SAPrfc.getOutputTable();
            String WERKS,LGORT,LGOBE;
            for (int i = 0; i < table.getNumRows(); i++, table.nextRow()){
                 WERKS = table.getString("WERKS");
                 LGORT = table.getString("LGORT");
                 LGOBE = table.getString("LGOBE");
                 System.out.println("[" + i + "] - WERKS : "+WERKS+" LGORT: "+LGORT+" LGOBE: "+LGOBE );
            }

            //RFC with parameters
            SAPrfc.setRfcName("ZMMF_CAT_MATERIALES");
            SAPrfc.clearParameters();
            SAPrfc.addParameter("IWERKS", 2010);
            SAPrfc.executeRFC();    
            SAPrfc.setTableName("TI_MATERIALES");
            JCoTable table2 = SAPrfc.getOutputTable();
            String MATNR,MAKTX,DUN14,BISMT;
            for (int i = 0; i < table2.getNumRows(); i++, table2.nextRow()){
                 MATNR = table2.getString("MATNR");
                 MAKTX = table2.getString("MAKTX");
                 DUN14 = table2.getString("DUN14");
                 BISMT = table2.getString("BISMT");
                 System.out.println("[" + i + "] - MATNR : "+MATNR+" MAKTX: "+MAKTX+" DUN14: "+DUN14+" BISMT: "+BISMT );
            }

            //RFC without parameters
            SAPrfc.setRfcName("ZMMF_CAT_TIPO_MOV");
            SAPrfc.clearParameters();
            SAPrfc.executeRFC();    
            SAPrfc.setTableName("TI_TIPO_MOV");
            JCoTable table3 = SAPrfc.getOutputTable();
            String ACTION,DDTEXT;
            for (int i = 0; i < table3.getNumRows(); i++, table3.nextRow()){
                ACTION = table3.getString("ACTION");
                DDTEXT = table3.getString("DDTEXT");
                System.out.println("[" + i + "] - ACTION : "+ACTION+" DDTEXT: "+DDTEXT);
            }
        }catch (Exception ex) {
            System.out.println("Exception "+ex);
        }
    }       
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM