设为首页 收藏本站
查看: 680|回复: 0

[经验分享] java call sap

[复制链接]

尚未签到

发表于 2015-9-19 05:58:33 | 显示全部楼层 |阅读模式
  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+",");
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 }
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-115537-1-1.html 上篇帖子: [SAP ABAP开发技术总结]消息处理Messages 下篇帖子: SAP 中的用户类型
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表