java rfc接口_java调用sap的RFC接口
sap目前是世界上最大的也是使用最多的ERP系统,很多大型系统都将自己的业务数据放到了SAP系统来进行管理,那么当别的系统需要这些数据时,就需要从SAP中获取这些数据。SAP中有各种不同类型的接口,RFC,PI等等。下面记录的是java如何调用RFC的接口。网上可以找到很多类似的文章,代码也是以前的老手写的,也比较易懂,这里再记下来主要是为了以后找起来方便。
java调用RFC接口需要用到sapjco3.jar,windows下还需要将文件sapjco3.dll文件放到system32的目录下,linux下同样需要把sapjco3.so放入项目的执行目录下。代码如下:
JOCTest:
package jco;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
import java.util.ArrayList;
import java.util.List;
public class JCOTest {
public static void main(String[] args)
{
getUser();
}
public static List getUser() {
JCoFunction function = RfcManager.getFunction("FUNCION_USER");
RfcManager.execute(function);
JCoParameterList outputParam = function.getTableParameterList();
JCoTable bt = outputParam.getTable("TABLEOUT");
List list = new ArrayList();
for (int i = 0; i < bt.getNumRows(); i++) {
bt.setRow(i);
User user = new User();
user.setUserName(bt.getString("USER_NAME"));
list.add(user);
}
return list;
}
}RfcManager:
package jco;
import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.Environment;
import java.io.IOException;
import java.util.Properties;
public final class RfcManager {
private static final String ABAP_AS_POOLED = "ABAP_AS_POOL";
private static JCOProvider provider;
private static JCoDestination destination;
static {
Properties properties = loadProperties();
// catch IllegalStateException if an instance is already registered
try {
provider = new JCOProvider();
Environment.registerDestinationDataProvider(provider);
provider.changePropertiesForABAP_AS(ABAP_AS_POOLED, properties);
} catch (IllegalStateException e) {
System.out.println(e.getMessage());
}
}
public static Properties loadProperties() {
Properties props=new Properties();
props.setProperty("jco.client.user","value");
props.setProperty("jco.client.passwd","value");
props.setProperty("jco.client.lang", "value");
props.setProperty("jco.client.client","value");
props.setProperty("jco.client.sysnr","value");
props.setProperty("jco.client.ashost","value");
props.setProperty("jco.destination.peak_limit","value");
props.setProperty("jco.destination.pool_capacity","value");
return props;
}
public static JCoDestination getDestination() throws JCoException {
if (destination == null) {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
}
return destination;
}
public static void execute(JCoFunction function) {
System.out.println("SAP Function Name : " + function.getName());
try {
function.execute(getDestination());
} catch (JCoException e) {
e.printStackTrace();
}
}
public static JCoFunction getFunction(String functionName) {
JCoFunction function = null;
try {
function = getDestination().getRepository().getFunctionTemplate(functionName).getFunction();
} catch (JCoException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return function;
}
}
package jco;
import com.sap.conn.jco.ext.*;
import java.util.HashMap;
import java.util.Properties;
public class JCOProvider implements DestinationDataProvider,SessionReferenceProvider {
private HashMap secureDBStorage = new HashMap();
private DestinationDataEventListener eL;
@Override
public Properties getDestinationProperties(String destinationName) {
try
{
//read the destination from DB
Properties p = secureDBStorage.get(destinationName);
if(p!=null)
{
//check if all is correct, for example
if(p.isEmpty()){
System.out.println("destination configuration is incorrect!");
}
return p;
}
System.out.println("properties is null ...");
return null;
}
catch(RuntimeException re)
{
System.out.println("internal error!");
return null;
}
}
@Override
public void setDestinationDataEventListener(
DestinationDataEventListener eventListener) {
this.eL = eventListener;
System.out.println("eventListener assigned ! ");
}
@Override
public boolean supportsEvents() {
return true;
}
//implementation that saves the properties in a very secure way
public void changePropertiesForABAP_AS(String destName, Properties properties) {
synchronized(secureDBStorage)
{
if(properties==null)
{
if(secureDBStorage.remove(destName)!=null)
eL.deleted(destName);
}
else
{
secureDBStorage.put(destName, properties);
eL.updated(destName); // create or updated
}
}
}
public JCoSessionReference getCurrentSessionReference(String scopeType) {
RfcSessionReference sesRef = JcoMutiThread.localSessionReference.get();
if (sesRef != null)
return sesRef;
throw new RuntimeException("Unknown thread:" + Thread.currentThread().getId());
}
public boolean isSessionAlive(String sessionId) {
return false;
}
public void jcoServerSessionContinued(String sessionID)
throws SessionException {
}
public void jcoServerSessionFinished(String sessionID) {
}
public void jcoServerSessionPassivated(String sessionID)
throws SessionException {
}
public JCoSessionReference jcoServerSessionStarted() throws SessionException {
return null;
}
}
package jco;
import com.sap.conn.jco.ext.JCoSessionReference;
import java.util.concurrent.atomic.AtomicInteger;
public class RfcSessionReference implements JCoSessionReference {
static AtomicInteger atomicInt = new AtomicInteger(0);
private String id = "session-" + String.valueOf(atomicInt.addAndGet(1));;
public void contextFinished() {
}
public void contextStarted() {
}
public String getID() {
return id;
}
}
package jco;
public interface IMultiStepJob {
public boolean runNextStep();
String getName();
public void cleanUp();
}
package jco;
import java.util.Hashtable;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class JcoMutiThread extends Thread {
public static Hashtable sessions = new Hashtable();
public static ThreadLocal localSessionReference = new ThreadLocal();
private BlockingQueue queue ;
private CountDownLatch doneSignal;
private boolean isSapBusy = false;
public JcoMutiThread(CountDownLatch doneSignal, BlockingQueue queue) {
this.doneSignal = doneSignal;
this.queue = queue;
}
@Override
public void run() {
try {
for (;;) {
IMultiStepJob job = queue.poll(10, TimeUnit.SECONDS);
// stop if nothing to do
if (job == null){
break;
}
if(isSapBusy){
Thread.sleep(5000);
}
RfcSessionReference sesRef = sessions.get(job);
if (sesRef == null) {
sesRef = new RfcSessionReference();
sessions.put(job, sesRef);
}
localSessionReference.set(sesRef);
//Thread Started ("Task " + job.getName() + " is started.");
try {
isSapBusy = job.runNextStep();
} catch (Throwable th) {
th.printStackTrace();
}
if(isSapBusy){
//sap system busy, try again later("Task " + job.getName() + " is passivated.");
queue.add(job);
}else{
//" call sap finished, Task " + job.getName() ;
sessions.remove(job);
job.cleanUp();
}
localSessionReference.set(null);
}
} catch (InterruptedException e) {
// just leave
} finally {
doneSignal.countDown();
}
}
}
java rfc接口_java调用sap的RFC接口相关推荐
- java web短信接口_Java调用WebService短信接口-Go语言中文社区
一.WebService接口说明 以http post的形式进行发送,上面是请求信息,下面是返回值. 接口文档返回值部分介绍. 二.Java代码,其中的MD5工具类请参照我的其他文章,或自己找一个MD ...
- php调用sap端rfc接口,SAP 接口编程之 RFC 系列(15) : 调用自定义函数
从外部环境调用自定义函数 (FM, Function Module)与调用 SAP 内置的函数并没有不同,需要注意的是在 SAP 系统自定义 FM 的要点: 1)FM 必须是 Remote-enabl ...
- JAVA客户端调用SAP提供的接口(保姆级教程)
目前接触到SAP提供了两种不同的接口,对应也有两种不同的调用方式:1.Restful接口,支持直接通过post请求调用:2.WSDL接口,需要使用SoapUI测试并生成代码调用.大家在对接前要先确认接 ...
- java版微信朋友圈_java调用个人微信API接口发朋友圈,评论和删除朋友圈
java调用个人微信API接口发朋友圈,评论和删除朋友圈 /** * 发送朋友圈任务 * @author wechatno:tangjinjinwx * @param ctx * @param vo ...
- java中抽象类 接口_java中的抽象类与接口
1.抽象类的定义 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 2.简单实例 ...
- java callable接口_Java多线程之Callable接口的实现有返回值的线程
import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util. ...
- java 组件 接口_java学习之图形用户接口AWT组件部分
记录学习Java私塾Java视频培训教程中级-9 图形用户接口AWT组件部分 学习视频资源分享: 地址:http://pan.baidu.com/s/1eSDiZAi 密码:lvy6 PS:视频的画质 ...
- java实现多个接口_java允许实现多个接口
java不允许多重继承,也就是说一个子类只能有一个父类,Son extends FatherA,FatherB 是错误的为了弥补这点不 足,java允许实现多个接口, 接口就是给出一些没有内容的方法, ...
- java中如何定义接口_java中如何定义一个接口
使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,其中接口体由常量定义和方法定义两部分组成.定义接口的基本格式如下:[修饰符] interface 接口名 [e ...
- 电子面单api接口_JAVA调用示例
电子面单是一种通过热敏纸打印输出纸质×××的物流服务.通过热感应显示文字,打印速度比传统针式打印速度提升4~6倍.电子面单以接口形式嵌入到自己的系统.网站上,可以在自己的平台操作打印电子面单. 目前电 ...
最新文章
- android 自定义命名空间,Android自定义ActionBar实例
- Protobuf3语言指南
- windows 7下用SaveFileDialog弹不出对话框问题
- Zuul鉴权和添加用户服务
- 【Python】RotatingFileHandler:log日志文件自定义大小+滚动输出
- linux 运行eclipse,解决Linux下Eclipse启动错误
- 项目不能上线,是开发的锅,还是产品的错?
- Spark MLlib
- 网站转移服务器,网站转移云服务器
- 清华谭浩强编著的c语言程序设计教程,清华大学谭浩强C语言程序设计教程第3版 (9).doc...
- python与开源gis空间分析_Python 与开源GIS —数据处理、空间分析与地图制图
- 突然发现自己的虚拟VMNET8 没有了,不要慌
- Windows和Linux(Ubuntu)查看电脑主机型号和序列号的命令
- Windows下编译LibreCAD
- 互联网广告的基本概念
- VS2005 常见问题分析
- amp;#9733;平衡法则在生活中的应用
- 【虹科案例】极高的精度水平——虹科数字化仪在大型强子对撞机机器保护系统中的应用
- linux怎么打开rpm文件怎么打开,RPM格式文件怎么打开
- 键盘研究:按键,键码,键符号