您的位置:首页 > 编程语言 > Java开发

JAVA调用SAP的函数RFC--个人小结

2016-10-22 11:48 274 查看
   首先,在网上下载一个下面的压缩包(不同版本内容会稍微不同),打开sapjco3-NTintel-3.0.7.zip,将sapjco3.jar拷入到lib里,选中sapjco3.jar,右键点击Buid Path, 选择Add to Build Path既可。

   然后,打开sapjco3-NTintel-3.0.7.zip,将sapjco3.dll拷入到系统system32目录下(如果调试不成功,请将sapjco3.dll拷入到lib文件夹里)。

  正常的JAVA开发环境的基础上就可以直接写个java类,我写了两个类:Java2SapDestination和MyDestinationDataProvider,直接上代码:package com.jerei.wcm.common.utils.base;
import java.util.Map;

import java.util.Properties;
import com.jerei.wcm.common.utils.base.MyDestinationDataProvider;

import com.sap.conn.jco.JCoDestination;

import com.sap.conn.jco.JCoDestinationManager;

import com.sap.conn.jco.JCoException;

import com.sap.conn.jco.JCoFunction;

import com.sap.conn.jco.JCoTable;

import com.sap.conn.jco.ext.DestinationDataProvider;

import com.sap.conn.jco.ext.Environment;
public class Java2SapDestination {

 

  public Object executeCalls(Map<String, Object> map,String[] items)

     {

         JCoDestination dest;

         Object  object = null;

         try

         {

             dest = JCoDestinationManager.getDestination("ABAP_AS");

             dest.ping();

             System.out.println("路之家订单开始传入SAP " + map.size());

            

             JCoFunction function = dest.getRepository().getFunction("ZDLZLCRM_BJ_SET_INTENTION");//从对象仓库中获取 RFM 函数,需要有SAP提供

             function.getImportParameterList().setValue("VBKZH", "I");

             function.getImportParameterList().setValue("BSTKD", map.get("no"));

             function.getImportParameterList().setValue("RT_VBELN", "");

             function.getImportParameterList().setValue("AUART", map.get("auart"));

             function.getImportParameterList().setValue("VKORG", map.get("vkorg"));

             function.getImportParameterList().setValue("VTWEG", "10");

             function.getImportParameterList().setValue("SPART", "10");

             function.getImportParameterList().setValue("VKBUR", "928");

             function.getImportParameterList().setValue("VKGRP", "");

             function.getImportParameterList().setValue("KDGRP", "");

             function.getImportParameterList().setValue("BSTDK", map.get("bstdk"));

             function.getImportParameterList().setValue("KONDA", "");

             function.getImportParameterList().setValue("KUNNR_AG", "176938");

             function.getImportParameterList().setValue("KUNNR_WE", "");

             function.getImportParameterList().setValue("KUNNR_RE", "");

             function.getImportParameterList().setValue("KUNNR_RG", "");

             function.getImportParameterList().setValue("ZTERM", "Y001");

             function.getImportParameterList().setValue("VBELN_CRM", map.get("no"));

             function.getImportParameterList().setValue("INFO", map.get("remark"));

             function.getImportParameterList().setValue("Z017", map.get("z017"));

             JCoTable tDateRange = function.getTableParameterList().getTable("ITEMS");

            

             //一次插入订单的所有物料

             int items_size = items.length;

             for(int i = 0 ; i < items_size; i++){

              //逐行追加物料信息

              tDateRange.appendRow();

              tDateRange.setRow(i);

              String[] items_detail = items[i].split(",");

               tDateRange.setValue("MATNR", items_detail[0]);

               tDateRange.setValue("ZMENG", items_detail[1]);

               tDateRange.setValue("KBETR_ZPR4", items_detail[2]);

               tDateRange.setValue("POSNR", items_detail[3]);

             }

             

             tDateRange.setValue("VBKZ", "I");

             tDateRange.setValue("SPECIAL", "");

             function.execute(dest);

             Object flag = function.getExportParameterList().getValue("RETURNCODE");

             Object sapNo = function.getExportParameterList().getValue("VBELN_EXPORT");

             System.out.println("function " + flag+"-"+sapNo);

             object = flag+"-"+sapNo;

         }

         catch(JCoException e)

         {

             e.printStackTrace();

             System.out.println("Execution on destination " + map+ " failed");

         }

   return object;

        

     }

    

     public static Properties getDestinationPropertiesFromUI()

     {

         //adapt parameters in order to configure a valid destination

         Properties connectProperties = new Properties();

         connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "IP地址");

         connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");

         connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "800");

         connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "用户名");

         connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "密码");

         connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "ZH");

         return connectProperties;

     }

    

     public Object data2SAP(Map<String, Object> param,String[] array){

       MyDestinationDataProvider myProvider = new MyDestinationDataProvider();

         

          try

           {

              Environment.registerDestinationDataProvider(myProvider);

           }

            catch(IllegalStateException providerAlreadyRegisteredException)

           {

              throw new Error(providerAlreadyRegisteredException);

           }

          String destName = "ABAP_AS";//这个名字可以自己定义

          

          myProvider.changeProperties(destName, getDestinationPropertiesFromUI());

          Object reStr = executeCalls(param,array);

          Environment.unregisterDestinationDataProvider(myProvider);

      return reStr;

     }

    

}

package com.jerei.wcm.common.utils.base;
import java.util.HashMap;

import java.util.Properties;
import com.sap.conn.jco.ext.DataProviderException;

import com.sap.co
4000
nn.jco.ext.DestinationDataEventListener;

import com.sap.conn.jco.ext.DestinationDataProvider;
public class MyDestinationDataProvider implements DestinationDataProvider{
    private DestinationDataEventListener eL;

    private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();

   

    public Properties getDestinationProperties(String destinationName)

    {

        try

        {

            //read the destination from DB

            Properties p = secureDBStorage.get(destinationName);
            if(p!=null)

            {

                //check if all is correct, for example

                if(p.isEmpty())

                    throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);
                return p;

            }

           

            return null;

        }

        catch(RuntimeException re)

        {

            throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);

        }

    }
    //An implementation supporting events has to retain the eventListener instance provided

    //by the JCo runtime. This listener instance shall be used to notify the JCo runtime

    //about all changes in destination configurations.

    public void setDestinationDataEventListener(DestinationDataEventListener eventListener)

    {

        this.eL = eventListener;

    }
    public boolean supportsEvents()

    {

        return true;

    }
    //implementation that saves the properties in a very secure way

    void changeProperties(String destName, Properties properties)

    {

        synchronized(secureDBStorage)

        {

            if(properties==null)

            {

                if(secureDBStorage.remove(destName)!=null)

                    eL.deleted(destName);

            }

            else

            {

                secureDBStorage.put(destName, properties);

                eL.updated(destName); // create or updated

            }

        }

    }

}

     然后就可以直接调用Java2SapDestination类的data2SAP方法了,以上就可以实现JAVA程序和SAP某个函数的交互了,有几个问题需要注意,也是我在开发中遇到的:
     1.每次调用SAP接口,建立连接之后,要关闭掉(Environment.unregisterDestinationDataProvider(myProvider);)
     2.SAP函数有內表的话,要一次把主数据和內表数据操作结束,tDateRange.appendRow(); tDateRange.setRow(i); 把多条內表数据绑定一条主数据上去
     3.最后就是sapjco3.jar包,一定不要忘记


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA SAP