Spring @EventListener 异步中使用condition的问题

@EventListener是spring在4.2+推出的更好的使用spring事件架构的方式,并且异步方式也很好设定

但是在spring4.2.7版本上使用eventlistener的condition 的使用需要注意以下情况可能失效:

condition 使用例子如@EventListener(condition = "#event.isAsync")

1. 需要对同一个事件进行区分同步异步

2. 使用condition来进行过滤

例如:需要对事件用condition进行区分同步异步

@Async

@EventListener(condition = "#event.isAsync")

public void handleOrderCreatedEventAsync(TestEvent event) {  

}  

@EventListener(condition = "#event.isAsync == false")

public void handleOrderCreatedEvent(TestEvent event) {  

}  

修正的做法,是使用两个事件区分即:

@Async

@EventListener

public void handleOrderCreatedEventAsync(TestEventAsync event) {  

}  

@EventListener

public void handleOrderCreatedEvent(TestEvent event) {  

}  

还不清楚,在更高的版本上是否已经有进一步的修正,待以后研究

猜你喜欢

转载自www.cnblogs.com/zuochanzi/p/9572310.html
今日推荐