PI自定义适配器模块
1.开发EJB项目
1.1Eclipse创建EJB项目
1.2类方法ChgRestFormat.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.sap.pi.geely.custom; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.rmi.RemoteException; 8 import java.security.KeyStore; 9 import java.security.KeyStoreException; 10 import javax.annotation.PostConstruct; 11 import javax.annotation.PreDestroy; 12 import javax.ejb.Local; 13 import javax.ejb.LocalHome; 14 import javax.ejb.Remote; 15 import javax.ejb.RemoteHome; 16 import javax.ejb.Stateless; 17 import javax.naming.InitialContext; 18 import javax.naming.NamingException; 19 import com.sap.aii.af.lib.mp.module.Module; 20 import com.sap.aii.af.lib.mp.module.ModuleContext; 21 import com.sap.aii.af.lib.mp.module.ModuleData; 22 import com.sap.aii.af.lib.mp.module.ModuleException; 23 import com.sap.aii.af.lib.mp.module.ModuleHome; 24 import com.sap.aii.af.lib.mp.module.ModuleLocal; 25 import com.sap.aii.af.lib.mp.module.ModuleLocalHome; 26 import com.sap.aii.af.lib.mp.module.ModuleRemote; 27 import com.sap.engine.interfaces.messaging.api.Message; 28 import com.sap.engine.interfaces.messaging.api.MessageKey; 29 import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory; 30 import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess; 31 import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus; 32 import com.sap.engine.interfaces.messaging.api.exception.InvalidParamException; 33 34 /** 35 * Session Bean implementation class ChgRestFormat 36 */ 37 @Stateless(name = "ChgRestFormatBean") 38 @Local(value = { ModuleLocal.class }) 39 @Remote(value = { ModuleRemote.class }) 40 @LocalHome(value = ModuleLocalHome.class) 41 @RemoteHome(value = ModuleHome.class) 42 public class ChgRestFormat implements Module{ 43 /** 44 * Default constructor. 45 */ 46 @Override 47 public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData) throws ModuleException { 48 // TODO Auto-generated method stub 49 AuditAccess audit = null; 50 Message msg = null; 51 MessageKey msgKey = null; 52 byte[] content = null; 53 String contstr = null; 54 String fldNode = null; 55 String msgType = null; 56 String dataFormat = null; 57 String nameSpace = null; 58 String fullpload = null; 59 60 try { 61 62 //Load message: 63 msg = (Message) inputModuleData.getPrincipalData(); 64 msgKey = new MessageKey(msg.getMessageId(),msg.getMessageDirection()); //msg.getMessageKey(); 65 66 audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess(); 67 audit.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, 68 "Edit by rico | Custom-Java-Adapter-Module (localejbs/ChgRestFormat)"); 69 70 //Loads the contents of the incoming message: 71 content = msg.getDocument().getContent(); 72 contstr = new String(msg.getDocument().getContent()); 73 audit.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, 74 "Edit by rico | ChgRestFormat | Document-Content:" + contstr); 75 76 fullpload = new String(msg.getMainPayload().getContent()); 77 audit.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, 78 "Edit by rico | ChgRestFormat | Payload-Content:" + fullpload); 79 80 //Load configuration parameters: 81 fldNode = (String) moduleContext.getContextData("fldNode"); 82 audit.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, 83 "Edit by rico | ChgRestFormat | input-fldNode:" + fldNode); 84 85 msgType = (String) moduleContext.getContextData("msgType"); 86 audit.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, 87 "Edit by rico | ChgRestFormat | input-msgType:" + msgType); 88 89 nameSpace = (String) moduleContext.getContextData("nameSpace"); 90 audit.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, 91 "Edit by rico | ChgRestFormat | input-nameSpace:" + nameSpace); 92 93 dataFormat = (String) moduleContext.getContextData("dataFormat"); 94 audit.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, 95 "Edit by rico | ChgRestFormat | input-nameSpace:" + dataFormat); 96 97 //convert content to specify format(JSON or XML) 98 if(dataFormat.equals("XML")==true) { 99 contstr = "<"+fldNode+">"+contstr+"</"+fldNode+">"; 100 101 if((msgType != null && !msgType.isEmpty()) && (nameSpace != null && !nameSpace.isEmpty())) { 102 String stacd = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+"<ns0:"+msgType+" xmlns:ns0=\""+nameSpace+"\">"; 103 String endcd = "</ns0:"+msgType+">"; 104 contstr = stacd + contstr + endcd; 105 } 106 }else if(dataFormat.equals("JSON")==true) { 107 contstr = "{\""+fldNode+"\":\""+contstr+"\"}"; 108 109 if((msgType != null && !msgType.isEmpty()) && (nameSpace != null && !nameSpace.isEmpty())) { 110 String stajs = "\""+msgType+"\":"+contstr; 111 contstr = "{"+stajs+"}"; 112 } 113 } 114 115 audit.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, 116 "Edit by rico | ChgRestFormat | Trans-Content:" + contstr); 117 118 //Transform string to array 119 byte convert[] = contstr.getBytes("UTF-8"); 120 121 //ByteArrayInputStream byteArr= new ByteArrayInputStream(content); 122 123 //Update output: 124 try { 125 msg.getDocument().setContent(convert); 126 } catch (InvalidParamException e) { 127 audit.addAuditLogEntry(msgKey, AuditLogStatus.ERROR, e.getMessage()); 128 throw new ModuleException(e.getMessage(), e); 129 } 130 131 inputModuleData.setPrincipalData(msg); 132 133 } catch (Exception e) { 134 ModuleException me = new ModuleException(e); 135 throw me; 136 } 137 138 return inputModuleData; 139 } 140 141 }
1.3配置META-INF: ejb-j2ee-engine.xml
1.4配置EAR:application-j2ee-engine.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <application-j2ee-engine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="application-j2ee-engine.xsd"> 3 <reference reference-type="hard"> 4 <reference-target target-type="service" provider-name="sap.com">engine.security.facade</reference-target> 5 </reference> 6 <reference reference-type="hard"> 7 <reference-target target-type="library" provider-name="sap.com">engine.j2ee14.facade</reference-target> 8 </reference> 9 <reference reference-type="hard"> 10 <reference-target target-type="service" provider-name="sap.com">com.sap.aii.af.svc.facade</reference-target> 11 </reference> 12 <reference reference-type="hard"> 13 <reference-target target-type="interface" provider-name="sap.com">com.sap.aii.af.ifc.facade</reference-target> 14 </reference> 15 <reference reference-type="hard"> 16 <reference-target target-type="library" provider-name="sap.com">com.sap.aii.af.lib.facade</reference-target> 17 </reference> 18 <reference reference-type="hard"> 19 <reference-target target-type="library" provider-name="sap.com">com.sap.base.technology.facade</reference-target> 20 </reference> 21 <fail-over-enable xsi:type="fail-over-enableType_disable" 22 mode="disable" /></application-j2ee-engine>
1.5发布
1.5.1可手工导出导入:
1.5.2通过SERVER发布
PI服务器实例编码
确认后输入登录信息即可发布到PI。
发布成功后进入PI首页查看EJB Explorer
2.PI配置
2.1ESR-Message Mapping
2.2Communication Channel
3.测试
3.1Postman发送
3.2PI消息