@TransactionalEventListener at spring batch

£可爱£侵袭症+ 提交于 2020-04-18 06:31:10

问题


I want to process something after commit on spring batch.

And I tried this example (https://dzone.com/articles/transaction-synchronization-and-spring-application).

That example works perfectly on springboot like this flow.

1. some update query and event publish by ApplicationEventPublisher
2. some update query and event publish by ApplicationEventPublisher
3. some update query and event publish by ApplicationEventPublisher
4. commit
5. after commit logic I maked
6. after commit logic I maked
7. after commit logic I maked

But on spring batch not work as expected.

1. some update query and event publish by ApplicationEventPublisher, and after commit logic
2. some update query and event publish by ApplicationEventPublisher, and after commit logic
3. some update query and event publish by ApplicationEventPublisher, and after commit logic
3. commit

I doubt @TransactionalEventListener or ApplicationEventPublisher doesn't work on spring batch.

Is any difference spring context between springboot and spring batch??

Or for @TransactionalEventListener, Is any setting on spring batch??


回答1:


  1. some update query and event publish by ApplicationEventPublisher

Spring Batch does not intercept those events. You need to make your listener participate in one of the step lifecycle events (before/after read, before/after write, etc)

I want to process something after commit on spring batch.

You can use a ChunkListener#afterChunk for that, which is called after the transaction is committed.



来源:https://stackoverflow.com/questions/60966901/transactionaleventlistener-at-spring-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!