JCO3远程调用SAP接口随笔一(配置连接池)

一、配置环境:
1.下载sapjco3.dll插件置放在JDK、bin文件夹下
2.下载sapjco3.jar置放工程中

二、配置访问属性文件
SAPConnectionPool.properties

#sap服务器IP地址,如果是外网访问,则在内网IP前加/H/外网IP/H/192.168.0.10 
jco.client.ashost=192.168.0.10 
#系统编号    
jco.client.sysnr=01
#配置client 
jco.client.client=999
jco.client.user=likb
jco.client.passwd=330330
jco.destination.pool_capacity=3
jco.destination.peak_limit=10
jco.client.lang=ZH

三、封装获取链接池链接java类,以便接口使用时调用
SAPConnectionPool类:

@Service("SAPConnectionPool")
public class SAPConnectionPool {
    private static final String SAP_CONN="SAP_CONN";

    public static JCoDestination getSAPDestination(){
        try {
            JCoDestination dest = JCoDestinationManager.getDestination(SAP_CONN);
            return dest;
        } catch (JCoException ex) {
                System.out.println(ex);
                System.out.println("连接失败");
                //重新连接
                return RegetJocodestination();
        }           
    }   

    /*****
    * @author     :QZC
    * @createDate :2015年8月27日 上午11:08:47
    * 函数功能描述:重新获取JCODestination
    * @return
    ****
     */
    public static JCoDestination RegetJocodestination(){
        try{
            Properties properFile = new Properties();
            ClassLoader cl=Thread.currentThread().getContextClassLoader();
            String filePath=cl.getResource("").toString()+"SAPConnectionPool.properties";
            filePath=filePath.replace("file:", "");
            FileInputStream inputFile = new FileInputStream(filePath);
            properFile.load(inputFile);
            inputFile.close();

            Properties connectProperties = new Properties();
            connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, properFile.getProperty("jco.client.ashost"));
            connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  properFile.getProperty("jco.client.sysnr"));
            connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, properFile.getProperty("jco.client.client"));
            connectProperties.setProperty(DestinationDataProvider.JCO_USER,   properFile.getProperty("jco.client.user"));
            connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, properFile.getProperty("jco.client.passwd"));
            connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, properFile.getProperty("jco.destination.pool_capacity"));
            connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,   properFile.getProperty("jco.destination.peak_limit"));
            connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   properFile.getProperty("jco.client.lang"));

            CustomDestinationDataProvider provider = new CustomDestinationDataProvider();
            provider.addDestinationProperties(SAP_CONN, connectProperties);
            Environment.registerDestinationDataProvider(provider);
            try {
                JCoDestination dest = JCoDestinationManager.getDestination(SAP_CONN);
                return dest;
            } catch (JCoException ex) {
                    System.out.println(ex);
                    System.out.println("重新连接失败");
            }           
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;        
    }

}

获取链接的辅助处理类:CustomDestinationDataProvider.java

public class CustomDestinationDataProvider implements DestinationDataProvider {
    private Map providers = new HashMap();
    @Override
    public Properties getDestinationProperties(String destName) {
       if (destName == null)
           throw new NullPointerException("请指定目的名称");
       if (providers.size() == 0)
           throw new IllegalStateException("请加入一个目的连接参数属性给提供者");
       return (Properties)providers.get(destName);
   }

   // 没有实现事件处理
   @Override
   public boolean supportsEvents(){
       return false;
   }

   @Override
   public void setDestinationDataEventListener(DestinationDataEventListener listener) {
       throw new UnsupportedOperationException();
   }

   public void addDestinationProperties(String destName, Properties provider) {
       providers.put(destName, provider);
   }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值