JAVA 调用 SAP RFC接口的方式实例

java要调用  SAP RFC接口时,需要用到sapjco3.jar 架包;

windows下还需要将文件sapjco3.dll文件放到system32的目录下;

linux下同样需要把sapjco3.so放入项目的执行目录下;

代码如下;

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<User> getUser() {  
  
        JCoFunction function = RfcManager.getFunction("FUNCION_USER");  
  
        RfcManager.execute(function);  
        JCoParameterList outputParam = function.getTableParameterList();  
        JCoTable bt = outputParam.getTable("TABLEOUT");  
        List<User> list = new ArrayList<User>();  
        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:
[java] view plain copy
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;  
    }  
}  
[java] view plain copy
package jco;  
  
import com.sap.conn.jco.ext.*;  
import java.util.HashMap;  
import java.util.Properties;  
  
public class JCOProvider implements DestinationDataProvider,SessionReferenceProvider {  
  
    private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();  
    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;  
    }  
}  

[java] view plain copy
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;  
    }  
  
}  

[java] view plain copy
package jco;  
  
public interface IMultiStepJob {  
    public boolean runNextStep();  
  
    String getName();  
  
    public void cleanUp();  
}  

[java] view plain copy
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<IMultiStepJob, RfcSessionReference> sessions = new Hashtable<IMultiStepJob, RfcSessionReference>();  
    public static ThreadLocal<RfcSessionReference> localSessionReference = new ThreadLocal<RfcSessionReference>();  
    private BlockingQueue<IMultiStepJob> queue ;  
    private CountDownLatch doneSignal;  
    private boolean isSapBusy = false;  
  
    public JcoMutiThread(CountDownLatch doneSignal, BlockingQueue<IMultiStepJob> 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();  
        }  
    }  
}  

 

 

 

 

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Studio中远程调用SAP RFC函数,可以通过以下步骤实现: 1. 首先,确保你的Android Studio项目中已经添加了相应的SAP Java Connector(SAP JCo)库。你可以在项目的build.gradle文件中添加依赖项,例如: ```java dependencies { implementation 'com.sap.conn.jco:jco3:3.1.2' } ``` 2. 创建一个SAP连接: 在你的代码中,实例化一个`JCoDestination`对象,并设置连接所需的属性,如SAP服务器的地址、用户名、密码等。例如: ```java JCoDestination destination = JCoDestinationManager.getDestination("MY_DESTINATION"); destination.getRepository(); // 设置连接属性 destination.getProperties().setProperty(DestinationDataProvider.JCO_ASHOST, "SAP服务器地址"); destination.getProperties().setProperty(DestinationDataProvider.JCO_SYSNR, "系统编号"); destination.getProperties().setProperty(DestinationDataProvider.JCO_CLIENT, "客户端"); destination.getProperties().setProperty(DestinationDataProvider.JCO_USER, "用户名"); destination.getProperties().setProperty(DestinationDataProvider.JCO_PASSWD, "密码"); ``` 3. 调用RFC函数: 使用SAP连接后,可以从SAP系统的函数库中获取function module,然后通过函数的`execute()`方法调用RFC函数并传递参数。例如: ```java JCoFunction function = destination.getRepository().getFunction("RFC_FUNCTION_NAME"); if (function == null) { throw new RuntimeException("Function not found"); } // 设置RFC函数的输入参数 function.getImportParameterList().setValue("parameterName", parameterValue); function.execute(destination); // 获取RFC函数的输出参数 String result = function.getExportParameterList().getString("outputParamName"); ``` 4. 处理RFC函数的返回结果: 根据你的需求,可以根据RFC函数的返回结果进行一系列的操作,例如显示在界面上、保存到本地等。 以上就是在Android Studio中远程调用SAP RFC函数的基本步骤。请注意,在实际应用中,你可能还需要处理连接的异常、异常情况下的处理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值