3

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.

Luis Soeiro
  • 4,262
  • 6
  • 35
  • 45

1 Answers1

7

An @Remote interface that also implements java.rmi.Remote will have it's exception handling changed slightly. Namely, every method in the interface will be required to declare java.rmi.RemoteException. That will allow the client to receive the connection related issues. In the absence of java.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.

@Remote
public interface OrangeRemote extends java.rmi.Remote {

    public void doSomething() throws RemoteException;
}

@Stateless
public class OrangeBean implements OrangeRemote {

    @Override
    public void doSomething() {
        //...
    }
}

Note that the bean itself is not required to declare RemoteException in 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.

David Blevins
  • 19,178
  • 3
  • 53
  • 68
  • Do I still get EJBExceptions and only network issues raise RemoteException? Or does RemoteExcpetion encapsulate all exceptions? – Luis Soeiro Jun 03 '11 at 16:43
  • You still get EJBException for anything not related to moving data across the wire. Note that RemoteException covers all 'moving data' issues such sending objects which might not be serializable. The client, server, and connection could be fine in that scenario. – David Blevins Jun 03 '11 at 18:50
  • Unfortunately, it seems that we are getting our SystemException (runtime exception) wrapped around a RemoteException. It should have been wrapped on a EJBException, shouldn't it? – Luis Soeiro Feb 23 '12 at 15:53