在事务中处理 spring-data-rest 应用程序事件 [英] Handle spring-data-rest application events within the transaction
问题描述
当数据更新时,我需要通过 JMS 将通知事件发布到外部系统.我希望在将对象提交到数据库以确保完整性的同一事务中完成此操作.
I need to publish notification events to external systems over JMS, when data is updated. Id like this to be done within the same transaction as the objects are committed to the database to ensure integrity.
spring-data-rest 发出的 ApplicationLifecycle 事件似乎是实现此逻辑的合乎逻辑的地方.
The ApplicationLifecycle events that spring-data-rest emits seemed like the logical place to implement this logic.
@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 开始和提交之前和之后.
However, these events happen before and after the tx starts and commits.
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 管理的事务中执行的行为?
What extension point does spring-data-rest have for adding behaviour that will execute within the spring managed transaction?
推荐答案
我使用 aop(切入点和 tx 建议)来解决这个问题:
I use aop (pointcut and tx advice) to solve this problem:
@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 注释控制器方法相同.
This is the same as having controller methods annotated with @Transactional.
这篇关于在事务中处理 spring-data-rest 应用程序事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!