Technical Articles
How to create Custom Adapter Module in SAP PI/PO for EJB 3.0 Version
Introduction:
In this blog, we will see step-by-step process to create SAP PI/PO Custom Adapter module, EJB version 3.0. As, I don’t find any blog, where each steps are clearly mentioned, with detail configuration. Hope this will help SAP PI/PO Developers.
Custom Adapter Module Creation Process:-
Module Name–> ReadExcelFile
In NWDS: Go to File –-> New –-> EJB Project
EJB Project–> ExcelToXML_EJB
EAR Project–> ExcelToXML_EAR
EJB module version –> 3.0
Click Next and Next.
EJB Client JAR –> Uncheck
Generate ejb-jar.xml deployment descriptor –> Check
Click on Finish.
Two Projects are Created as below.
Right Click on EJB Project –> New –> Session Bean (EJB 3.x)
Choose EJB project created before and provide Java package & Class Name.
State Type –> Stateless
Check Remote and Local and Finish.
Below Objects are created.
Add External JAR files and Libraries to EJB project.
Right Click on EJB project –> Preference –> JAVA Build Path –> Add Libraries
Select XPI Library –> Click Next
Select XPI Adapter Libraries from drop down –> Finish
Add “com.sap.aii.af.svc_api.jar” file as External Jars for auditlog
Add “jxl-2.6.jar” for reading Excel File (.xls).
Click on Apply –> Ok
Expand META-INF folder of EJB –> Click on ejb-j2ee-engine.xml –> Change the details as below.
Provide EJB and JNDI name.
JNDI name will be the Adapter Module Name in SAP PI/PO Communication Channel.
<?xml version=“1.0” encoding=“UTF-8”?>
<ejb-j2ee-engine xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“ejb-j2ee-engine_3_0.xsd”>
<enterprise-beans>
<enterprise-bean>
<ejb-name>ReadExcelFile</ejb-name>
<jndi-name>ReadExcelFile</jndi-name>
</enterprise-bean>
</enterprise-beans>
</ejb-j2ee-engine>
Expand META-INF folder of EJB Project –> Click on ejb-jar.xml –> Change the details as below
<ejb-class> –> This need to be your java package name and your Class name
<display-name> –> Your display name
<ejb-name> –> Your ejb name
<?xml version=“1.0” encoding=“UTF-8”?>
<ejb-jar xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns=“http://java.sun.com/xml/ns/javaee” xmlns:ejb=“http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd”
xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd” version=“3.0”>
<display-name>ExcelToXML_EJB</display-name>
<enterprise-beans>
<session>
<icon/>
<ejb-name>ReadExcelFile</ejb-name>
<home>com.sap.aii.af.lib.mp.module.ModuleHome</home>
<remote>com.sap.aii.af.lib.mp.module.ModuleRemote</remote>
<local-home>com.sap.aii.af.lib.mp.module.ModuleLocalHome</local-home>
<local>com.sap.aii.af.lib.mp.module.ModuleLocal</local>
<ejb-class>com.sap.java.modules.ReadExcelFile</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Expand META-INF folder of EAR Project –> Click on application-j2ee-engine.xml –> Change the details as below.
<?xml version=“1.0” encoding=“UTF-8”?>
<application-j2ee-engine xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“application-j2ee-engine.xsd”>
<reference reference-type=“hard”>
<reference-target target-type=“service” provider-name=“sap.com”>engine.security.facade</reference-target>
</reference>
<reference reference-type=“hard”>
<reference-target target-type=“library” provider-name=“sap.com”>engine.j2ee14.facade</reference-target>
</reference>
<reference reference-type=“hard”>
<reference-target target-type=“service” provider-name=“sap.com”>com.sap.aii.af.svc.facade</reference-target>
</reference>
<reference reference-type=“hard”>
<reference-target target-type=“interface” provider-name=“sap.com”>com.sap.aii.af.ifc.facade</reference-target>
</reference>
<reference reference-type=“hard”>
<reference-target target-type=“library” provider-name=“sap.com”>com.sap.aii.af.lib.facade</reference-target>
</reference>
<reference reference-type=“hard”>
<reference-target target-type=“library” provider-name=“sap.com”>com.sap.base.technology.facade</reference-target>
</reference>
<fail-over-enable xsi:type=“fail-over-enableType_disable”
mode=“disable” ></fail-over-enable>
</application-j2ee-engine>
Deploy the EAR File:-
Open Window –> Preferences –> SAP AS java –> Add
Right Click on EAR Project –> Run As –> Run on Server
Select Your Server –> Click Next
Select EJB project –> Finish
Then the project will be deployed in your server and you can see in EJB Explorer (SAP PI/PO ).
Use Created Adapter Module In Sender Communication Channel.
Deployment to Higher Environments:-
Right Click EAR Project –> Export –> SAP EAR File
Sample Adapter Module code:-
package com.sap.java.modules;
import javax.ejb.Stateless;
import com.sap.aii.af.lib.mp.module.Module;
import com.sap.aii.af.lib.mp.module.ModuleContext;
import com.sap.aii.af.lib.mp.module.ModuleData;
import com.sap.aii.af.lib.mp.module.ModuleException;
import com.sap.engine.interfaces.messaging.api.XMLPayload;
import com.sap.aii.af.service.auditlog.Audit;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import java.io.ByteArrayInputStream;
import jxl.Cell;
import jxl.Workbook;
/**
* Session Bean implementation class ReadExcelFile
*/
@Stateless
public class ReadExcelFile implements Module {
MessageKey amk = null;
@Override
public ModuleData process(ModuleContext mc, ModuleData imd) throws ModuleException {
String msgType = null;
String nameSpace = null;
Object obj = null;
Message msg = null;
try {
obj = imd.getPrincipalData();
msg = (Message) obj;
amk = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
XMLPayload xp = msg.getDocument();
if (xp != null) {
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,“AO: Now calling Convert Method to convert Excel(.xls) to XML”);
byte by[] = convert(xp.getContent());
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,“AO: Conversion Done Successfully.”);
xp.setContent(by);
}
imd.setPrincipalData(msg);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,“AO: Principle data set successfully.”);
} catch (Exception e){
ModuleException me = new ModuleException(e);
throw me;
}
return imd;
}
public byte[] convert(byte src[]) throws Exception {
String xmldata = “”;
try {
ByteArrayInputStream byteArr= new ByteArrayInputStream(src);
Workbook wb = Workbook.getWorkbook(byteArr);
xmldata =“<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n\n”+ “<Details>”;
Cell[] cells ;
Cell[] cellNames ;
cellNames = wb.getSheet(0).getRow(0);
for(int j=1;j<wb.getSheet(0).getRows();j++) {
xmldata = xmldata+“<Record>”;
cells = wb.getSheet(0).getRow(j);
for(int i=0;i<wb.getSheet(0).getColumns();i++){
xmldata = xmldata+“<“+cellNames[i].getContents()+“>”+cells[i].getContents()+“</”+cellNames[i].getContents()+“>”;
}
xmldata = xmldata + “</Record>”;
}
xmldata = xmldata + “</Details>”;
wb.close();
} catch (Exception e){
ModuleException me = new ModuleException(e);
throw me;
}
return xmldata.getBytes();
}
}
Note:-
1 ) I am using below details for my development.
NWDS–> 7.3 SP06 PAT0009 and jdk1.8.0_131
SAP PO –> 7.5 SP07
EJB Module Version–> 3.0
2) If you will not find XPI Libraries in your NWDS version, then you can add below jar files , as external jars and any other jar files as per your requirements.
3) Any Suggestion on this blog is always welcome.
4) We can create module and write Java code in different way, which you can find in different blogs.
Hi Brahma, Very useful blog on topic that has little doc.
Can please share how would one proceed to read channel parameters and use them in further processing .
BR,
Jay
Hi Jay,
Thanks.
I will create one more, to need parameter from adapter module and use in processing.
I have already done that and use in my project "ReplaceStringBean".
Thanks,
Bijayashree
Hi Bijayashree,
May I know what EAR version is selected in relation to having EJB 3.0? I was told that it has to be related to some apparent reason. Thank you.
Regards,
Joel