Skip to content

Commit 2245bce

Browse files
committed
Merge pull request #9055 from garyrussell:GH-9041
* pr/9055: Remove deprecated spring.rabbitmq.listener.* properties Polish "Support direct AMQP container" Support direct AMQP container
2 parents 0f38031 + 1e9c1a6 commit 2245bce

File tree

9 files changed

+387
-315
lines changed

9 files changed

+387
-315
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.amqp;
18+
19+
import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFactory;
20+
import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder;
21+
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
22+
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
23+
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
24+
import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;
25+
import org.springframework.amqp.support.converter.MessageConverter;
26+
import org.springframework.boot.autoconfigure.amqp.RabbitProperties.ListenerRetry;
27+
import org.springframework.util.Assert;
28+
29+
/**
30+
* Configure {@link RabbitListenerContainerFactory} with sensible defaults.
31+
*
32+
* @param <T> the container factory type.
33+
* @author Gary Russell
34+
* @author Stephane Nicoll
35+
* @since 2.0.0
36+
*/
37+
public abstract class AbstractRabbitListenerContainerFactoryConfigurer<
38+
T extends AbstractRabbitListenerContainerFactory<?>> {
39+
40+
private MessageConverter messageConverter;
41+
42+
private MessageRecoverer messageRecoverer;
43+
44+
private RabbitProperties rabbitProperties;
45+
46+
/**
47+
* Set the {@link MessageConverter} to use or {@code null} if the out-of-the-box
48+
* converter should be used.
49+
* @param messageConverter the {@link MessageConverter}
50+
*/
51+
protected void setMessageConverter(MessageConverter messageConverter) {
52+
this.messageConverter = messageConverter;
53+
}
54+
55+
/**
56+
* Set the {@link MessageRecoverer} to use or {@code null} to rely on the default.
57+
* @param messageRecoverer the {@link MessageRecoverer}
58+
*/
59+
protected void setMessageRecoverer(MessageRecoverer messageRecoverer) {
60+
this.messageRecoverer = messageRecoverer;
61+
}
62+
63+
/**
64+
* Set the {@link RabbitProperties} to use.
65+
* @param rabbitProperties the {@link RabbitProperties}
66+
*/
67+
protected void setRabbitProperties(RabbitProperties rabbitProperties) {
68+
this.rabbitProperties = rabbitProperties;
69+
}
70+
71+
protected final RabbitProperties getRabbitProperties() {
72+
return this.rabbitProperties;
73+
}
74+
75+
/**
76+
* Configure the specified rabbit listener container factory. The factory can be
77+
* further tuned and default settings can be overridden.
78+
* @param factory the {@link AbstractRabbitListenerContainerFactory} instance to
79+
* configure
80+
* @param connectionFactory the {@link ConnectionFactory} to use
81+
*/
82+
public abstract void configure(T factory, ConnectionFactory connectionFactory);
83+
84+
85+
protected void configure(T factory, ConnectionFactory connectionFactory,
86+
RabbitProperties.AmqpContainer configuration) {
87+
Assert.notNull(factory, "Factory must not be null");
88+
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
89+
Assert.notNull(configuration, "Configuration must not be null");
90+
factory.setConnectionFactory(connectionFactory);
91+
if (this.messageConverter != null) {
92+
factory.setMessageConverter(this.messageConverter);
93+
}
94+
factory.setAutoStartup(configuration.isAutoStartup());
95+
if (configuration.getAcknowledgeMode() != null) {
96+
factory.setAcknowledgeMode(configuration.getAcknowledgeMode());
97+
}
98+
if (configuration.getPrefetch() != null) {
99+
factory.setPrefetchCount(configuration.getPrefetch());
100+
}
101+
if (configuration.getDefaultRequeueRejected() != null) {
102+
factory.setDefaultRequeueRejected(configuration.getDefaultRequeueRejected());
103+
}
104+
if (configuration.getIdleEventInterval() != null) {
105+
factory.setIdleEventInterval(configuration.getIdleEventInterval());
106+
}
107+
ListenerRetry retryConfig = configuration.getRetry();
108+
if (retryConfig.isEnabled()) {
109+
RetryInterceptorBuilder<?> builder = (retryConfig.isStateless()
110+
? RetryInterceptorBuilder.stateless()
111+
: RetryInterceptorBuilder.stateful());
112+
builder.maxAttempts(retryConfig.getMaxAttempts());
113+
builder.backOffOptions(retryConfig.getInitialInterval(),
114+
retryConfig.getMultiplier(), retryConfig.getMaxInterval());
115+
MessageRecoverer recoverer = (this.messageRecoverer != null
116+
? this.messageRecoverer : new RejectAndDontRequeueRecoverer());
117+
builder.recoverer(recoverer);
118+
factory.setAdviceChain(builder.build());
119+
}
120+
}
121+
122+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.amqp;
18+
19+
import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory;
20+
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
21+
22+
/**
23+
* Configure {@link DirectRabbitListenerContainerFactoryConfigurer} with sensible defaults.
24+
*
25+
* @author Gary Russell
26+
* @author Stephane Nicoll
27+
* @since 2.0
28+
*/
29+
public final class DirectRabbitListenerContainerFactoryConfigurer
30+
extends AbstractRabbitListenerContainerFactoryConfigurer<DirectRabbitListenerContainerFactory> {
31+
32+
@Override
33+
public void configure(DirectRabbitListenerContainerFactory factory, ConnectionFactory connectionFactory) {
34+
RabbitProperties.DirectContainer config = getRabbitProperties().getListener()
35+
.getDirect();
36+
configure(factory, connectionFactory, config);
37+
if (config.getConsumersPerQueue() != null) {
38+
factory.setConsumersPerQueue(config.getConsumersPerQueue());
39+
}
40+
}
41+
42+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.amqp;
1818

1919
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
20+
import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory;
2021
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
2122
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
2223
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
@@ -25,6 +26,7 @@
2526
import org.springframework.beans.factory.ObjectProvider;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2728
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2830
import org.springframework.context.annotation.Bean;
2931
import org.springframework.context.annotation.Configuration;
3032

@@ -55,24 +57,48 @@ class RabbitAnnotationDrivenConfiguration {
5557

5658
@Bean
5759
@ConditionalOnMissingBean
58-
public SimpleRabbitListenerContainerFactoryConfigurer rabbitListenerContainerFactoryConfigurer() {
60+
public SimpleRabbitListenerContainerFactoryConfigurer simpleRabbitListenerContainerFactoryConfigurer() {
5961
SimpleRabbitListenerContainerFactoryConfigurer configurer = new SimpleRabbitListenerContainerFactoryConfigurer();
6062
configurer.setMessageConverter(this.messageConverter.getIfUnique());
6163
configurer.setMessageRecoverer(this.messageRecoverer.getIfUnique());
6264
configurer.setRabbitProperties(this.properties);
6365
return configurer;
6466
}
6567

66-
@Bean
68+
@Bean(name = "rabbitListenerContainerFactory")
6769
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
68-
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
70+
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "simple", matchIfMissing = true)
71+
public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(
6972
SimpleRabbitListenerContainerFactoryConfigurer configurer,
7073
ConnectionFactory connectionFactory) {
7174
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
7275
configurer.configure(factory, connectionFactory);
7376
return factory;
7477
}
7578

79+
80+
@Bean
81+
@ConditionalOnMissingBean
82+
public DirectRabbitListenerContainerFactoryConfigurer directRabbitListenerContainerFactoryConfigurer() {
83+
DirectRabbitListenerContainerFactoryConfigurer configurer =
84+
new DirectRabbitListenerContainerFactoryConfigurer();
85+
configurer.setMessageConverter(this.messageConverter.getIfUnique());
86+
configurer.setMessageRecoverer(this.messageRecoverer.getIfUnique());
87+
configurer.setRabbitProperties(this.properties);
88+
return configurer;
89+
}
90+
91+
@Bean(name = "rabbitListenerContainerFactory")
92+
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
93+
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "direct")
94+
public DirectRabbitListenerContainerFactory directRabbitListenerContainerFactory(
95+
DirectRabbitListenerContainerFactoryConfigurer configurer,
96+
ConnectionFactory connectionFactory) {
97+
DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory();
98+
configurer.configure(factory, connectionFactory);
99+
return factory;
100+
}
101+
76102
@EnableRabbit
77103
@ConditionalOnMissingBean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)
78104
protected static class EnableRabbitConfiguration {

0 commit comments

Comments
 (0)