SAP JCo的Server/Client编程实例
JCo是服務(wù)于SAP系統(tǒng)和Java系統(tǒng)的RFC中間件,是用Java實(shí)現(xiàn)的API,以Jar包的方式發(fā)布。應(yīng)用靈活,但使用起來相對(duì)繁瑣,NetWeaver Portal中基于Java的Webdynpro開發(fā)環(huán)境SAPNetWeaver Developer Studio就是通過JCo連接SAP的。通過一個(gè)簡(jiǎn)單實(shí)例,描述一下實(shí)現(xiàn)過程,開發(fā)環(huán)境:Eclipse + ECC,準(zhǔn)備好JCo的Jar包。
?
一、SAP端:
?
SE37,創(chuàng)建一個(gè)RFC,供Java端調(diào)用
function?zsap_calculate.
*"----------------------------------------------------------------------
*"*"Local?interface:
*"??IMPORTING
*"?????VALUE(NUMBER1)?TYPE??STRING
*"?????VALUE(NUMBER2)?TYPE??STRING
*"?????VALUE(OPERATOR)?TYPE??STRING
*"??EXPORTING
*"?????VALUE(RESULT)?TYPE??STRING
*"----------------------------------------------------------------------
??try?.
??????case?operator.
????????when?'+'.
??????????result?=?number1?+?number2.
?
????????when?'-'.
??????????result?=?number1?-?number2.
?
????????when?'*'.
??????????result?=?number1?*?number2.
?
????????when?'/'.
??????????result?=?number1?/?number2.
?
????????when?others.
??????????result?=?'Not?supported!'.
??????endcase.
?
????catch?cx_root.
??????result?=?'Not?supported!'.
??endtry.
?
??condense?result.
?
endfunction.
SM59配置RFC destination
?
注意這里要設(shè)置為Unicode的communication type
?
SE37定義Java系統(tǒng)調(diào)用的接口函數(shù):
?
SE38創(chuàng)建測(cè)試程序:
report??zyincl_test_00?no?standard?page?heading.
?
data:
????l_number1?type?string,
????l_number2?type?string,
????l_operator?type?string,
????l_result?type?string.
?
l_number1?=?'15'.
l_number2?=?'221'.
l_operator?=?'+'.
?
call?function?'ZJAVA_CALCULATE'?destination?'JCOPRO001'
??exporting
????number1??=?l_number1
????number2??=?l_number2
????operator?=?l_operator
??importing
????result???=?l_result.
?
write?l_result.
?
二、Java端:
?
在Eclipse中創(chuàng)建一個(gè)Java Project
?
把JCo的Jar包配置到項(xiàng)目
?
創(chuàng)建服務(wù)類,處理SAP的請(qǐng)求,記得設(shè)置屬性Unicode為1:
package org.clyde;
import com.sap.mw.jco.*;
import com.sap.mw.jco.JCO.Client;
import com.sap.mw.jco.JCO.Function;
import com.sap.mw.jco.JCO.ParameterList;
import com.sap.mw.jco.JCO.Repository;
import com.sap.mw.jco.JCO.Server;
public class Service extends Server {
private static Client client;
public Service(String gwhost, String gwserv, String progid, IRepository rep) {
super(gwhost, gwserv, progid, rep);
super.setProperty("unicode", "1");
if (client == null) {
client = JCO.createClient("550", "clyde", "1q2w3e4r", "EN",
"192.168.1.8", "00");
client.connect();
}
}
@Override
//重載方法,處理SAP對(duì)Java系統(tǒng)的遠(yuǎn)程訪問
protected void handleRequest(Function function) throws Exception {
ParameterList input = function.getImportParameterList();
ParameterList output = function.getExportParameterList();
String number1, number2, operator, result;
number1 = input.getString("NUMBER1");
number2 = input.getString("NUMBER2");
operator = input.getString("OPERATOR");
if (function.getName().equals("ZJAVA_CALCULATE")) {
//調(diào)用SAP系統(tǒng)的RFC:ZSAP_CALCULATE實(shí)現(xiàn)計(jì)算
result = getResult(number1, number2, operator);
System.out.println("Calculating=> ?" + number1 + operator + number2
+ " = " + result);
output.setValue(result, "RESULT");
}
}
public String getResult(String number1, String number2, String operator) {
Repository rep = new Repository("", client);
Function func = rep.getFunctionTemplate("ZSAP_CALCULATE").getFunction();
ParameterList input = func.getImportParameterList();
input.setValue(number1, "NUMBER1");
input.setValue(number2, "NUMBER2");
input.setValue(operator, "OPERATOR");
func.setImportParameterList(input);
client.execute(func);
return func.getExportParameterList().getString("RESULT");
}
}
?
創(chuàng)建啟動(dòng)服務(wù)的Client類:
package org.clyde;
?
importcom.sap.mw.jco.JCO;
importcom.sap.mw.jco.JCO.Client;
importcom.sap.mw.jco.JCO.Repository;
?
public classJCoClient {
publicstatic void main(String[] args) {
Clientclient = JCO.createClient("550", "clyde","1q2w3e4r", "EN",
"192.168.1.8","00");
client.connect();
Repositoryrep = new JCO.Repository("", client);
//啟動(dòng)服務(wù)
newService("192.168.1.8", "sapgw00", "JCOPRO001",rep).start();
System.out.println("Serviceis started.");
}
?
}
?
運(yùn)行JCoClient,啟動(dòng)服務(wù):
?
回到SAP端,運(yùn)行測(cè)試程序,成功返回結(jié)果:
?
修改參數(shù),再測(cè)試幾次,在Java端可以監(jiān)控到請(qǐng)求的日志:
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!
總結(jié)
以上是生活随笔為你收集整理的SAP JCo的Server/Client编程实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C3P0 配置
- 下一篇: 自动化集成:Pipeline整合Dock