相关疑难解决方法(0)

处理事务中的spring-data-rest应用程序事件

我需要在更新数据时通过JMS向外部系统发布通知事件.我想这样做是在同一个事务中完成的,因为对象被提交到数据库以确保完整性.

Spring-data-rest发出的ApplicationLifecycle事件似乎是实现此逻辑的逻辑位置.

@org.springframework.transaction.annotation.Transactional
public class TestEventListener extends AbstractRepositoryEventListener<Object> {

    private static final Logger LOG = LoggerFactory.getLogger(TestEventListener.class);

    @Override
    protected void onBeforeCreate(Object entity) {
        LOG.info("XXX before create");
    }

    @Override
    protected void onBeforeSave(Object entity) {
        LOG.info("XXX before save");
    }

    @Override
    protected void onAfterCreate(Object entity) {
        LOG.info("XXX after create");
    }

    @Override
    protected void onAfterSave(Object entity) {
        LOG.info("XXX after save");
    }

}
Run Code Online (Sandbox Code Playgroud)

但是,这些事件发生在tx开始和提交之前和之后.

08 15:32:37.119 [http-nio-9000-exec-1] INFO  n.c.v.vcidb.TestEventListener - XXX before create 
08 15:32:37.135 [http-nio-9000-exec-1] TRACE o.s.t.i.TransactionInterceptor - Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]



08 15:32:37.432 [http-nio-9000-exec-1] …
Run Code Online (Sandbox Code Playgroud)

spring-transactions spring-data-jpa spring-data-rest

14
推荐指数
1
解决办法
4007
查看次数