首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java SAP通信

Java SAP通信
EN

Stack Overflow用户
提问于 2018-11-26 08:57:09
回答 2查看 1.6K关注 0票数 2

我必须使用JCo3.0从一个独立的Java应用程序连接到SAP /3系统。根据我的理解,有两种连接SAP系统的方法:

  • JCo客户端
  • JCo服务器

现在,我的应用程序从SAP系统发送和接收数据。我需要使用JCo客户机/服务器吗?

EN

回答 2

Stack Overflow用户

发布于 2018-11-27 16:09:16

通过RFC协议连接到SAP内部系统的两种方法是:

  1. 入站RFC通信( RFC客户机/ Java调用ABAP)
  2. 出站RFC通信( RFC服务器/ ABAP调用Java)

对于入站RFC,需要使用JCoDestination在ABAP端执行远程功能模块。对于出站RFC,您需要在SAP网关注册一个JCoServer,然后该网关将接收来自ABAP端的传入请求,以便在Java端处理远程功能模块。在两个通信方向上都有一个请求,也可能是对此请求的响应,因此数据流通常是双向的,不管您是在进行入站或出站RFC通信。入站和出站只区分谁发起RFC调用。

票数 1
EN

Stack Overflow用户

发布于 2020-03-25 18:31:12

对于我来说,使用R3和stijco3.jar的解决方案如下所示:

代码语言:javascript
复制
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);
        }
    }       
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53477576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档