您的位置:首页 > 其它

通过SAPjco + sapidoc3 来实现第3方系统和SAP R3系统进行IDOC的双向传送数据

2009-12-18 09:57 1236 查看
1.从SAP market上下载sapidoc3和sapjco3下载两个java包



2.jco服务器端代码如下

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

<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-fareast-language:EN-US;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐