SAP JCo的Server/Client编程实例

JCo是服务于SAP系统和Java系统的RFC中间件,是用Java实现的API,以Jar包的方式发布。应用灵活,但使用起来相对繁琐,NetWeaver Portal中基于Java的Webdynpro开发环境SAPNetWeaver Developer Studio就是通过JCo连接SAP的。通过一个简单实例,描述一下实现过程,开发环境:Eclipse + ECC,准备好JCo的Jar包。

一、SAP端:

SE37,创建一个RFC,供Java端调用

SAP JCo的Server/Client编程实例_第1张图片

functionzsap_calculate.

*"----------------------------------------------------------------------

*"*"Localinterface:

*"IMPORTING

*"VALUE(NUMBER1)TYPESTRING

*"VALUE(NUMBER2)TYPESTRING

*"VALUE(OPERATOR)TYPESTRING

*"EXPORTING

*"VALUE(RESULT)TYPESTRING

*"----------------------------------------------------------------------

try.

caseoperator.

when'+'.

result=number1+number2.

when'-'.

result=number1-number2.

when'*'.

result=number1*number2.

when'/'.

result=number1/number2.

whenothers.

result='Notsupported!'.

endcase.

catchcx_root.

result='Notsupported!'.

endtry.

condenseresult.

endfunction.


SM59配置RFC destination

SAP JCo的Server/Client编程实例_第2张图片

注意这里要设置为Unicode的communication type

SAP JCo的Server/Client编程实例_第3张图片

SE37定义Java系统调用的接口函数:

SAP JCo的Server/Client编程实例_第4张图片

SE38创建测试程序:

reportzyincl_test_00nostandardpageheading.

data:

l_number1typestring,

l_number2typestring,

l_operatortypestring,

l_resulttypestring.

l_number1='15'.

l_number2='221'.

l_operator='+'.

callfunction'ZJAVA_CALCULATE'destination'JCOPRO001'

exporting

number1=l_number1

number2=l_number2

operator=l_operator

importing

result=l_result.

writel_result.

二、Java端:

在Eclipse中创建一个Java Project

SAP JCo的Server/Client编程实例_第5张图片

把JCo的Jar包配置到项目

SAP JCo的Server/Client编程实例_第6张图片

创建服务类,处理SAP的请求,记得设置属性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对Java系统的远程访问
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")) {


//调用SAP系统的RFC:ZSAP_CALCULATE实现计算
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");
}


}

创建启动服务的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);

//启动服务

newService("192.168.1.8", "sapgw00", "JCOPRO001",rep).start();

System.out.println("Serviceis started.");

}

}

运行JCoClient,启动服务:


回到SAP端,运行测试程序,成功返回结果:

SAP JCo的Server/Client编程实例_第7张图片

修改参数,再测试几次,在Java端可以监控到请求的日志:

SAP JCo的Server/Client编程实例_第8张图片

你可能感兴趣的:(server)