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:

  1. 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
    dependency.
    -     Easily breakable (developers will mistype), doesn’t express container
    dependency.
     
  2. 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).
     
  3. Base class. Provide template code in base class, have developers extend from
    it.
    +   No template code, not breakable.
    -    Single inheritance in java makes this base class a no-no.
     
  4. 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.
    -    ?
     
  5. EntityManager callbacks as opposed to POJO callbacks. e.g.

    EntityManager.registerLifeCycleCallbackListener
      (LifeCycleListener listener,
    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
    happens.
    +/-   "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?