简体   繁体   English

Spring AMQP 侦听器事务的目的是什么

[英]What's the purpose for Spring AMQP listener transactions

I'm trying to understand how transaction works in Spring AMQP.我试图了解事务在 Spring AMQP 中的工作原理。 Reading the docs:https://docs.spring.io/spring-amqp/reference/html/#transactions , I know the purpose for enabling transaction in publisher (Best Effort One Phase Commit pattern) but I have no idea why it could be necessary in MessageListener?阅读文档:https://docs.spring.io/spring-amqp/reference/html/#transactions ,我知道在发布者中启用事务的目的(尽力而为一阶段提交模式)但我不知道为什么它可以在 MessageListener 中有必要吗?

Let's take an example:让我们举个例子:

acknowledgeMode=AUTO

  1. Consume message using @RabbitListener使用@RabbitListener消费消息
  2. Insert data into database将数据插入数据库
  3. Publish message using rabbitTemplate使用rabbitTemplate发布消息

According to docs: https://docs.spring.io/spring-amqp/reference/html/#acknowledgeMode , if acknowledgeMode is set to AUTO then if any next operation fails, listener will fail too and message will be returned to queue.根据文档: https://docs.spring.io/spring-amqp/reference/html/#acknowledgeMode ,如果acknowledgeMode设置为AUTO那么如果任何下一个操作失败,监听器也会失败并且消息将返回到队列。

Another question is whats the difference between local transaction and external transaction in that case (setting container.setTransactionManager(transactionManager()); or not)?另一个问题是在那种情况下本地事务和外部事务之间的区别是什么(设置container.setTransactionManager(transactionManager());或不设置)?

I would appreciate some clarification:)我会很感激一些澄清:)

Enable transactions in the listener so that any/all downstream RabbitTemplate operations participate in the same transaction.在侦听器中启用事务,以便任何/所有下游RabbitTemplate操作参与同一事务。

If there is a failure, the container will rollback the transaction (removing the publishes), nack the message (or messages if batch size is greater than one) and then commit the nacks so the message(s) will be redelivered.如果出现故障,容器将回滚事务(删除发布),nack 消息(或消息,如果批大小大于一个)然后提交 nack,以便重新传递消息。

When using an external transaction manager (such as JDBC), the container will synchronize the AMQP transaction with the external transaction.当使用外部事务管理器(如JDBC)时,容器会将AMQP事务与外部事务同步。

Downstream templates participate in the transaction regardless of whether it is local (AMQP only) or synchronized.下游模板参与事务,无论它是本地的(仅限 AMQP)还是同步的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM