java call sap

1、下载需要的jar,windows用dll,linux用so

  win下载地址     linux下载地址 win下载地址new

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();
}
}*/
}
上一篇:inux 驱动程序开发中输入子系统总共能产生哪些事件类型(EV_KEY,EV_ABS,EV_REL)


下一篇:记在centos中连接无线网络的一次过程