It seems that it would be desirable to encapsulate any remote references to
EJBObjects with some type of delegation object. This way, if an error occurs
when referencing this remote object, a recovery process could be initiated,
or an email could be sent, or any number of pre- and post-call operations
can take place.
When designing such a class, at least in JDK 1.2, reflection jumps to the
forefront as a means for creating a generic delegation wrapper. If the
remote reference is passed to this delegation object, the delegation object
can make calls before and after the business method is invoked. On the
surface this appears very favorable.
One problem I currently face occurs when dealing with exceptions. Any method
call on an EJBObject class can throw a RemoteException, as well as any
number of application exceptions. Suppose I want to handle RemoteException's
by retrying the method call after a specific delay. The invoke method for a
basic reflection mechanism looks like the method below. The actual
reflection invocation occurs in invoke0() and is not of importance to my
question. I have also removed error trapping for reflection type errors from
this example:
public Object invoke(String methodName, Object[] parameters) throws
Exception {
Object result = null;
int attempts = 0;
while (attempts++ < 3) {
try {
result = invoke0(methodName, parameters);
} catch (Exception x) {
if (x instanceof RemoteException) {
if (attempts == 3) {
throw new EJBConnectionException(x.getMessage());
}
try { Thread.sleep(2000); }
catch (InterruptedException ix) {}
} else
throw x;
}
}
return result;
}
The example implementation above wants to retry the method if a
RemoteException occurs. In order to trap for this exception, I have to use
instanceof. If it is of type RemoteException, I pause and retry the method.
So far, so good.
Now I want the caller of this method to be able to trap application
exceptions the same way as they used to:
try {
someRemote.increaseSalary(5000);
} catch (SalaryException sx) {
...do something
}
But now, because my invoke method throws Exception, the client requests have
to catch this exception as well.
try {
someRemote.increaseSalary(5000);
} catch (SalaryException sx) {
...do something
} catch (Exception x) {
...do nothing or something
}
Long winded...sorry.
Is there a way to design the reflection invocation so that it does not have
to throw Exception? I also don't want to wrap the exception in a
RuntimeException, as that would require all business methods to unwrap the
RuntimeException, to find the actual exception.
Also, the Proxy ability in JDK 1.3 may save my bacon on this design, but I
can't go there...yet.
thanks,
jim
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".