Example: IDoc Server for tRFC

Definition

            package com.sap.mw.jco.jra.idoc.tests;

import com.sap.conn.idoc.IDocDocumentList;
import com.sap.conn.idoc.IDocXMLProcessor;
import com.sap.mw.jco.jra.IDocMessageListener;
import com.sap.mw.jco.jra.idoc.JRAIDoc;
import java.io.*;
import java.util.Properties;
import javax.ejb.*;

public class IdocMdbBean
    implements MessageDrivenBean, IDocMessageListener 
    {

        <…constructor and other Bean methods…>

       public void onMessage(IDocDocumentList idocList, String transactionalID, Properties info)
        {
            FileOutputStream fos;
            OutputStreamWriter osw;
            String xmlString = null;
            fos = null;
            osw = null;
            try
            {
                //creating the file object( <current TID>_reqeuest.xml )
                String iDocFile = 
                     new StringBuilder(String.valueOf(transactionalID)).append("_request.xml").toString();
                iDocFile = "D:\\usr\\sap\\data\\" + iDocFile;

                //IDocXMLProcessor for parsing/rendering IDoc data...
                IDocXMLProcessor xmlProcessor = JRAIDoc.getIDocFactory().getIDocXMLProcessor();
                try{
                        //...for example to write the data to XML file
                        fos = new FileOutputStream(iDocFile);
                }catch (java.io.FileNotFoundException fnfEx){System.out.println(fnfEx.getStackTrace());}
                osw = new OutputStreamWriter(fos, "UTF8");
                xmlProcessor.render(idocList, osw, 7);
                osw.flush();

                //... or just as XML string for further processing
                xmlString = xmlProcessor.render(idocList);
            }
            catch(Throwable thr)
            {
                thr.printStackTrace();
                try
                {
                    if(osw != null)
                        osw.close();
                    if(fos != null)
                        fos.close();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }


         

The following code shows an example of ejb-jar.xml Especially in the ejb-jar.xml appropriate configuration is required to register the bean properly to the resource adapters.

               <?xml version="1.0" encoding="UTF-8"?><ejb-jar id="ejb-jar_ID"  version="2.1"  xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
        <display-name>IdocMdbBean</display-name>
        <enterprise-beans>
                <message-driven>
                        <icon />
                        <ejb-name>IdocMdb</ejb-name>
                        <ejb-class>
com.sap.mw.jco.jra.idoc.tests.IdocMdbBean
</ejb-class>
                        <messaging-type>
com.sap.mw.jco.jra.IDocMessageListener
</messaging-type>
                        <transaction-type>Bean</transaction-type>
                        <activation-config>
                                <activation-config-property>
                                        <activation-config-property-name>
                                                  FunctionName
                                         </activation-config-property-name>
                                        <activation-config-property-value>
                                                IDOC_INBOUND_ASYNCHRONOUS
                                                </activation-config-property-value>
                                </activation-config-property>
                                <activation-config-property>
                                        <activation-config-property-name>
                                                  BindingKey
                                         </activation-config-property-name>
                                         <activation-config-property-value>
                                                  XMIIREF
                                          </activation-config-property-value>
                                </activation-config-property>
                        </activation-config>
                </message-driven>
        </enterprise-beans>
</ejb-jar>