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封装数据的接口技术.由于其跨平台.跨防火墙.开发成本低.开发周期短等优势 ...
随机推荐
- CentOS下yum安装VNCserver
VNC全称是Virtual Network Computing,属于远程控制类软件.其优点是支持跨操作系统的远程图形化控制.在日常工作中,服务器常常是存在机房,不可能每次需要图形界面操作就跑到机房,因 ...
- Jsonp简单认识(后端使用的是asp.net mvc)
一.Jsonp简介:由于浏览器基于安全有同源策略(同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性)机制,所以前端无法使用Ajax来获取来获取其他域名下返回的数据,而Jsonp可 ...
- CodeForces 701A Cards
直接看示例输入输出+提示 1. 统计所有数的和 sum,然后求 sum/(n/2) 的到一半数的平均值 6 1 5 7 4 4 3 ->1+5+7+4+4+3=24 分成3组 每组值为8 in ...
- hdu 2047 阿牛的EOF牛肉串
如果末尾加的是E或F,显然是2*a[i-1] 如果末尾加的是O,则末2位一定是EO或FO,则为2*a[i-2]. 然后两者相加 2*a[i-1]+2*a[i-2] = 2*(a[i-1]+a[i-2] ...
- iOS6新特征:UICollectionView高级使用示例之CircleLayout
DEMO 下面再看看Demo运行的效果图,通过这样的一个Demo,我们可以看出,使用UICollectionView可以很方便的制作出照片浏览等应用.并且需要开发者写的代码也不多. 程序刚刚启 ...
- Android Studio 设置不自动缩进匿名内部类
Android Studio 会默认缩进匿名内部类代码,这让人感觉有些不大适应,可以使用下面的方法进行取消. 取消选中橙色框前的几个复选框即可.
- Objective-C 之优雅的命名(转)
There are only two hard things in Computer Science: cache invalidation and naming things. 在计算机科学中只有两 ...
- git windows中文目录乱码问题解决
转自:http://blog.chinaunix.net/uid-9789774-id-3080448.html Git的Windows版本Msysgit对中文的支持不够好 当使用时,会出现以下三种情 ...
- Eclipse启动Tomcat,45S超时问题解决
在Eclipse中启动Tomcat服务器时,经常由于系统初始化项目多,导致出现45秒超时的Tomcat服务器启动错误. 以前我一般通过找到XML配置文件,将对应Timeout为45的值,修改为 ...
- smb
smb编辑 SMB(Server Message Block)是协议名,它能被用于Wap连接和客户端与服务器之间的信息沟通.