事务期间触发的 CDI 事件的范围

Scope of CDI events fired during a transaction

来自Weld documentation

Transactional observers receive their event notifications during the before or after completion phase of the transaction in which the event was raised.

这是否意味着在事务期间触发的 CDI 事件的范围仅限于该事务?

我想是的,但我只是想确保在事务完成时我不需要做任何清理工作。

我想你对这个概念有点误解。

没有"scope of events"这样的东西。交易的故事如下——你有一个交易方法,在那个方法中,你触发了一个事件。在您的应用程序的某个地方,您有观察者,这些观察者具有观察此事件所需的类型,并且它们也绑定到特定的事务阶段。

这实际上意味着,当交易达到给定状态时,此类观察者将收到通知 - 不早不晚:

    public void observeAfterCompletion(@Observes(during = AFTER_COMPLETION) Foo someEvent) {
        // this will be notified once the transaction reached AFTER_CEMPLETION stage
    }

    public void observeBeforeCompletion(@Observes(during = BEFORE_COMPLETION) Foo event) {
        // this will be notified once the transaction reaches BEFORE_COMPLETION stage
    }

有关事务何时达到给定状态的更多信息,您需要检查 JTA 规范,而不是 CDI。

I don't have to do any cleanup when a transaction completed.

不,你不知道。标准事件后你不清理任何东西,这里也不需要清理。