The @EJB annotation can be use in "managed clients" to access an EJB.
One can place this annotation in a servlet class, declaring a member variable.
public class MyServlet extends HttpServlet {
@EJB
private MyWorkerInterface theWorker;
}
That @EJB annotation is expanded to JNDI lookups that (I assume) are executed when the servlet initialises. Those JNDI lookups might fail: the EJB provider can choose to modify their annotations to specify particular JNDI names, my @EJB reference would then need to specify the non-default JNDI name or the lookup would fail.
Also I guess, as the EJB can be remote there is the possibility of transient, network failures and server-bounce errors.
My thought: when using theWorker, I should check for its validity.
if ( theWorker == null ) {
// ... etc.
My questions:
1.) Are such null checks necessary?
2.) If they are, and the nulls may be caused by a transient error such as a temporary failure of the remote server, is any recovery possible? The servlet is now intialised. Do I really need to restart my servlet in order to recover? Surely not?
3.) Tentative thought: Explicit, lazy, JNDI lookup code may be needed in preference to using @EJB. Comments?