zoukankan      html  css  js  c++  java
  • java call sap

    1、下载需要的jar,windows用dll,linux用so

      win下载地址     linux下载地址 win下载地址new

    2、环境:

      windows -> sapjco3.dll放到windows目录下

      linux -> libsapjco3.so 放到lib目录下(没权限就建个环境变量让后放进去)

    3、代码:

      1 package cn.com.sony.newsis.common.tools;
      2 
      3 import java.io.File;
      4 import java.io.FileOutputStream;
      5 import java.text.SimpleDateFormat;
      6 import java.util.Date;
      7 import java.util.List;
      8 import java.util.Properties;
      9 import java.util.concurrent.ConcurrentHashMap;
     10 
     11 import cn.com.sony.npc.util.Props.PropsUtil;
     12 
     13 import com.sap.conn.jco.AbapException;
     14 import com.sap.conn.jco.JCo;
     15 import com.sap.conn.jco.JCoDestination;
     16 import com.sap.conn.jco.JCoDestinationManager;
     17 import com.sap.conn.jco.JCoException;
     18 import com.sap.conn.jco.JCoFunction;
     19 import com.sap.conn.jco.JCoParameterList;
     20 import com.sap.conn.jco.ext.DestinationDataProvider;
     21 
     22 
     23 public class AUJcoClient
     24 {
     25     static String ABAP_AS_POOLED = "AU_AS_WITH_POOL";
     26     static String[] jcoPara=Operate.getAUJcoPara();
     27     static int timeOut=Integer.parseInt(jcoPara[8]);
     28     public static int FUNCTIONCOUNT=0;
     29     public static long FOLLOWCOUNT=0;
     30     public static int PEAK_LIMIT=Integer.parseInt(jcoPara[7]);
     31     
     32     public static ConcurrentHashMap<Long,Long> hm=new ConcurrentHashMap<Long,Long>();
     33     
     34 
     35     /**
     36      * initial JCO connection
     37      */
     38     static{
     39         for(int i=0;i<jcoPara.length;i++){
     40             System.out.print(jcoPara[i]+",");
     41         }
     42         Properties connectProperties = new Properties();
     43         connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, jcoPara[0]);
     44         connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  jcoPara[1]);
     45         connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, jcoPara[2]);
     46         connectProperties.setProperty(DestinationDataProvider.JCO_USER,   jcoPara[3]);
     47         connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, jcoPara[4]);
     48         connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   jcoPara[5]);
     49         connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, jcoPara[6]);
     50         connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, jcoPara[7]);
     51         
     52         createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
     53         
     54    }
     55     
     56     static void createDataFile(String name, String suffix, Properties properties){
     57         File cfg = new File(name+"."+suffix);
     58         if(!cfg.exists()){
     59             FileOutputStream fos = null;
     60             try{
     61                 fos = new FileOutputStream(cfg, false);
     62                 properties.store(fos, "for tests only !");
     63             }catch (Exception e){
     64                 //Point 4 –  handle this exception
     65                 throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
     66             }finally{
     67                 try{
     68                     if(fos!=null) fos.close(); 
     69                 }catch(Exception e){
     70                     e.printStackTrace();
     71                 }
     72             }
     73         }
     74     }
     75     
     76     /**
     77      * access JCO Interface 'Z_CREDIT_EXPOSURE'
     78      * @param customerAccountNumber
     79      * @param companyCode
     80      * @param creditControlArea
     81      * @param dateCreditExposure
     82      * @return
     83      * @throws JCoException 
     84      */
     85     public static String[] functionCallcreditExposure(String customerAccountNumber,String companyCode,String creditControlArea,String dateCreditExposure) throws Exception{
     86         String[] res = new String[3];
     87         String  creditLimit = null;
     88         String  Receivable = null;
     89         String  creditExposure = null;
     90         try{
     91         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
     92         JCoFunction function = destination.getRepository().getFunction("Z_CREDIT_EXPOSURE");
     93         JCoParameterList inParm  = function.getImportParameterList();
     94             inParm.setValue("KUNNR", "4002485");
     95             inParm.setValue("BUKRS",  "AU40");
     96             inParm.setValue("KKBER","AU40");
     97             inParm.setValue("DATE_CREDIT_EXPOSURE","99991231");
     98             FUNCTIONCOUNT++;
     99             function.execute(destination);
    100             FOLLOWCOUNT++;
    101             if(FOLLOWCOUNT>100000) FOLLOWCOUNT=0; 
    102             hm.put(new Long(FOLLOWCOUNT), new Date().getTime()/1000);
    103             creditExposure = function.getExportParameterList().getString("SUM_OPENS").toString();
    104             Receivable = function.getExportParameterList().getString("OPEN_ITEMS").toString();
    105             creditLimit = function.getExportParameterList().getString("CREDITLIMIT").toString();
    106             res[0] = creditExposure;
    107             res[1] = Receivable;
    108             res[2] = creditLimit;
    109         }catch (JCoException e) {
    110             throw e;
    111         }finally{
    112             FUNCTIONCOUNT--;
    113         }
    114         return res;
    115     }
    116 
    117     /**
    118      * access JCO Interface 'Y_RFC_AP_ACC_GETKEYDATEBALANC'
    119      * @param  material String
    120      * @param  req_qty String
    121      * @param  customer String
    122      * @return String part status
    123      */
    124     public static String functionCallGetBalance(String vendorAccountNumber,String companyCode,String dateCreditExposure) throws Exception{
    125         String  vendorBalance = null;
    126         try{
    127         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
    128         JCoFunction function = destination.getRepository().getFunction("Y_RFC_AP_ACC_GETKEYDATEBALANC");
    129         JCoParameterList inParm    = function.getImportParameterList();
    130             inParm.setValue("VENDOR", vendorAccountNumber);
    131             inParm.setValue("COMPANYCODE",  companyCode);
    132             inParm.setValue("KEYDATE",dateCreditExposure);
    133             FUNCTIONCOUNT++;
    134             function.execute(destination);
    135             FOLLOWCOUNT++;
    136             if(FOLLOWCOUNT>100000) FOLLOWCOUNT=0; 
    137             hm.put(new Long(FOLLOWCOUNT), new Date().getTime()/1000);
    138             vendorBalance = function.getExportParameterList().getString("LC_BAL").toString();
    139         } catch(AbapException e) {
    140             System.out.println(e.toString());
    141         }finally{
    142             FUNCTIONCOUNT--;
    143         }
    144         return vendorBalance;
    145     }
    146     
    147     /**
    148      * Control Maximum concurrency
    149      * @param  material String
    150      * @param  req_qty String
    151      * @param  customer String
    152      *@return part status
    153      */
    154     public static Object functionControlGetBalance(String VendoraccountNumber,String CompanyCode,String DateCreditExposure) throws Exception {
    155         
    156         String status=null;
    157         Object partStat=null;
    158         Date d1=null;
    159         Date d2=null;
    160                 String path= PropsUtil.get("JCO_ROOT");
    161         try{
    162             /*
    163              * WAS的自动重启机制会产生"Library is already loaded in another ClassLoader"的bug,必须黑屏重启jvm
    164              * Java虚拟机为了在JNI本地库中确保基于classloader的命名空间隔离,因而不允许一个JNI本地库被两个不同的classloader加载。
    165              * 只要将JNI class单独发布成jar包,并放在配置公共(default、all或share)的lib目录中,问题就可以解决
    166              */
    167             JCo.setTrace(0, path);
    168         }catch (Exception e) {
    169             System.err.println("Jco loader error:"+e);
    170         }
    171         
    172         LayOut lo=new LayOut(path+"/AUJcoPrint.log");
    173 
    174         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    175         SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    176         StringBuffer sb=new StringBuffer("");
    177         
    178         try{
    179             sb.append(sdf2.format(new Date())).append("	").append("VendoraccountNumber:")
    180             .append(VendoraccountNumber).append("	").append("CompanyCode:").append(CompanyCode)
    181             .append("	").append("DateCreditExposure:").append(DateCreditExposure).append("
    ");
    182                 int i=0;
    183                 //最大并发量PEAK_LIMIT,超时timeOut秒
    184                 while(FUNCTIONCOUNT>PEAK_LIMIT && i<timeOut){
    185                     Thread.sleep(1000);
    186                     i++;
    187                 }
    188                 if(FUNCTIONCOUNT<PEAK_LIMIT){
    189                     d1=new Date();
    190                     //访问JCO接口
    191                     partStat=functionCallGetBalance(VendoraccountNumber,CompanyCode,DateCreditExposure);
    192                     if(partStat==null){
    193                         partStat="sku not exist";
    194                     }
    195                     d2=new Date();
    196                     status="OK";
    197                 }else{
    198                     status="reached the maximum number of JCO_PEAK_LIMIT";
    199                     partStat="timeout";
    200                     System.err.println(FUNCTIONCOUNT+"    "+new Date().toLocaleString());
    201                 }
    202 //            }
    203         }catch(Exception e){
    204             //连接失败异常
    205             status=e.getMessage();
    206             e.printStackTrace();
    207         }finally{
    208             StringBuffer sb2=new StringBuffer("");
    209             long responseTime=-1;
    210             if(d1!=null&&d2!=null) responseTime=d2.getTime()-d1.getTime();
    211             sb2.append(sdf2.format(new Date())).append("	").append("ExecID:")
    212             .append(FOLLOWCOUNT).append("	").append("InvocationTime:")
    213             .append(d1==null?"null":sdf.format(d1)).append("	").append("ResponseReceivedTime:")
    214             .append(d2==null?"null":sdf.format(d2)).append("	")
    215             .append("TotalTimeTaken:").append(responseTime).append("	")
    216             .append("Status:").append(status);
    217             lo.writeFile(sb.append(sb2).toString());
    218         }
    219         return partStat;
    220     }
    221     
    222 /*    public static void main(String[] args){
    223         try{
    224             System.out.println("=======AUbegin=============");
    225             String xx = functionCallGetBalance("A1053905A","1","1000053");
    226             String[] res = functionCallcreditExposure("632","AU40","AU40","99991231");
    227             System.out.println("======end==============:");
    228         }catch(Exception e){
    229             e.printStackTrace();
    230         }
    231     }*/
    232 }
  • 相关阅读:
    数据库路由中间件MyCat
    MyCat
    网页动画师与技术开发,如何精准高效的协作完成动效。
    JAVA异常的最佳工程学实践探索
    MySQL导入.sql文件及常用命令
    如何申请新浪SAE,发布自己的网站
    新手教程: 如何在新浪云计算SAE里部署代码
    微信公众平台开发(一) 配置接口
    SQL数据库面试题以及答案
    Sql Server之旅——终点站 nolock引发的三级事件的一些思考
  • 原文地址:https://www.cnblogs.com/sprinng/p/4245112.html
Copyright © 2011-2022 走看看