Search code examples
javacdiweldapache-tomeeopenwebbeans

CDI 1.1: Is @Observes @Initialized(TransactionScoped.class) supposed to work?


CDI 1.1: Is @Observes @Initialized(TransactionScoped.class) supposed to work?

@ApplicationScoped
public class MyClass {
public void processConversationScopedInit(@Observes 
  @Initialized(TransactionScoped.class) Object payload) {}

  public void processConversationScopedDestroyed(@Observes 
  @Destroyed(TransactionScoped.class) Object payload) {}
}

It's a standard scope in CDI 1.1, but it's conspiciuously missing from this list here:

http://www.next-presso.com/2014/06/you-think-you-know-everything-about-cdi-events-think-again/

I can't seem to get it to work in TomEE 7.0.5/OpenWebBeans, and I'm curious if that's just an oversight in the spec. Thanks!


Solution

  • @TransactionScoped is not a standard scope in a way that it is not implemented by CDI (or, well, CDI impls). It comes from JTA and it also has their package - javax.transaction.

    That being said, it should behave as all other @NormalScope contexts and hence should fire the @Initialized events for you to observe. Although do note that CDI spec only recommends this by saying:

    Portable extensions are encouraged to fire an event with qualifier @Initialized(X.class) when a custom context is initialized, and an event with qualifier @Destroyed(X.class) when a custom context is destroyed, where X is the scope type associated with the context. A suitable event payload should be chosen.

    I haven't tried this myself but I would wager this works for most up to date implementations. Then again, you are in CDI 1.1 which is now pretty ancient.