We are currently trying to figure out how callbacks should be implemented
with EJB 3. So far, we have identified five different techniques and I’d
like to get some feedback from readers about them. Here is a quick
rundown, edited from messages sent by Marc Fleury and Craig Russell, along with
their respective pros and cons:
- Magic callbacks. Method signature is the same as a container defined
one. Eg public void ejbPassivate();
+ Very lightweight, no template code, no base class, no interface
- Easily breakable (developers will mistype), doesn’t express container
- Interface. Current state in 2.x spec.
+ Clear dependency expression, not breakable.
Lot of template code (today’s code is cluttered with many lines of callback
template code that does nothing).
- Base class. Provide template code in base class, have developers extend from
+ No template code, not breakable.
- Single inheritance in java makes this base class a no-no.
- Annotations. Annotate any method as callback (as the example of "@remove"
that Linda already showed)
+ No template code, not breakable (annotations can be supported in
IDE/compile), any name.
- EntityManager callbacks as opposed to POJO callbacks. e.g.
Class classOfInterest, long lifeCycleEventsOfInterest);
+ No interference with POJO class.
+ Exploits well-known event listener paradigm.
+ Very lightweight, only called if specific class/life cycle event
+/- "aspect oriented".
Not object-oriented (code outside POJO affects POJO).
- How can we extend this to Session beans?
Can you think of other options? Which one would you favor?