Java(JCo3)与SAP系统相互调用

更新时间:2023-05-12 02:32:26 阅读: 评论:0

Java(JCo3)与SAP系统相互调⽤
外部系统(Java)调⽤BAPI函数
在调⽤BAPI时,SAP为各编程环境(VB、C++、Java等)提供了RFC库及SAP连接器(如Jco、Nco等)。这些类库中的RFC API封闭了外部系统和SAP的连接细节
安装JCo3
JCo有32位和64为之分,32位的JVM选择32位的JCO, 64位的JVM选择64位的JCO, 在windows环境,选择相应的sapjco3.dll, Unix和Linux环境选择合适的sapjco3.so
解压后将sapjco3.dll拷贝到c:/windows/system32与C:\Program Files (x86)\Java\jdk1.7.0_51\bin下,将sapjco3.jar加⼊项⽬的classpath中。
测试安装成功与否,很简单,打开⼀个命令:
/sapjco3.jar
或者
java -cp C:/sapjco3.jar About
创建JCo3连接
JCo连接到SAP服务器有两种⽅法,分别是直连和通过连接池进⾏连接。其差别在于,打开直连连接后可以⼀直保持连接;连接池则是在需要时才建⽴连接,连接暂不需要时,将被释放回连接池,再分配给其他⽤户使⽤。在⽹络服务器应⽤程序⾥,⼀般采⽤连接池进⾏连接SAP服务器。
如果是⽼系统,可能要还注意远程登录⽤户的类型:
直连
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import jco.JCoDestination;
import jco.JCoDestinationManager;
import jco.JCoException;
import DestinationDataProvider;
public class ConnectNoPool {// 直连⽅式,⾮连接池
// 连接属性配置⽂件名,名称可以随便取
static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
static {
Properties connectProperties = new Properties();
connectProperties.tProperty(DestinationDataProvider.JCO_ASHOST,
"192.168.111.137");
connectProperties.tProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties
.
tProperty(DestinationDataProvider.JCO_CLIENT, "800");
connectProperties.tProperty(DestinationDataProvider.JCO_USER,
"SAPECC");
// 注:密码是区分⼤⼩写的,要注意⼤⼩写
connectProperties.tProperty(DestinationDataProvider.JCO_PASSWD,
"sapecc60");
connectProperties.tProperty(DestinationDataProvider.JCO_LANG, "en");
// 需要将属性配置保存属性⽂件,该⽂件的⽂件名为 ABAP_AS_WITHOUT_POOL.jcoDestination,
// Destination()调⽤时会需要该连接配置⽂件,后缀名需要为jcoDestination
createDataFile(ABAP_AS, "jcoDestination", connectProperties);
}
// 基于上⾯设定的属性⽣成连接配置⽂件
static void createDataFile(String name, String suffix, Properties properties) {
File cfg = new File(name + "." + suffix);
if (!ists()) {
try {
FileOutputStream fos = new FileOutputStream(cfg, fal);
properties.store(fos, "for tests only !");
fos.clo();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void connectWithoutPool() throws JCoException {
// 到当前类所在⽬录中搜索 ABAP_AS_WITHOUT_POOL.jcoDestination
// 属性连接配置⽂件,并根据⽂件中的配置信息来创建连接
JCoDestination destination = JCoDestinationManager
.getDestination(ABAP_AS);// 只需指定⽂件名(不能带扩展名jcoDestination名,会⾃动加上)      System.out.println("Attributes:");
// 调⽤destination属性时就会发起连接,⼀直等待远程响应
System.out.Attributes());
}
public static void main(String[] args) throws JCoException {
connectWithoutPool();
}
}
Attributes:
DEST:                  ABAP_AS_WITHOUT_POOL
OWN_HOST:              jiangzhengjun
PARTNER_HOST:          SAPECC6
SYSTNR:                00
SYSID:                ECC
CLIENT:                800
USER:                  SAPECC
LANGUAGE:              E
ISO_LANGUAGE:          EN
OWN_CODEPAGE:          4102
OWN_CHARSET:          UTF16
OWN_ENCODING:          utf-16
OWN_BYTES_PER_CHAR:    2
PARTNER_CODEPAGE:      4103
PARTNER_CHARSET:      UTF16
PARTNER_ENCODING:      utf-16
PARNER_BYTES_PER_CHAR: 2
OWN_REL:              720
PARTNER_REL:          731
PARTNER_TYPE:          3
KERNEL_REL:            720
TRACE:
RFC_ROLE:              C
OWN_TYPE:              E
CPIC_CONVID:          00000000
连接池
程序运⾏结果与上⾯直接是⼀样的
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import jco.JCoDestination;
import jco.JCoDestinationManager;
import jco.JCoException;
import DestinationDataProvider;
public class ConnectPooled {// 连接池
static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static {
Properties connectProperties = new Properties();
connectProperties.tProperty(DestinationDataProvider.JCO_ASHOST,
"192.168.111.137");
connectProperties.tProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties
.tProperty(DestinationDataProvider.JCO_CLIENT, "800");
connectProperties.tProperty(DestinationDataProvider.JCO_USER,
"SAPECC");
// 注:密码是区分⼤⼩写的,要注意⼤⼩写
connectProperties.tProperty(DestinationDataProvider.JCO_PASSWD,
"sapecc60");
connectProperties.tProperty(DestinationDataProvider.JCO_LANG, "en");
// *********连接池⽅式与直接不同的是设置了下⾯两个连接属性
// JCO_PEAK_LIMIT - 同时可创建的最⼤活动连接数,0表⽰⽆限制,默认为JCO_POOL_CAPACITY的值
// 如果⼩于JCO_POOL_CAPACITY的值,则⾃动设置为该值,在没有设置JCO_POOL_CAPACITY的情况下为0
connectProperties.tProperty(DestinationDataProvider.JCO_PEAK_LIMIT,
"10");
// JCO_POOL_CAPACITY - 空闲连接数,如果为0,则没有连接池效果,默认为1
connectProperties.tProperty(
DestinationDataProvider.JCO_POOL_CAPACITY, "3");
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
static void createDataFile(String name, String suffix, Properties properties) {
File cfg = new File(name + "." + suffix);
if (!ists()) {
try {
FileOutputStream fos = new FileOutputStream(cfg, fal);
properties.store(fos, "for tests only !");
fos.clo();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void connectWithPooled() throws JCoException {
JCoDestination destination = JCoDestinationManager
.
getDestination(ABAP_AS_POOLED);
System.out.println("Attributes:");
System.out.Attributes());
}
public static void main(String[] args) throws JCoException {
connectWithPooled();
}
}
DestinationDataProvider接⼝(不需连接属性配置⽂件)
上⾯直接连接、连接池,两种连接⽅法都需要先建⽴⼀个属性配置⽂件,然后JCo再从建⽴好⽂件⾥读取连接到SAP服务器所需要的连接属性,这个⽅法很难在实际的环境中应⽤,存储SAP连接属性配置信息到⼀个⽂件⾥,是⽐较不安全的。然⽽,JCO为我们提供了另外⼀种连接的⽅法:Destination
DataProvider,通过它我们就可以将⼀个连接变量信息存放在内存⾥
import java.util.HashMap;
import java.util.Properties;
import jco.JCoDestination;
import jco.JCoDestinationManager;
import DestinationDataEventListener;
import DestinationDataProvider;
import Environment;
public class CustomSAPDestinationDataProvider {
DestinationDataProvider {
private HashMap<String, Properties>destinations;
private static MyDestinationDataProvider provider = new MyDestinationDataProvider(); private MyDestinationDataProvider() {// 单例模式
if (provider == null) {
destinations = new HashMap<String, Properties>();
}
}
public static MyDestinationDataProvider getInstance() {
return provider;
}
// 实现接⼝:获取连接配置属性
public Properties getDestinationProperties(String destinationName) {
if (ainsKey(destinationName)) {
(destinationName);
} el {
throw new RuntimeException("Destination " + destinationName
+ " is not available");
}
}
public void tDestinationDataEventListener(DestinationDataEventListener eventListener) { this.eL = eventListener;
}
public boolean supportsEvents() {
return true;
}
/**
* Add new destination 添加连接配置属性
*
* @param properties
*            holds all the required data for a destination
**/
void addDestination(String destinationName, Properties properties) {
synchronized (destinations) {
destinations.put(destinationName, properties);
}
}
}
public static void main(String[] args) throws Exception {
// 获取单例
MyDestinationDataProvider myProvider = MyDestinationDataProvider
.getInstance();
// Register the MyDestinationDataProvider 环境注册
// TEST 01:直接测试
// ABAP_AS is the test destination name :ABAP_AS为⽬标连接属性名(只是逻辑上的命名)  String destinationName = "ABAP_AS";
System.out.println("Test destination - " + destinationName);
Properties connectProperties = new Properties();
JCO_ASHOST,
"192.168.111.123");
connectProperties.tProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties
.tProperty(DestinationDataProvider.JCO_CLIENT, "800");
connectProperties.tProperty(DestinationDataProvider.JCO_USER,
"SAPECC");
connectProperties.tProperty(DestinationDataProvider.JCO_PASSWD,
"sapecc60");
connectProperties.tProperty(DestinationDataProvider.JCO_LANG, "en"); // Add a destination
myProvider.addDestination(destinationName, connectProperties);
// Get a destination with the name of "ABAP_AS"
JCoDestination DES_ABAP_AS = JCoDestinationManager
.getDestination(destinationName);
// Test the destination with the name of "ABAP_AS"
try {
DES_ABAP_AS.ping();
System.out.println("Destination - " + destinationName + " is ok");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Destination - " + destinationName
+ " is invalid");
}
// TEST 02:连接池测试
// Add another destination to test
// ABAP_AS2 is the test destination name
String destinationName2 = "ABAP_AS2";
System.out.println("Test destination - " + destinationName2);
Properties connectProperties2 = new Properties();
connectProperties2.tProperty(DestinationDataProvider.JCO_ASHOST, "192.168.111.123");
connectProperties2.tProperty(DestinationDataProvider.JCO_SYSNR, "00");  connectProperties2
.tProperty(DestinationDataProvider.JCO_CLIENT, "800");
connectProperties2.tProperty(DestinationDataProvider.JCO_USER, "SAPECC");
connectProperties2.tProperty(DestinationDataProvider.JCO_PASSWD, "sapecc60");
connectProperties2.tProperty(DestinationDataProvider.JCO_LANG, "en");  connectProperties2.tProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");
connectProperties2.tProperty(
DestinationDataProvider.JCO_POOL_CAPACITY, "3");
// Add a destination
myProvider.addDestination(destinationName2, connectProperties2);
// Get a destination with the name of "ABAP_AS2"
JCoDestination DES_ABAP_AS2 = JCoDestinationManager
.getDestination(destinationName2);
// Test the destination with the name of "ABAP_AS2"
try {
DES_ABAP_AS2.ping();
System.out.println("Destination - " + destinationName2 + " is ok");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Destination - " + destinationName2
+ " is invalid");
}
}
}
访问结构 (Structure)
accessSAPStructure() throws JCoException {
JCoDestination destination = JCoDestinationManager
.getDestination(ABAP_AS);
JCoFunction function = Repository().getFunction( "");从对象仓库中获取 RFM 函数
if (function == null)

本文发布于:2023-05-12 02:32:26,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/885924.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:连接   属性   配置
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图