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封装数据的接口技术.由于其跨平台.跨防火墙.开发成本低.开发周期短等优势 ...
随机推荐
- .net Int16 、(int Int32)、 Int64 的区别
关于什么是16位整数,32位整数,64位整数,请看这里:http://www.cnblogs.com/EasonJim/p/4837061.html Int16 值类型表示值介于 -32768 到 + ...
- 车牌号对应归属地及城市JSON带简码
车牌号对应归属地及城市JSON带简码 car_city.json [ { "code": "冀A", "city": "石家庄&q ...
- hiho1015(kmp+统计出现次数)
http://hihocoder.com/problemset/problem/1015 时隔多天再次温习了一下KMP #include <iostream> #include <c ...
- linux建立用户 详细
.你同时属于两个或两个以上的组. 两个条件你至少具备一个,你才能够把文件所属旧组变为新组.使用如下的命令将当前目录下所有html文件所属的组改为httpd: chgrp httpd *.html 和c ...
- ECSHOP后台商品列表显示商品缩略图
ECSHOP后台商品列表显示商品缩略图 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-06 ecshop 后台商品列表显示商品缩略图,大楷步凑如下: ...
- 初学JDBC,防SQL注入简单示例
在JDBC简单封装的基础上实现 public class UserDao{ public static void testGetUser(String userName) throws Excepti ...
- hdu 2018 母牛的故事
#include<stdio.h> int main(void) { int i,n,j,k; long long narr[60]; narr[1]=1; narr[2]=2; narr ...
- web漏洞总结
目录: 1.sql注入获取数据库信息2.sql注入绕过管理后台登录3.反射型xss4.存储型xss5.csrf6.文件上传7.暴力破解8.目录遍历9.权限跨越10.文件包含11.未知漏洞 web漏洞演 ...
- xss概念剖析
XSS又叫CSS (Cross-Site Scripting) ,跨站脚本攻击.恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意 ...
- SQL中CONVERT转化函数的用法
格式:CONVERT(data_type,expression[,style])说明:此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,c ...