日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java rfc接口_java调用sap的RFC接口

發布時間:2023/12/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。