java call sap

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

  下载地址点我

2、代码:

  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("\t").append("VendoraccountNumber:")
180             .append(VendoraccountNumber).append("\t").append("CompanyCode:").append(CompanyCode)
181             .append("\t").append("DateCreditExposure:").append(DateCreditExposure).append("\r\n");
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("\t").append("ExecID:")
212             .append(FOLLOWCOUNT).append("\t").append("InvocationTime:")
213             .append(d1==null?"null":sdf.format(d1)).append("\t").append("ResponseReceivedTime:")
214             .append(d2==null?"null":sdf.format(d2)).append("\t")
215             .append("TotalTimeTaken:").append(responseTime).append("\t")
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 }
时间: 2024-07-30 05:17:05

java call sap的相关文章

java调用sap的webservice(需要登录验证)

1.Base64.java /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License a

JAVA heap space 内存溢出

3月28日开始使用JAVA调用SAP RFC后提示内存溢出 尝试增加set java_OPTS.....但仍然提示内存溢出 最终设置方法

ABAP和Java的单元测试Unit Test

ABAP ABAP class单元测试的执行入口,CLASS_SETUP, 是硬编码在单元测试框架实现CL_AUNIT_TEST_CLASS里的. 待执行的单元测试方法通过CL_AUNIT_TEST_CLASS-get_Test_Methods返回到一个内表里,然后LOOP该内表,依次执行. Java 最常用的Maven plugin是maven-surefire-plugin,可以方便地单独触发单元测试,或者把单元测试的执行集成为Maven build的一个子步骤. 下图表明在执行mvn cl

【ABAP系列】SAP Web Dynpro 技术简介

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP Web Dynpro 技术简介 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 AP webdynpro是为SAP NetWeaver战略UI编程模型. 它是用于创建平台并独立于语言,基于WEB的用户界面. Web Dynpro可用于SAP NetWeaver Application Serv

“在什么时候学习编程才合适?”

40岁后学习编程是否太晚了?7点技巧让学习变得轻松有趣 很多人经常会问:“在什么时候学习编程才合适?”,甚至有很多在职场中打滚了多年的人也会有这样的疑问.他们当中有很多人都想把自己的专业跳到编程专业,原因是多方面的,其中包括了他们对自动化的畏惧,而这个问题在40岁以上的人群中变得更具针对性. 编程是一项很花脑力.精力的高压工作,在40岁后才开始学习编程,是不是太晚了? Damian Wolf给出的答案是“不会晚”!Damian是InfoWorld,DZone,Life Hack等多家数字科技杂志

【自动化测试】自动化工具之QTP

1.QTP HP QuickTest Professional 提供符合所有主要应用软件环境的功能测试和回归测试的自动化.采用关键字驱动的理念以简化测试用例的创建和维护.它让用户可以直接录制屏幕上的操作流程,自动生成功能测试或者回归测试用例.专业的测试者也可以通过提供的内置脚本和调试环境来取得对测试和对象属性的完全控制. 特点: 1)QTP是一个侧重于功能的回归自动化测试工具:提供了很多插件,如:.NET的,Java的,SAP的,Terminal Emulator的等等,分别用于各自类型的产品测

JCo 指南

http://blog.csdn.net/asdfak/article/details/5834731 Java Connector and BAPI 前些日子想去深入的研究下Portal Development,但是载了一个周末也没有把NetWeaver Developer Studio弄下来.闲暇时间,浏览了下JCo的API文档和几个简单的DEMO,开始尝试着自己去用JCo做一些操作,并记录下整个过程,以供大家参考. 一些理论性的东西源自SAP的官方培训教材和一些高手的笔记,英文不佳,翻译的

40岁后学习编程是否太晚了?7点技巧让学习变得轻松有趣

很多人经常会问:“在什么时候学习编程才合适?”,甚至有很多在职场中打滚了多年的人也会有这样的疑问.他们当中有很多人都想把自己的专业跳到编程专业,原因是多方面的,其中包括了他们对自动化的畏惧,而这个问题在40岁以上的人群中变得更具针对性. 编程是一项很花脑力.精力的高压工作,在40岁后才开始学习编程,是不是太晚了? Damian Wolf给出的答案是“不会晚”!Damian是InfoWorld,DZone,Life Hack等多家数字科技杂志媒体的专栏作家,他本人也是一个科技迷,也正在想学习编程.

sapjco3 开发与部署环境设置

windows 环境设置 1.sapjco3.dll 需要与 sapjco3.jar 在同一目录 2.设置系统环境变量,将sapjco3所在目录加入系统环境变量 例如: 新建环境变量 变量名: JAVA_SAPJCO 变量值: E:\sapjco3\sapjco3-win32 将新建的 JAVA_SAPJCO 环境变量加入 系统环境变量 Path变量集合中. %JAVA_SAPJCO%\sapjco3.jar 3.项目部署运行 将 sapjco3.dll 加入到c:/windows/system