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封装数据的接口技术.由于其跨平台.跨防火墙.开发成本低.开发周期短等优势 ...
随机推荐
- Erlang第一课----基本变量
1.(in Erlang shell)A sequence of expressions must be terminated with a period followed by whitespace ...
- ExtJS入门教程02,form也可以很优雅
在上一篇<Extjs window 入门>中,我们已经看到了如何将一个form组件放到window中,今天我们来看看form的一些优雅的工作方式. 使用fieldDefaults,优雅的设 ...
- MyEclipse------如何在特定目录下创建文件夹
Directory.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
- Mongo报如下类似错误时的修改方法Cannot natively represent the long 1396367483000 on this platform
今天通过php连接mongodb,并读取数据时,其中一个字段为: "createTime":NumberLong("1397524645000"),由于太长,一 ...
- strstr()
char * __cdecl strstr ( const char * str1, const char * str2 ) { char *cp = (char *) str1; char *s1, ...
- 编写自己的Acunetix WVS漏洞扫描脚本详细教程
AWVS提供了自定义的脚本编程接口,可是网上的资料很少,只有官方的几篇介绍和参考手册,最近研究了一下怎么编写AWVS的漏洞脚本来写一篇简单性的文章 本文以8.0为例,首先呢安装好Acunetix We ...
- xss绕过过滤之方法
很多网站为了避免XSS的攻击,对用户的输入都采取了过滤,最常见的就是对<>转换成<以及>,经过转换以后<>虽然可在正确显示在页面上,但是已经不能构成代码语句了.这个 ...
- 使用SqlSessionTemplate实现数据库的操作
EmployeeMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE map ...
- linux 的终端字体色和背景色的修改方法(三)
除了在窗口下修改,配置文件中修改外,还可以用shell来修改,此处为B shell linux BASH shell下设置字体及背景颜色 类型:转载 这篇文章主要介绍了linux BASH shell ...
- apache配置多个虚拟主机
设置apache 多个虚拟目录记录 #配置第2个虚拟目录<VirtualHost 127.0.0.2:80>ServerName www.xx.comDocumentRoot " ...