java call sap
1、下载需要的jar,windows用dll,linux用so
2、环境:
windows -> sapjco3.dll放到windows目录下
linux -> libsapjco3.so 放到lib目录下(没权限就建个环境变量让后放进去)
3、代码:
package cn.com.sony.newsis.common.tools; import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap; import cn.com.sony.npc.util.Props.PropsUtil; import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCo;
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.JCoParameterList;
import com.sap.conn.jco.ext.DestinationDataProvider; public class AUJcoClient
{
static String ABAP_AS_POOLED = "AU_AS_WITH_POOL";
static String[] jcoPara=Operate.getAUJcoPara();
static int timeOut=Integer.parseInt(jcoPara[8]);
public static int FUNCTIONCOUNT=0;
public static long FOLLOWCOUNT=0;
public static int PEAK_LIMIT=Integer.parseInt(jcoPara[7]); public static ConcurrentHashMap<Long,Long> hm=new ConcurrentHashMap<Long,Long>(); /**
* initial JCO connection
*/
static{
for(int i=0;i<jcoPara.length;i++){
System.out.print(jcoPara[i]+",");
}
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, jcoPara[0]);
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, jcoPara[1]);
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, jcoPara[2]);
connectProperties.setProperty(DestinationDataProvider.JCO_USER, jcoPara[3]);
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, jcoPara[4]);
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, jcoPara[5]);
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, jcoPara[6]);
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, jcoPara[7]); createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties); } static void createDataFile(String name, String suffix, Properties properties){
File cfg = new File(name+"."+suffix);
if(!cfg.exists()){
FileOutputStream fos = null;
try{
fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
}catch (Exception e){
//Point 4 – handle this exception
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}finally{
try{
if(fos!=null) fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
} /**
* access JCO Interface 'Z_CREDIT_EXPOSURE'
* @param customerAccountNumber
* @param companyCode
* @param creditControlArea
* @param dateCreditExposure
* @return
* @throws JCoException
*/
public static String[] functionCallcreditExposure(String customerAccountNumber,String companyCode,String creditControlArea,String dateCreditExposure) throws Exception{
String[] res = new String[3];
String creditLimit = null;
String Receivable = null;
String creditExposure = null;
try{
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
JCoFunction function = destination.getRepository().getFunction("Z_CREDIT_EXPOSURE");
JCoParameterList inParm = function.getImportParameterList();
inParm.setValue("KUNNR", "4002485");
inParm.setValue("BUKRS", "AU40");
inParm.setValue("KKBER","AU40");
inParm.setValue("DATE_CREDIT_EXPOSURE","99991231");
FUNCTIONCOUNT++;
function.execute(destination);
FOLLOWCOUNT++;
if(FOLLOWCOUNT>100000) FOLLOWCOUNT=0;
hm.put(new Long(FOLLOWCOUNT), new Date().getTime()/1000);
creditExposure = function.getExportParameterList().getString("SUM_OPENS").toString();
Receivable = function.getExportParameterList().getString("OPEN_ITEMS").toString();
creditLimit = function.getExportParameterList().getString("CREDITLIMIT").toString();
res[0] = creditExposure;
res[1] = Receivable;
res[2] = creditLimit;
}catch (JCoException e) {
throw e;
}finally{
FUNCTIONCOUNT--;
}
return res;
} /**
* access JCO Interface 'Y_RFC_AP_ACC_GETKEYDATEBALANC'
* @param material String
* @param req_qty String
* @param customer String
* @return String part status
*/
public static String functionCallGetBalance(String vendorAccountNumber,String companyCode,String dateCreditExposure) throws Exception{
String vendorBalance = null;
try{
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
JCoFunction function = destination.getRepository().getFunction("Y_RFC_AP_ACC_GETKEYDATEBALANC");
JCoParameterList inParm = function.getImportParameterList();
inParm.setValue("VENDOR", vendorAccountNumber);
inParm.setValue("COMPANYCODE", companyCode);
inParm.setValue("KEYDATE",dateCreditExposure);
FUNCTIONCOUNT++;
function.execute(destination);
FOLLOWCOUNT++;
if(FOLLOWCOUNT>100000) FOLLOWCOUNT=0;
hm.put(new Long(FOLLOWCOUNT), new Date().getTime()/1000);
vendorBalance = function.getExportParameterList().getString("LC_BAL").toString();
} catch(AbapException e) {
System.out.println(e.toString());
}finally{
FUNCTIONCOUNT--;
}
return vendorBalance;
} /**
* Control Maximum concurrency
* @param material String
* @param req_qty String
* @param customer String
*@return part status
*/
public static Object functionControlGetBalance(String VendoraccountNumber,String CompanyCode,String DateCreditExposure) throws Exception { String status=null;
Object partStat=null;
Date d1=null;
Date d2=null;
String path= PropsUtil.get("JCO_ROOT");
try{
/*
* WAS的自动重启机制会产生"Library is already loaded in another ClassLoader"的bug,必须黑屏重启jvm
* Java虚拟机为了在JNI本地库中确保基于classloader的命名空间隔离,因而不允许一个JNI本地库被两个不同的classloader加载。
* 只要将JNI class单独发布成jar包,并放在配置公共(default、all或share)的lib目录中,问题就可以解决
*/
JCo.setTrace(0, path);
}catch (Exception e) {
System.err.println("Jco loader error:"+e);
} LayOut lo=new LayOut(path+"/AUJcoPrint.log"); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
StringBuffer sb=new StringBuffer(""); try{
sb.append(sdf2.format(new Date())).append("\t").append("VendoraccountNumber:")
.append(VendoraccountNumber).append("\t").append("CompanyCode:").append(CompanyCode)
.append("\t").append("DateCreditExposure:").append(DateCreditExposure).append("\r\n");
int i=0;
//最大并发量PEAK_LIMIT,超时timeOut秒
while(FUNCTIONCOUNT>PEAK_LIMIT && i<timeOut){
Thread.sleep(1000);
i++;
}
if(FUNCTIONCOUNT<PEAK_LIMIT){
d1=new Date();
//访问JCO接口
partStat=functionCallGetBalance(VendoraccountNumber,CompanyCode,DateCreditExposure);
if(partStat==null){
partStat="sku not exist";
}
d2=new Date();
status="OK";
}else{
status="reached the maximum number of JCO_PEAK_LIMIT";
partStat="timeout";
System.err.println(FUNCTIONCOUNT+" "+new Date().toLocaleString());
}
// }
}catch(Exception e){
//连接失败异常
status=e.getMessage();
e.printStackTrace();
}finally{
StringBuffer sb2=new StringBuffer("");
long responseTime=-1;
if(d1!=null&&d2!=null) responseTime=d2.getTime()-d1.getTime();
sb2.append(sdf2.format(new Date())).append("\t").append("ExecID:")
.append(FOLLOWCOUNT).append("\t").append("InvocationTime:")
.append(d1==null?"null":sdf.format(d1)).append("\t").append("ResponseReceivedTime:")
.append(d2==null?"null":sdf.format(d2)).append("\t")
.append("TotalTimeTaken:").append(responseTime).append("\t")
.append("Status:").append(status);
lo.writeFile(sb.append(sb2).toString());
}
return partStat;
} /* public static void main(String[] args){
try{
System.out.println("=======AUbegin=============");
String xx = functionCallGetBalance("A1053905A","1","1000053");
String[] res = functionCallcreditExposure("632","AU40","AU40","99991231");
System.out.println("======end==============:");
}catch(Exception e){
e.printStackTrace();
}
}*/
}
java call sap的更多相关文章
- (一)通过JAVA连接SAP (sapjco3.jar在Windows和MacOS上的配置)
(一)通过JAVA连接SAP调用接口 (sapjco3.jar在Windows和MacOS上的配置) 一.sapjoc3.jar获取 由于sap官网提供的链接需要合作公司提供账号密码,如果商用请索要正 ...
- (二)通过JAVA调用SAP接口 (增加一二级参数)
(二)通过JAVA调用SAP接口 (增加一二级参数) 一.建立sap连接 请参考我的上一篇博客 JAVA连接SAP 二.测试项目环境准备 在上一篇操作下已经建好的环境后,在上面的基础上新增类即可 三. ...
- JAVA连接SAP
1.首先需要在SAP事务码SE37中新建一个可以被远程调用的RFC 事务码:SE37 新建一个函数组:输入事务码SE37回车后,来到函数构建器屏幕,到上面一排菜单栏:转到 -> 函数组 -> ...
- 最新.net和Java调用SAP RFC中间件下载
还记得2012年初我发布的全网络第一个关于.net 连接SAP RFC的NCO3原创博文,用的就是SAP出的最新的.Net Connector 3.0的版本,在那个时候都是普遍用其他蹩脚的方式或Web ...
- java调用sap的webservice(需要登录验证)
1.Base64.java /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache ...
- java实现SAP BO登录
最近一个项目用到了SAP的businessObjects,需要进行二次开发,今天开发了登录接口,遇到了一些问题,进行了解决,现在分享一下. 1.依赖jar包的添加 bo登录需要用到一些jar包,具体在 ...
- Java连接Sap系统调并调用RFC函数
参考博客:https://blog.csdn.net/qq_36026747/article/details/81287462 https://www.cnblog ...
- java 调用SAP RFC函数错误信息
RFC接口调用SAP如果有异常会通过com.sap.mw.jco.JCO$Exception: 抛出异常 在开发中遇到的异常有如下 用户名密码可能是错误或者用户无权限,确认用户,必要时联系SAP负责人 ...
- SAP和Java系统的Webservice实例
简介: 关于Webservice的概念和原理,简单来讲,Webservice是一种基于SOAP传输协议,用WSDL描述,用XML封装数据的接口技术.由于其跨平台.跨防火墙.开发成本低.开发周期短等优势 ...
随机推荐
- RedGate .NET Reflector注册问题(反注册)
Reflector分为桌面版和VS集成版本,当我们使用注册机注册的时候如果注册了Standvard版本,那么我们的VS就不能集成查看,也不能Debug,那么这 显然不是我们想要的,我们会选择重新注册, ...
- Android APK反编译问题
因为这几天想做一个离线音乐播放器,然后需要很多.png图片,在度娘上搜图片感觉效果不好, 就需要用到反编译工具来将一些.apk文件中的资源文件提取出来. 这里就只讲下怎么使用工具来进行反编译,什么?你 ...
- 从js的repeat方法谈js字符串与数组的扩展方法
js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...
- sencha touch pull-refresh-panel 面板下拉刷新
转自:http://www.cnblogs.com/mlzs/archive/2013/06/04/3117518.html 此效果手机未测试,目测没问题,我是搬运工... 演示地址:http://s ...
- Linux下web目录权限设置
1.nginx和php-fpm运行用户为www 2.我们假设web目录所属着为web_owner 3.将web目录的用户和用户组设置为web_owner和www,如下命令:chown -R web_o ...
- 5个常用Java代码混淆器 助你保护你的代码
[IT168 技术文档] 从事Java编程的人都知道,可以通过逆向工程反编译得到Java程序的源代码,这种反编译工具之一就是JAD.因此,为保护我们的劳动成果,尽可能给反编译人员制造障碍,我们可以使用 ...
- Ueditor配置及在项目中的使用
引言 上篇中简单介绍了Ueditor的两种定制方式,想了解的请戳这里:Ueditor的两种定制方式.在项目中,Ueditor该怎么使用更方便呢?很容易让人想到将ueditor放入用户控件页,可以拖到需 ...
- linux 的useradd 命令的p选项
linux 的useradd 命令的p选项 错误用法: #useradd gaojian -p gaojian # ...
- sublime Text 3实用功能和常用快捷键收集
下面是我通过网上视频教程或文本资料学习sublime Text3时收集的一些实用功能和常用快捷键,现在分享出来,如果还有其它的好用的功能可以在下面留言,以便互相学习. PS:ST3在Mac OX与Wi ...
- 前端框架react研究
摘要: 最近公司要做一个嵌套在app中的应用,考虑着用Facebook的react来开发view,所以就研究了下.下面是我在开发中遇到的坑,希望能给你帮助. 项目地址:https://github.c ...