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

时间:2014-09-08 03:38:02

标签: spring-data-jpa spring-transactions 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");
    }

}

然而,这些事件发生在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] TRACE o.s.t.i.TransactionInterceptor - Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save] 
08 15:32:37.479 [http-nio-9000-exec-1] INFO  n.c.v.vcidb.TestEventListener - XXX after create 

spring-data-rest有什么扩展点来添加将在spring管理事务中执行的行为?

3 个答案:

答案 0 :(得分:7)

我使用aop(切入点和tx建议)来解决这个问题:

@Configuration
@ImportResource("classpath:/aop-config.xml")
public class AopConfig { ...

aop-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop.xsd
                      http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd"
    default-autowire="byName">

    <aop:config>
        <aop:pointcut id="restRepositoryTx"
            expression="execution(* org.springframework.data.rest.webmvc.RepositoryEntityController.*(..))" />
        <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut-ref="restRepositoryTx" order="20" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="postCollectionResource*" propagation="REQUIRES_NEW" rollback-for="Exception" />
            <tx:method name="putItemResource*" propagation="REQUIRES_NEW" rollback-for="Exception" />
            <tx:method name="patchItemResource*" propagation="REQUIRES_NEW" rollback-for="Exception" />
            <tx:method name="deleteItemResource*" propagation="REQUIRES_NEW" rollback-for="Exception" />
            <!-- <tx:method name="*" rollback-for="Exception" /> -->
        </tx:attributes>
    </tx:advice>

</beans>

这与使用@Transactional注释的控制器方法相同。

答案 1 :(得分:1)

phlebas 描述的解决方案有效。而且我还认为“ 在同一事务中运行事件处理程序”应该是Spring Data Rest应该提供的功能。有许多常见的用例需要拆分逻辑以分离eventHandler。就像“数据库中的触发器”一样。下面显示的版本与 phlebas 解决方案相同。

    @Aspect
    @Component
    public class SpringDataRestTransactionAspect {

        private TransactionTemplate transactionTemplate;

        public SpringDataRestTransactionAspect(PlatformTransactionManager transactionManager) {
            this.transactionTemplate = new TransactionTemplate(transactionManager);
            this.transactionTemplate.setName("around-data-rest-transaction");
        }

        @Pointcut("execution(* org.springframework.data.rest.webmvc.*Controller.*(..))")
        public void aroundDataRestCall(){}

        @Around("aroundDataRestCall()")
        public Object aroundDataRestCall(ProceedingJoinPoint joinPoint) throws Throwable {
            return transactionTemplate.execute(transactionStatus -> {
                try {
                    return joinPoint.proceed();
                } catch (Throwable e) {
                    transactionStatus.setRollbackOnly();
                    if(e instanceof RuntimeException) {
                        throw (RuntimeException)e;
                    } else {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
    }

答案 2 :(得分:0)

我没有参与spring-data-rest,但是对于spring,可以通过以下方式处理。

1)定义自定义TransactionSynchronizationAdapter,并在TransactionSynchronizationManager中注册bean。

通常,我有一个方法registerSynchronizaiton,带有@Before切入点。

@SuppressWarnings("rawtypes") @Before("@annotation(org.springframework.transaction.annotation.Transactional)")
    public void registerSynchronization() {
        // TransactionStatus transStatus = TransactionAspectSupport.currentTransactionStatus();
        TransactionSynchronizationManager.registerSynchronization(this);
        final String transId = UUID.randomUUID().toString();
        TransactionSynchronizationManager.setCurrentTransactionName(transId);
        transactionIds.get().push(transId);
        if (TransactionSynchronizationManager.isActualTransactionActive() && TransactionSynchronizationManager
            .isSynchronizationActive() && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
            if (!TransactionSynchronizationManager.hasResource(KEY)) {
                final List<NotificationPayload> notifications = new ArrayList<NotificationPayload>();
                TransactionSynchronizationManager.bindResource(KEY, notifications);
            }
        }
    }

2)并且,实现如下的覆盖方法

@Override public void afterCompletion(final int status) {
    CurrentContext context = null;
    try {
        context = ExecutionContext.get().getContext();
    } catch (final ContextNotFoundException ex) {
        logger.debug("Current Context is not available");
        return;
    }
    if (status == STATUS_COMMITTED) {
        transactionIds.get().removeAllElements();
        publishedEventStorage.sendAllStoredNotifications();
        // customize here for commit actions
    } else if ((status == STATUS_ROLLED_BACK) || (status == STATUS_UNKNOWN)) {
       // you can write your code for rollback actions
    }
}