views:

341

answers:

1

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?

+1  A: 

Sorry for not giving any references to specs, I'm only speaking from previous experience with JBoss.

1) No, unless the container made a mistake.
2) Not relevant, your instance will be a proxy to the remote service, implementing the remote interface. The injection will therefore always succeed. The error will arise when you call the methods of the proxy, so you need to be prepared to handle those errors when using the remote interface. That is what RemoteException was intended for, all errors should be wrapped in a RemoteException for you to catch if something goes wrong. If you let it propagate, the tx will be rolled back, which is a sane default if you manipulate tx-enabled resources only.
3) This is usually only needed if you need different initial context properties for the JNDI lookup, but for these cases I would personally use a DI engine and another annotation (@EJBFromHost2 for instance). Using explicit JNDI lookups gets very kludgy, especially if you later want to change to another JNDI implementation or settings (if you want to cluster your app for instance).

disown
Thanks. I guess my whole question revoves around the issue of whether your answer to 1 is right. I think in simple cases, eg. 1 EAR with both EJBs and (serlvet) client you're right. But in more complex scenarios where JNDI names can be specified in the annotations then I think **we** rather than the container may be able to get it wrong.
djna
Having talked with people that "know", it does appear that you're correct about the container's obligations. The servlet should not be started if the @EJB injection fails. Hence the servlet author should be able to rely upon the reference not being null. However not all containers are bug free, so we might want to code defensively - depends whether one object to NPE stack traces filling up one's logs.
djna
While I agree with you that some containers are buggy, I think that you need to rely on them. You have to make some basic assumptions about your runtime environment, otherwise you would need to check for everything, including memory allocations etc. That is why exceptions where invented, to show errors although you don't explicitly have to check for them. My recommendation would be to trust the container and let the errors propagate to exceptions. The only exception to this rule is if you really want to be able to do recovery or switch to another implementation etc.
disown
Yes I agree with the principle. It's just that if we do have that container bug, then every user request that hits the app will cause an NPE, if each produces a stack trace in the logs we get swamped by noise. If I knew my container was buggy, I might choose to defend against it. I really shouldn't need to though.
djna
My general feeling is that remote should be considered as an alternative to RMI, not a way of partitioning the app. Look at seam for instance, they basically assume sticky session type load balancing, remote support is minimal. I guess this is one of the fallacies, the network will never be reliable, so you better not build your app to rely on it.
disown
But apps barfing is very annoying in JEE, I agree
disown