1、JAVA与SAP数据交互的方式总结 日期:2009-12-09 1、RFC方式 Java程序直接通过RFC访问SAP的对象(或称函数,可能叫法不对)SAP提供了BAPI(Business Application Programming Interface),BAPI是SAP系统对外提供的一系列接口,主要是使第三方程序通过这些接口来使用SAP,从而方便客户定制某些程序.VB,Java,C,C++等都可以通过BAPI来访问SAP.BAPI是通过R/3系统上的RFC(Remote function call)功能来实现的.因为BAPI的强大功
2、能作为基础,SAP就完全可以选择Java在CRM上加强各种功能,比如可以用Java快速开发一个实现特定功能的客户端。针对Java,SAP也提供了一个API叫Java Connector(JCo),可以使用它方便的调用BAPI提供的接口。 举例说明 :在我们的某一个项目中,需要每个月从R3取出供应商的寄售和非寄售汇总结算数据和明细数据,展现在供应商信息平台上,供供应商开发票和财务部付款进行结算,那么可以做一个定时器,通过RFC在每个月的一号0点将结算数据取出。 private final static String FUNCTION_GET_NONVMI_DATA = 'ZPUR
3、DATA_GET_TMP1'; /** * 处理接口参数 */ private JCO.ParameterList getNonVmiTableParameterList( JCO.Client aConnection, String bukr, String month, String start, String end, String lifnr) { IRepository aRepository = new JCO.Repository('SAPRep', aConnection); IFunctionTemplate function
4、Template = aRepository .getFunctionTemplate(FUNCTION_GET_NONVMI_DATA); logger.debug('FunctionTemplate=' + functionTemplate); JCO.Function function = new JCO.Function(functionTemplate); JCO.ParameterList input = function.getImportParameterList(); input.getField('BUKRS').setValue(bukr
5、); //input.getField('SPMON').setValue(month); input.getField('LIFNR').setValue(lifnr); input.getField('ZBUDATB').setValue(start); input.getField('ZBUDATN').setValue(end); logger.debug('ImportParameterList=' + input); aConnection.execute(function); JCO.ParameterList tableParams =
6、 function.getTableParameterList(); return tableParams; } 2、Idoc方式 适用于中间文件的IDoc定义格式,是SAP企业系统软件的应用程序之间或SAP应用程序与外部程序之间电子数据交换用的标准数据格式。IDoc是SAP的应用程序连接系统的数据转换工具。IDoc用于数据异步处理:每个IDoc生成独立的文本文件,无需连接中央数据库,就可以传送给要求数据的工作平台。SAP的另一个系统业务应用程序接口则用于数据同步处理。一个大公司的网络操作环境很可能需要各地分公司的电脑都能与公司的主数据库连接。这些电脑很可能是用不同的硬件
7、或操作系统平台。因为IDoc对数据进行了压缩,所以它无需变换格式就能在不同的操作系统上使用。 IDoc类型指定不同种类的数据,比如说购买订单或发票,它们可能被划分为更细小的数据种类,即信息类型。更详细的分类意味着一个IDoc类型只能储存某一特定交易所需的数据,这样既提高了工作效率又降低了资源损耗。在事务处理过程中,IDoc随时会生成。例如,在运货交易过程中,可能会产生打印货运清单所需数据的IDoc。客户在SAP系统执行完一项交易后,在数据传送过程中和经过ALE通讯层时,一个或多个IDoc会生成。通讯层执行远程功能调用,使用由客户模式规定得端口定义和RFC介面定义。IDoc的接收
8、者可能为R/3、R/2或一些外部系统。 在采用IDOC方式的时候,可以采用IDOC落地或不落地的方式,一般只有在跟踪测试或做传输记录的时候的采用IDOC落地的方式,一般其他情况基本上都是采用不落地的方式。 public class JcoIdocServer extends JCoIDoc.Server { private final Log logger = LogFactory.getLog(JcoIdocServer.class); public JcoIdocServer(java.util.Properties properties, IRepo
9、sitory jcoRepository, IDoc.Repository idocRepository) { super(properties, jcoRepository, idocRepository); }// constructor MyIDocServer /** * Overridden method of JCoIDoc.Server. Function requests that do not * contain IDocs will be handled here. These requests will be stored as
10、 * XML file in the incoming path. No other action will be done. The * return values won't be filled and no exception will be thrown to the * caller. */ protected void handleRequest(JCO.Function function) { logger.error('error:incoming function request '' + function.getName()
11、 + '',but this should be not happen.'); }// method handleRequest /** * Overridden method of JCoIDoc.Server. Function requests that contain * IDocs will be handled here. All IDocs will be stored as XML files in * the incoming path. Additionally, IDocs that are part of an ORDERS
12、 message will be processed specifically. Some relevant information is * extracted from these IDocs and will be stored in a text file in the * incoming path. */ protected void handleRequest(IDoc.DocumentList documentList) { logger.debug('Incoming IDoc list request containing '
13、 + documentList.getNumDocuments() + ' documents...'); IDoc.DocumentIterator iterator = documentList.iterator(); IDoc.Document doc = null; while (iterator.hasNext()) { doc = iterator.nextDocument(); logger.debug('Processing document no. ' + doc.getIDocNumber() + '...');
14、 JcoIdocAudit jcoIdocAudit = new JcoIdocAudit(); jcoIdocAudit.setTabnam(doc.getTableStructureName()); jcoIdocAudit.setMandt(doc.getClient()); jcoIdocAudit.setDocnum(doc.getIDocNumber()); jcoIdocAudit.setDocrel(doc.getIDocSAPRelease()); jcoIdocAudit.setStatus(doc.getStat
15、us()); jcoIdocAudit.setDirect(doc.getDirection()); jcoIdocAudit.setOutmod(doc.getOutputMode()); jcoIdocAudit.setExprss(doc.getExpressFlag()); jcoIdocAudit.setTest(doc.getTestFlag()); jcoIdocAudit.setIdoctyp(doc.getIDocType()); jcoIdocAudit.setCimtyp(doc.getIDocTypeExten
16、sion()); jcoIdocAudit.setMestyp(doc.getMessageType()); jcoIdocAudit.setMescod(doc.getMessageCode()); jcoIdocAudit.setMesfct(doc.getMessageFunction()); jcoIdocAudit.setStd(doc.getEDIStandardFlag()); jcoIdocAudit.setStdvrs(doc.getEDIStandardVersion()); jcoIdocAudit.setStd
17、mes(doc.getEDIMessageType()); jcoIdocAudit.setSndpor(doc.getSenderPort()); jcoIdocAudit.setSndprt(doc.getSenderPartnerType()); jcoIdocAudit.setSndpfc(doc.getSenderPartnerFunction()); jcoIdocAudit.setSndprn(doc.getSenderPartnerNumber()); jcoIdocAudit.setSndsad(doc.getSenderAd
18、dress()); jcoIdocAudit.setSndlad(doc.getSenderLogicalAddress()); jcoIdocAudit.setRcvpor(doc.getRecipientPort()); jcoIdocAudit.setRcvprt(doc.getRecipientPartnerType()); jcoIdocAudit.setRcvpfc(doc.getRecipientPartnerFunction()); jcoIdocAudit.setRcvprn(doc.getRecipientPartnerNu
19、mber()); jcoIdocAudit.setRcvsad(doc.getRecipientAddress()); jcoIdocAudit.setRcvlad(doc.getRecipientLogicalAddress()); jcoIdocAudit.setCredat(doc.getCreationDateAsString()); jcoIdocAudit.setCretim(doc.getCreationTimeAsString()); jcoIdocAudit.setRefint(doc.getEDITransmissionFi
20、le()); jcoIdocAudit.setRefgrp(doc.getEDIMessageGroup()); jcoIdocAudit.setRefmes(doc.getEDIMessage()); jcoIdocAudit.setArckey(doc.getArchiveKey()); jcoIdocAudit.setSerial(doc.getSerialization()); jcoIdocAuditManager.create(jcoIdocAudit); String[] result = new String[] {
21、 JcoIdocAudit.OPERATION_FAILURE, '未知错误' }; String msgType = doc.getMessageType(); logger.debug('Processing message of type '' + msgType + ''...'); if ('MATMAS'.equals(msgType)) { logger.debug('idocManager.processMATMAS(doc),msgType=' + msgType); logger.debug(
22、'idocManager=' + idocManager); result = idocManager.processMATMAS(doc); } else { logger.debug('invalid msgType:' + msgType); return; } jcoIdocAudit.setOpcod(result[0]); jcoIdocAudit.setOpmsg(result[1]); jcoIdocAuditManager.update(jcoIdocAudit); } }//
23、 method handleRequest /** * The following 4 methods for transaction management are not * implemented here in this sample program. For a description on how to * implement them in real production scenarios please see the JCo API * specification for the JCO.Server class. */ pr
24、otected boolean onCheckTID(String tid) { return true; }// method onCheckTID protected void onConfirmTID(String tid) { }// method onConfirmTID protected void onCommit(String tid) { }// method onCommit protected void onRollback(String tid) { }// method onRollback @Override
25、 public String toString() { return 'gwhost=' + this.getGWHost() + ';gwserv=' + this.getGWServ() + ';progid=' + this.getProgID(); } } 3、XI方式 XI 的集成又可以分为两个层次,第一个层次称为Integration Broker,以消息的方式进行的数据的交换;另外一个层次是 BPM,即业务流程管理。在 XI 中,数据(可以使一条记录,比如单个的物料主数据;也可以是多条记录,比如含主单明细的供应商主数据)从某一个业务系
26、统通过 XI 发送到另外一个业务系统,站在业务系统的角度,前者称为 Sender,后者称为 Receiver 。 适配器(Adapter)进行 XI-XML 格式与业务系统的特定类型的数据格式之间的转换。依据业务系统的数据格式的不同,适配器可以分为多种类型,比如 SOAP Adapter, JDBC Adapter, File Adapter, RFC Adapter 和 IDoc Adapter 等。通常说的实现方式,譬如 SOAP+RFC,是指Sender 与 XI 之间使用 SOAP Adapter,XI 与 Receiver 之间使用 RFC Adapter。






