原文地址
http://help.sap.com/saphelp_nw04/helpdata/en/8b/c88442a07b0e53e10000000a155106/content.htm
应用场景
通过学习下面的教程,你将可以编写一个Java程序以建立一个到SAP网关的服务器连接。
开发步骤
· 编写一个类,并令其继承JCO.Server 类
· 实现一个构造方法,并传入其父类(JCO.Server)构造所需的参数,例如Gateway Host, Gateway Service, Program ID 以及Repository。
· 重写其父类的protected方法handleRequest,并在其中实现收到请求后需要执行的操作。
· 创建多个JCO.Server的实例,并通过start()来启动它们。
示例代码
public class MyFirstServer extends JCO.Server {
/**
* Create an instance of my own server
* @param gwhost (gateway host)
* @param gwserv (gateway service number)
* @param progid (program id)
* @param repository (repository used by the server to lookup the
definitions of an inc)
*/
public MyFirstServer(String gwhost, String gwserv,
String progid, IRepository repository) {
super(gwhost,gwserv,progid,repository);
}
/**
* Overrides the default method.
*/
protected void handleRequest(JCO.Function function) {
JCO.ParameterList input = function.getImportParameterList();
JCO.ParameterList output = function.getExportParameterList();
JCO.ParameterList tables = function.getTableParameterList();
System.out.println("handleRequest(" + function.getName() + ")");
if (function.getName().equals("STFC_CONNECTION")) {
System.out.println(">>> request STFC_CONNECTION: " + input.getString("REQUTEXT"));
output.setValue(input.getString("REQUTEXT"),"ECHOTEXT");
output.setValue("This is a response from MyFirstServer","RESPTEXT");
}
}
}
public class FirstExample {
static MyFirstServer serverConnections[] = new MyFirstServer[3];
/**
* Start the server
*/
public static void startServers() {
JCO.addClientPool("POOL", 3, "000", "user" ,"password" , "EN",
"abap_system" ,"00");
IRepository repository = JCO.createRepository("REP", "POOL");
for(int i = 0; I < serverConnections.length; i++) {
// Server listens for incoming requests from system 1
// (Change gateway host, service, and program ID according to your
needs)
serverConnections [i] = new MyFirstServer
("gwhost", //gateway host, often the same as host
"sapgw00", //gateway service, generally sapgw+<SYSNR>
"JCOSERVER01", // corresponds to program ID defined in SM59
repository);
serverConnections [i].start();
}
public static void stopServers() {
for(int i = 0; I < serverConnections.length; i++) {
serverConnections [i].stop();
}
public static void main(String[] args) {
startServers() ;
}
}