I would like to create an exception handler for the specific case that happens when an EJB client application loses the connection with the application server. The code we are creating is able to handle the client application in user friendly way to try to reconnect to the same server or another server in a fail-over environment. By “losing the connection” I mean anything the requires reconnecting. The cause maybe a network problem, server lock-up, service down, etc.
Here is an idea of what we are looking for (client code):
private void doSomething() throws RecoverableException {
//(...)
BusinessRemoteEJB ejb=ctx.lookup("BusinessRemoteEJB");
try {
List<product> list=ejb.getProducts();
//(...)
} catch (EJBException e){
Exception e = e.getCausedByException();
//Here is what I'm looking for: some excpetion that indicates a connection problem
if(e !=null && e instanceof EJBConnectionException){
//This will be catched in a proper place to try to reconnect
throw new RecoverableException(e);
} else {
//If it is any other excpetion, just let it work up the stack
throw e;
}
Of course EJBConnectionException doesn’t exist. Is there anything like it?
We are using OpenEJB 1.4 (EJB 3.x compliant). However, we want to use an exception handler that could be used across different application servers if possible.
Another thing is that some application servers provide some kind of fail over mechanisms but our requirements are a little specific and we can implement it directly in the client code.
An
@Remoteinterface that also implementsjava.rmi.Remotewill have it’s exception handling changed slightly. Namely, every method in the interface will be required to declarejava.rmi.RemoteException. That will allow the client to receive the connection related issues. In the absence ofjava.rmi.Remote, the container will convert all such issues to EJBException. There are no requirements beyond it being an EJBException, so relying on the cause or any subclass of EJBException is non-portable.Note that the bean itself is not required to declare
RemoteExceptionin the throws clause. Java lets you declare a subset of the exceptions when implementing an interface and EJB does not change that. Throwing RemoteException from the bean itself would defeat the entire point, so leaving it out is a good idea.