mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add boolean flag spring.rabbit.listener.useTransactionManager
If set *and* there is a transaction manager in the context, then the listener container created by Spring Boot will use the transaction manager to execute the listeners. Fixes gh-3432 (by virtue of not requiring a JtaTransactionManager any more).
This commit is contained in:
parent
f6c395d0a6
commit
67933ab01f
@ -25,7 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.transaction.jta.JtaTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration for Spring AMQP annotation driven endpoints.
|
* Configuration for Spring AMQP annotation driven endpoints.
|
||||||
@ -38,15 +38,16 @@ import org.springframework.transaction.jta.JtaTransactionManager;
|
|||||||
class RabbitAnnotationDrivenConfiguration {
|
class RabbitAnnotationDrivenConfiguration {
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private JtaTransactionManager transactionManager;
|
private PlatformTransactionManager transactionManager;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
|
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
|
||||||
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
|
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
|
||||||
ConnectionFactory connectionFactory) {
|
ConnectionFactory connectionFactory, RabbitProperties config) {
|
||||||
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
|
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
|
||||||
factory.setConnectionFactory(connectionFactory);
|
factory.setConnectionFactory(connectionFactory);
|
||||||
if (this.transactionManager != null) {
|
if (this.transactionManager != null
|
||||||
|
&& config.getListener().isUseTransactionManager()) {
|
||||||
factory.setTransactionManager(this.transactionManager);
|
factory.setTransactionManager(this.transactionManager);
|
||||||
}
|
}
|
||||||
return factory;
|
return factory;
|
||||||
|
@ -91,7 +91,7 @@ public class RabbitAutoConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(RabbitTemplate.class)
|
@ConditionalOnMissingBean(RabbitTemplate.class)
|
||||||
public RabbitTemplate rabbitTemplate() {
|
public RabbitTemplate rabbitTemplate(RabbitProperties config) {
|
||||||
return new RabbitTemplate(this.connectionFactory);
|
return new RabbitTemplate(this.connectionFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,6 +62,12 @@ public class RabbitProperties {
|
|||||||
*/
|
*/
|
||||||
private String addresses;
|
private String addresses;
|
||||||
|
|
||||||
|
private Listener listener = new Listener();
|
||||||
|
|
||||||
|
public Listener getListener() {
|
||||||
|
return this.listener;
|
||||||
|
}
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
if (this.addresses == null) {
|
if (this.addresses == null) {
|
||||||
return this.host;
|
return this.host;
|
||||||
@ -156,4 +162,16 @@ public class RabbitProperties {
|
|||||||
this.virtualHost = ("".equals(virtualHost) ? "/" : virtualHost);
|
this.virtualHost = ("".equals(virtualHost) ? "/" : virtualHost);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Listener {
|
||||||
|
private boolean useTransactionManager = false;
|
||||||
|
|
||||||
|
public boolean isUseTransactionManager() {
|
||||||
|
return this.useTransactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUseTransactionManager(boolean useTransactionManager) {
|
||||||
|
this.useTransactionManager = useTransactionManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user