1.从SAP market上下载sapidoc3和sapjco3下载两个java包
import com.sap.conn.idoc.*;
import java.io.*;
import com.sap.conn.jco.server.*;
import com.sap.conn.idoc.jco.*;
public class IDocServerExample
{
public static void main(String[] a)
{
try
{
// see provided examples of configuration files MYSERVER.jcoServer and BCE.jcoDestination
JCoIDocServer server = JCoIDoc.getServer("MYSERVER");
server.setIDocHandlerFactory(new MyIDocHandlerFactory());
server.setTIDHandler(new MyTidHandler());
MyThrowableListener listener = new MyThrowableListener();
server.addServerErrorListener(listener);
server.addServerExceptionListener(listener);
server.setConnectionCount(1);
server.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
static class MyIDocHandler implements JCoIDocHandler
{
public void handleRequest(JCoServerContext serverCtx, IDocDocumentList idocList)
{
该方法用来侦听SAP的IDOC 端口,如果有idoc发送到该端口,该方法就会把该idoc生成xml文件
FileOutputStream fos=null;
OutputStreamWriter osw=null;
try
{
IDocXMLProcessor xmlProcessor =
JCoIDoc.getIDocFactory().getIDocXMLProcessor();
fos=new FileOutputStream(serverCtx.getTID()+"_idoc.xml");
osw=new OutputStreamWriter(fos, "UTF8");
xmlProcessor.render(idocList, osw,
IDocXMLProcessor.RENDER_WITH_TABS_AND_CRLF);
osw.flush();
}
catch (Throwable thr)
{
thr.printStackTrace();
}
finally
{
try
{
if (osw!=null)
osw.close();
if (fos!=null)
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
static class MyIDocHandlerFactory implements JCoIDocHandlerFactory
{
private JCoIDocHandler handler = new MyIDocHandler();
public JCoIDocHandler getIDocHandler(JCoIDocServerContext serverCtx)
{
return handler;
}
}
static class MyThrowableListener implements JCoServerErrorListener, JCoServerExceptionListener
{
public void serverErrorOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Error error)
{
System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
error.printStackTrace();
}
public void serverExceptionOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Exception error)
{
System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
error.printStackTrace();
}
}
static class MyTidHandler implements JCoServerTIDHandler
{
public boolean checkTID(JCoServerContext serverCtx, String tid)
{
System.out.println("checkTID called for TID="+tid);
return true;
}
public void confirmTID(JCoServerContext serverCtx, String tid)
{
System.out.println("confirmTID called for TID="+tid);
}
public void commit(JCoServerContext serverCtx, String tid)
{
System.out.println("commit called for TID="+tid);
}
public void rollback(JCoServerContext serverCtx, String tid)
{
System.out.print("rollback called for TID="+tid);
}
}
}
以上程序需要server 配置文件和Destination的配置文件如下
server 配置文件格式:
#用来测试!
#2009-06-01
jco.server.connection_count=2
jco.server.gwhost=XXXXXXXX "SAP 服务器 IP
jco.server.progid=JCO_SERVER "这个可以取个有意义的名字
jco.server.gwserv=sapgw00 "sapgw + SAP 服务器系统编号
jco.server.repository_destination=ABAP_AS_WITH_POOL "Destination的属性的文件名字
Destination的配置文件格式:
jco.client.lang=en
jco.destination.peak_limit=10
jco.client.client=200
jco.client.passwd=XXXXX "密码
jco.client.user=XXXXXXX "用户名字
jco.client.sysnr=00 "SAP系统编号
jco.destination.pool_capacity=3
jco.client.ashost=XXXXXX "SAP 服务器 IP
3.从jco server发送idoc给SAP
import com.sap.conn.jco.*;
import com.sap.conn.idoc.jco.*;
import com.sap.conn.idoc.*;
import java.io.*;
public class IDocClientExample {
public static void main(String[] args) {
try
{
String iDocXML = null;
FileReader fileReader;
try
{
fileReader = new FileReader("MyIDocDocumentAsXML.xml");
BufferedReader br = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null)
{
sb.append(line);
}
iDocXML = sb.toString();
br.close();
fileReader.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
// see provided configuration file BCE.jcoDestination
JCoDestination destination=JCoDestinationManager.getDestination("ZATF91");
IDocRepository iDocRepository = JCoIDoc.getIDocRepository(destination);
String tid = destination.createTID();
IDocFactory iDocFactory = JCoIDoc.getIDocFactory();
// a) create new idoc
//IDocDocument doc = iDocFactory.createIDocDocument(iDocRepository, "MATMAS02");
//IDocSegment segment = doc.getRootSegment();
//segment = segment.addChild("E1MARAM");
// and so on. See Idoc Specification .....
//JCoIDoc.send(doc, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
// b) use existent xml file
IDocXMLProcessor processor=iDocFactory.getIDocXMLProcessor();
IDocDocumentList iDocList=processor.parse(iDocRepository, iDocXML);
JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
destination.confirmTID(tid);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.print("program end");
}
}
4.在SM59里创建RFC Des之前先把Jco server的代码运行起来,能建立jco server和SAP的连接
图中的Program ID 必须和jco server 属性文件里的jco.server.progid所指定的名字一致。在sap里定义IDOC的port都必须用该DES,这样jco server才能接受idoc
用SMGW来查看jco server是否登陆SAP