ecology调用SAPRFC接口第二种写法 (生成配置文件)

package com.kaka.workflow.utils;
import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;

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

/**
 * @Auther: kaka
 * @Date: 2023/08/13/11:13
 * @Description:
 */
public class ConnSAP  {
    static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
    static {
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "");//IP
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, ""); //编号
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "");//客户端
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, ""); //账号
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, ""); //密码
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh"); //语言
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, ""); //连接数
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, ""); //分页
        createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
    }

    static void createDataFile(String name, String suffix, Properties properties) {
        File cfg = new File(name + "." + suffix);
        if (!cfg.exists()) {
            try {
                FileOutputStream fos = new FileOutputStream(cfg, false);
                properties.store(fos, "SAP连接配置文件");
                fos.close();
            } catch (Exception e) {
                throw new RuntimeException(
                        "Unable to create the destination file " + cfg.getName(), e);
            }
        }
    }

    public static JCoDestination Connect() {
        JCoDestination destination = null;
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
       } catch (Exception e) {
            e.getCause();
        }
        return destination;
    }

    /***
    * @MethodName: main
    * @Description:  测试是否连接成功
    * @Param: [args]
    * @return: void
    * @Author: kaka
    * @Date: 2023/9/18 10:40
    */
    public static void main(String[] args) {
        JCoDestination connect = ConnSAP.Connect();
        //打印输出
        System.out.println(connect);
    }
}

运行结果还是成功的,缺点就是通过不方便移植,程序通过静态代码块生成,程序一启动就加载进内存,如果配置文件删除就会链接失败报错,可移植性差,不方便找到BUG。