zhangherong
2025-06-17 0ecd8b8c158436b23298b66b64eaef0438b6425b
art: SAP集成测试代码修改
已修改4个文件
138 ■■■■■ 文件已修改
src/main/java/org/jeecg/config/sap/CustomDestinationDataProvider.java 66 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/org/jeecg/config/sap/SapRfcConnectionManager.java 59 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/org/jeecg/modules/sap/service/impl/SAPServiceImpl.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-dev.yml 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/org/jeecg/config/sap/CustomDestinationDataProvider.java
@@ -1,31 +1,35 @@
//package org.jeecg.config.sap;
//
//import com.sap.conn.jco.ext.*;
//
//import java.util.Properties;
//
//public class CustomDestinationDataProvider implements DestinationDataProvider {
//
//    private final Properties properties = new Properties();
//
//    public void addDestination(String destinationName, Properties connectProperties) {
//        properties.put(destinationName, connectProperties);
//    }
//
//    @Override
//    public boolean supportsEvents() {
//        return false;
//    }
//
//    @Override
//    public void setDestinationDataEventListener(DestinationDataEventListener destinationDataEventListener) {
//
//    }
//
//    @Override
//    public Properties getDestinationProperties(String destinationName) {
//        return properties.getProperty(destinationName) != null
//                ? (Properties) properties.get(destinationName)
//                : null;
//    }
//}
package org.jeecg.config.sap;
import com.sap.conn.jco.ext.*;
import java.util.Properties;
public class CustomDestinationDataProvider implements DestinationDataProvider {
    private final String destName;
    private final Properties properties;
    public CustomDestinationDataProvider(String destName, Properties props) {
        this.destName = destName;
        this.properties = props;
    }
    @Override
    public boolean supportsEvents() {
        return false;
    }
    @Override
    public void setDestinationDataEventListener(DestinationDataEventListener destinationDataEventListener) {
    }
    @Override
    public Properties getDestinationProperties(String destinationName) {
        if (destName.equals(destinationName)) {
            return properties;
        }
        return null;
    }
}
src/main/java/org/jeecg/config/sap/SapRfcConnectionManager.java
@@ -1,19 +1,21 @@
package org.jeecg.config.sap;
import com.sap.conn.jco.*;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.Environment;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
@Component
@Slf4j
@Getter
public class SapRfcConnectionManager {
    @Value("${sap.rfc.destination}")
@@ -37,10 +39,13 @@
    @Value("${sap.rfc.lang}")
    private String lang;
    private String poolSize = "5";
    @Value("${sap.rfc.poolSize}")
    private String poolSize;
    private String idleTimeout = "10000";
    @Value("${sap.rfc.expirationTime}")
    private String expirationTime;
    @Value("${sap.rfc.peekLimit}")
    private String peekLimit = "10";
    private JCoDestination destination;
@@ -60,53 +65,15 @@
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, lang);
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, peekLimit);
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, poolSize);
        connectProperties.setProperty(DestinationDataProvider.JCO_EXPIRATION_TIME, idleTimeout);
        connectProperties.setProperty(DestinationDataProvider.JCO_EXPIRATION_TIME, expirationTime);
        // 创建动态目的地(避免依赖 SM59 配置)
//        CustomDestinationDataProvider provider = new CustomDestinationDataProvider();
//        provider.addDestination(destinationName, connectProperties);
        CustomDestinationDataProvider provider = new CustomDestinationDataProvider(destinationName, connectProperties);
        // 设置全局目的地提供者
//        Environment.registerDestinationDataProvider(provider);
        log.info("properties = " + connectProperties);
        //生成配置文件,JCoDestinationManager.getDestination()调用时会需要该连接配置文件,后缀名需要为jcoDestination
        FileOutputStream fos = null;
        String suffix = "jcoDestination";
        File cfg = new File(destinationName + "." + suffix);
        if (!cfg.exists()) {
            try {
                fos = new FileOutputStream(cfg, false);
                connectProperties.store(fos, "for tests only !");
                fos.close();
            } catch (Exception var9) {
                throw new Exception("Unable to create the destination file " + cfg.getName(), var9);
            } finally {
                if (null != fos) {
                    fos.close();
                }
            }
        }
        Environment.registerDestinationDataProvider(provider);
        // 获取目的地
        this.destination = JCoDestinationManager.getDestination(destinationName);
    }
    /**
     *
     * @return
     */
    public JCoDestination getDestination() {
        return destination;
    }
    /**
     *
     */
    public void destroy() {
        if (destination != null) {
//            destination;
        }
    }
}
src/main/java/org/jeecg/modules/sap/service/impl/SAPServiceImpl.java
@@ -11,6 +11,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Collections;
@Service
@Slf4j
public class SAPServiceImpl implements SAPService {
@@ -28,9 +31,8 @@
        }
        // 设置输入参数
//        JCoParameterList input = function.getImportParameterList();
//        input.setValue("MATERIAL_ID", materialId); // 参数名需与 SAP 函数定义一致
//        function.getImportParameterList().setValue("MATERIAL_ID", 0);
        JCoParameterList input = function.getImportParameterList();
        input.setValue("ZTAB_WERKS", Collections.singletonList("2301")); //参数名需与 SAP 函数定义一致
        // 执行调用
        function.execute(destination);
src/main/resources/application-dev.yml
@@ -317,10 +317,13 @@
    workFolder: D:\\hy_test\\b
sap:
  rfc:
    destination: ZPPF_022     # SM59 中配置的 RFC 目标名称
    destination: SAP_RFC_DEST # 自定义的目标地址 RFC 目标名称
    ashost: 10.101.0.188      # SAP 主机地址
    sysnr: '00'               # 系统编号
    client: 800               # 客户端编号
    user: SLSAP_JK            # 用户名
    passwd: 112233            # 密码
    lang: ZH                  # 语言
    poolSize: 5               # 线程池数量
    expirationTime: 10000     # 过期时间
    peekLimit: 10             # 峰值