3 Replies Latest reply on Feb 11, 2019 1:30 AM by ljnelson

    Firing an @Initialized(TransactionScoped.class) event

    ljnelson

      I wonder: should there be a CDI-specific wrapper around Narayana's TransactionManagerImple that fires an @Initialized(TransactionScoped.class)-qualified event?

       

      The specification says (in section 6.7):

       

      Portable extensions are encouraged to synchronously fire:

      • an event with qualifier @Initialized(X.class) when a custom context is initialized, i.e. ready for use,
      • an event with qualifier @BeforeDestroyed(X.class) when a custom context is about to be destroyed, i.e. before the actual destruction,
      • an event with qualifier @Destroyed(X.class) when a custom context is destroyed, i.e. after the actual destruction,

      where X is the scope type associated with the context.

      A suitable event payload should be chosen.

       

      I'm thinking that in its begin() method a Narayana-supplied but CDI-specific transaction manager wrapping Narayana's "ordinary" one should do:

      @Inject
      @Initialized(TransactionScoped.class)
      private Event<Transaction> initializationBroadcaster;

      @Inject @BeforeDestroyed(TransactionScoped.class)
      private Event<Transaction> beforeDestructionBroadcaster;

      @Inject @Destroyed(TransactionScoped.class)
      private Event<TransactionManager> destructionBroadcaster;

      // in its begin() method:
      super.begin();
      initializationBroadcaster.fire(this.getTransaction());

      // in its commit() and rollback() methods and anywhere else I'm forgetting:
      beforeDestructionBroadcaster.fire(this.getTransaction());
      super.commit(); // or rollback or whatever
      destructionBroadcaster.fire(this);

      I've typed this code without proofreading or testing but I think maybe you get the idea.

       

      I don't really care about the payloads of the various Event objects, though for the initialization one it seems handy to have the Transaction itself be the payload (so receivers can register synchronizations on it).

       

      If there's interest I'll put together a pull request.

       

      Best,

      Laird