您的位置:首页 > 编程语言 > Java开发

JAVA使用JCO实现调用SAP接口方法

2017-07-03 15:47 1086 查看

一、配置连接

(1)第一种连接配置

1.根据操作系统将sapjco3.dll文件放入C:\Windows\System32目录;

2.在项目中引入sapjco3.jar包;

3.创建DisplaySalesActivity.java类,配置生成连接

public class DisplaySalesActivity {
static String DESTINATION_NAME1 = "ABAP_AS_WITHOUT_POOL";
static String DESTINATION_NAME2 = "ABAP_AS_WITH_POOL";
//配置连接
static {
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "127.0.0.1");//IP
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");//系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "001");//客户端编号
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "username");//用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "password");//密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh");//语言
createDestinationDataFile(DESTINATION_NAME1, connectProperties);
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "30");//最大空闲连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "150");//最大活动连接数
createDestinationDataFile(DESTINATION_NAME2, connectProperties);
}
/*
*创建配置连接文件
/
static void createDestinationDataFile(String destinationName, Properties connectProperties) {
File destCfg = new File(destinationName + ".jcoDestination");
try {
FileOutputStream fos = new FileOutputStream(destCfg, false);
connectProperties.store(fos, "For tests only !");
fos.close();
} catch (Exception e) {
throw new RuntimeException("Unable to create the destination files", e);
}
}

/*
*获取连接
/
public static JCoDestination getSAPDestination() throws JCoException {
JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);

return destination;

}

public static void main(String[] args) throws JCoException {
getSAPDestination();
}


配置好后运行main方法,生成2个连接配置文件(DESTINATION_NAME1 与DESTINATION_NAME2 )和2个日志文件,如果

在Tomcat中运行,配置好生成地址可以直接在应用中调连接方法。

(2)第二种连接配置

1.配置环境:

下载sapjco3.dll插件置放在JDK或Tomcat的bin文件夹下

下载sapjco3.jar置放工程中

2.配置访问属性文件

SAPConnectionPool.properties

jco.client.lang=zh #语言

jco.destination.peak_limit=150 #最大活动连接数

jco.client.client=001 #客户端编号

jco.client.passwd=password #密码

jco.client.user=username #用户名

jco.client.sysnr=00 #系统编号

jco.destination.pool_capacity=30 #最大空闲连接数

jco.client.ashost=127.0.0.1 #IP

3.封装获取链接池链接Java类,以便接口使用时调用

SAPConnectionPool类:

@Service("SAPConnectionPool")
public class SAPConnectionPool {
private static final String SAP_CONN="SAP_CONN";

public static JCoDestination getSAPDestination(){
try {
JCoDestination dest = JCoDestinationManager.getDestination(SAP_CONN);
return dest;
} catch (JCoException ex) {
//System.out.println(ex);
//System.out.println("连接失败,重新连接!");
//重新连接
return RegetJocodestination();
}
}

/*****

* 函数功能描述:重新获取JCODestination
* @return
****
*/
public static JCoDestination RegetJocodestination(){
try{
Properties properFile = new Properties();
ClassLoader cl=Thread.currentThread().getContextClassLoader();
String filePath=cl.getResource("").toString()+"SAPConnectionPool.properties";
filePath=filePath.replace("file:", "");
filePath=filePath.replace("%20", " ");//替换文件名包含空格的
FileInputStream inputFile = new FileInputStream(filePath);
properFile.load(inputFile);
inputFile.close();

Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, properFile.getProperty("jco.client.ashost"));
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  properFile.getProperty("jco.client.sysnr"));
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, properFile.getProperty("jco.client.client"));
connectProperties.setProperty(DestinationDataProvider.JCO_USER,   properFile.getProperty("jco.client.user"));
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, properFile.getProperty("jco.client.passwd"));
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, properFile.getProperty("jco.destination.pool_capacity"));
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,   properFile.getProperty("jco.destination.peak_limit"));
connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   properFile.getProperty("jco.client.lang"));

CustomDestinationDataProvider provider = new CustomDestinationDataProvider();
provider.add
4000
DestinationProperties(SAP_CONN, connectProperties);
Environment.registerDestinationDataProvider(provider);
try {
JCoDestination dest = JCoDestinationManager.getDestination(SAP_CONN);
return dest;
} catch (JCoException ex) {
System.out.println(ex);
System.out.println("重新连接失败");
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}

}


CustomDestinationDataProvider类

public class CustomDestinationDataProvider implements DestinationDataProvider {
private Map providers = new HashMap();
@Override
public Properties getDestinationProperties(String destName) {
if (destName == null)
throw new NullPointerException("请指定目的名称");
if (providers.size() == 0)
throw new IllegalStateException("请加入一个目的连接参数属性给提供者");
return (Properties)providers.get(destName);
}

// 没有实现事件处理
@Override
public boolean supportsEvents(){
return false;
}

@Override
public void setDestinationDataEventListener(DestinationDataEventListener listener) {
throw new UnsupportedOperationException();
}

public void addDestinationProperties(String destName, Properties provider) {
providers.put(destName, provider);
}
}


二、接口调用

JCO采用的是中间式接口,即外部系统将SAP系统所需的信息生成中间数据表,SAP系统直接读取中间文件或将中间表中的信息写入数据库中,中间式接口是比较常用的一种方式,这种方式外部系统和SAP系统相对独立,接口不涉及双方内部的结构,而且接口的责任也很明确,数据的安全性也得到了保证。但这种方式存在的问题就是两个系统的数据同步性稍差一些,但只要合理地规定读写中间文件或数据表的时间,数据的同步性是不会影响使用的。

以下是我在项目中用到的参数传递的方法调用,应该基本满足部分接口调用,例:

JCoDestination destination =SAPConnectionPool.getSAPDestination();//获取连接
//返回一个JCoFunction初始参数的传递函数名。获取接口方法
JCoFunction function = destination.getRepository().getFunction("Function_NAME");

//1.单独的参数,不在表结构下
function.getImportParameterList().setValue("NAME1", "VALUE1");// 参数

//2.JCoStructure 一般为HEADER参数
JCoStructure Structure=function.getImportParameterList().getStructure("_HEADER");
Structure.setValue("NAME1", "VALUE1");//参数
Structure.setValue("NAME2", "VALUE2");//参数

//3.JCoTable 主体参数,可为多个主体参数。。。
JCoTable headerImportParam = function.getTableParameterList().getTable("_TABLE");//返回的值i个字段作为一个表
//如果为参数集合,在外层加for循环即可
headerImportParam.appendRow();//附加表的最后一个新行,行指针,它指向新添加的行。
headerImportParam.setValue("NAME1", "VALUE1");//参数
headerImportParam.setValue("NAME2", "VALUE2");//参数

//执行接口
function.execute(destination);

//接口返回结果
JCoTable returnStructure = function.getTableParameterList().getTable("TD_RETURN");
for (int i = 0; i < returnStructure.getNumRows(); i++) {
returnStructure.setRow(i);
System.out.println(returnStructure.getString("Param1"));
System.out.println(returnStructure.getString("Param2"));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐