Jakarta EE CDI 事件 TransactionPhase.AFTER_SUCCESS 事件顺序相反

问题描述 投票:0回答:1

当使用

AFTER_SUCCESS
注释观察者方法时,事件的接收顺序与事务期间实际触发的顺序相反。

伪代码示例:

@Transactional
void test(){
    pushEvent.fire( new PushEvent(10) );
    pushEvent.fire( new PushEvent(20) );
    pushEvent.fire( new PushEvent(30) ); 
}

观察者:

void onPushEvent( @Observes( during = TransactionPhase.AFTER_SUCCESS ) PushEvent event ){
   System.out.println(event.getValue())
}

意想不到但观察到的结果:

30
20
10

这个可以更改吗?

events jakarta-ee cdi
1个回答
0
投票

在我的项目中使用事件的线程本地缓冲区修复了此问题,该缓冲区在 AFTER_SUCCESS 上刷新并按照事件出现的顺序重播事件

ThreadLocal<List<PushEvent>> threadEventBufferHolder = ThreadLocal.withInitial( ArrayList::new );

void onPushEvent( @Observes( during = TransactionPhase.IN_PROGRESS ) PushEvent event ){
    threadEventBufferHolder.get().add( event );
}

void onPushEventFailure( @Observes( during = TransactionPhase.AFTER_FAILURE ) PushEvent event ){
    buffer.clear();
}

void onPushEventCommit( @Observes( during = TransactionPhase.AFTER_SUCCESS ) PushEvent event ){
    List<PushEvent> buffer = threadEventBufferHolder.get();
    buffer.forEach( this::doPrintlnValue );
    buffer.clear();
}

删除了不相关的代码以便于阅读

© www.soinside.com 2019 - 2024. All rights reserved.