Quantcast
Channel: SCN : Blog List - Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 676

Adapter Module: ExceptionCatcherBean

$
0
0

Adapter Module: ExceptionCatcherBean

 

Use

 

 

 

When a database error occurs in a synchronous proxy-to-jdbc scenario, the error information is not transferred back to the sender system. And unless the end user has access to SAP PI monitors, she/he only receives a PARSING GENERAL exception without any other information.

 

 

error_1.png

 

You use ExceptionCatcherBean module to wrap CallSAPAdapter module execution to catch any module exception to generate a new ModuleException object with error information.

 

 

error_2.png

 

 

All the information is then transferred back to the sender system.

 

 

sxmb_moni_error.png

 

 

Deployment

 

Enterprise Java Bean Project: ExceptionCatcher-ejb

Enterprise Java Bean Application: ExceptionCatcher-ear

 

Integration

 

The module can be used in any Sender Adapter.

 

Activities

 

This section describes all the activities that have to be carried out in order to configure the module.

 

Entries in processing sequence

 

Remove CallSAPAdapter module and insert ExceptionCatcherBean as shown in the picture below.

 

config.PNG

 

Entries in the module configuration

 

The adapter module doesn’t expect any parameter.

 

Audit Log

 

The execution process can be followed in the audit log generated per message.

 

 

Code


import java.util.Hashtable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.CreateException;
import javax.ejb.Local;
import javax.ejb.LocalHome;
import javax.ejb.Remote;
import javax.ejb.RemoteHome;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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.aii.af.lib.mp.module.ModuleHome;
import com.sap.aii.af.lib.mp.module.ModuleLocal;
import com.sap.aii.af.lib.mp.module.ModuleLocalHome;
import com.sap.aii.af.lib.mp.module.ModuleRemote;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.MessageDirection;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import com.sap.engine.interfaces.messaging.api.exception.MessagingException;
@Stateless(name = "ExceptionCatcherBean")
@Local(value = { ModuleLocal.class })
@Remote(value = { ModuleRemote.class })
@LocalHome(value = ModuleLocalHome.class)
@RemoteHome(value = ModuleHome.class)
@TransactionManagement(value=TransactionManagementType.BEAN)
public class ExceptionCatcherBean implements Module {
private AuditAccess audit; 
private MessageKey key;
@PostConstruct
public void initialiseResources() {
try {
audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
} catch (Exception e) {
throw new RuntimeException("Error in initializeResources: " + e.getMessage()); }
}
@Override
public ModuleData process(ModuleContext context, ModuleData inputModuleData) throws ModuleException {
ModuleData outputModuleData = inputModuleData;
key = getMessageKey(inputModuleData);
try { Hashtableenv = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl");
Context ctx = new InitialContext(env);
Object adapterObj = ctx.lookup("localejbs/CallSapAdapter");
if (adapterObj != null) {
try {
ModuleLocalHome adapterModule = (ModuleLocalHome) adapterObj;
ModuleLocal moduleLocal = adapterModule.create();
outputModuleData = moduleLocal.process(context, inputModuleData);
} catch (ModuleException e) {                       
throw new ModuleException((MessagingException) e.getCause());
} catch (CreateException e) {
audit.addAuditLogEntry(key, AuditLogStatus.ERROR, "Error found while trying  ModuleLocal instance" );
throw new ModuleException(e);
}
}
else { 
audit.addAuditLogEntry(key, AuditLogStatus.ERROR, "Unable to find adapter module.");
throw new ModuleException("Unable to find adapter module.");
}
}
catch (NamingException e) {
audit.addAuditLogEntry(key, AuditLogStatus.ERROR, "NamingException found: " + e.getMessage()); throw new ModuleException(e);
}
return outputModuleData;
}
private MessageKey getMessageKey(ModuleData inputModuleData) throws ModuleException {
MessageKey key = null;
try {
Object obj = null;
Message msg = null;
obj = inputModuleData.getPrincipalData();
msg = (Message) obj;
if (msg.getMessageDirection().equals(MessageDirection.OUTBOUND))
key = new MessageKey(msg.getMessageId(), MessageDirection.OUTBOUND);
else key = new MessageKey(msg.getMessageId(), MessageDirection.INBOUND);
}
catch (Exception e) {
throw new ModuleException("Unable to get message key",e);
}
return key;
}
@PreDestroy public void releaseResources() {
}
}

Viewing all articles
Browse latest Browse all 676

Trending Articles