Skip to content

Commit 2c8658c

Browse files
committed
Polish "Add Rabbit connection factory configurers"
See gh-26982
1 parent a96b3ef commit 2c8658c

File tree

4 files changed

+81
-61
lines changed

4 files changed

+81
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2012-2021 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+
* https://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+
117
package org.springframework.boot.autoconfigure.amqp;
218

319
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory;
@@ -15,47 +31,43 @@
1531
*/
1632
public abstract class AbstractConnectionFactoryConfigurer<T extends AbstractConnectionFactory> {
1733

18-
private RabbitProperties rabbitProperties;
34+
private final RabbitProperties rabbitProperties;
1935

2036
private ConnectionNameStrategy connectionNameStrategy;
2137

22-
public RabbitProperties getRabbitProperties() {
23-
return rabbitProperties;
24-
}
25-
26-
public void setRabbitProperties(RabbitProperties rabbitProperties) {
27-
this.rabbitProperties = rabbitProperties;
38+
protected AbstractConnectionFactoryConfigurer(RabbitProperties properties) {
39+
Assert.notNull(properties, "RabbitProperties must not be null");
40+
this.rabbitProperties = properties;
2841
}
2942

30-
public ConnectionNameStrategy getConnectionNameStrategy() {
31-
return connectionNameStrategy;
43+
protected final ConnectionNameStrategy getConnectionNameStrategy() {
44+
return this.connectionNameStrategy;
3245
}
3346

34-
public void setConnectionNameStrategy(ConnectionNameStrategy connectionNameStrategy) {
47+
protected final void setConnectionNameStrategy(ConnectionNameStrategy connectionNameStrategy) {
3548
this.connectionNameStrategy = connectionNameStrategy;
3649
}
3750

3851
/**
39-
* Configure the specified Rabbit connection factory - delegating to
40-
* {@link #configureSpecific} for the connection factory implementation specific
41-
* settings. The factory can be further tuned and default settings can be overridden.
42-
* @param connectionFactory the connection factory instance to configure
52+
* Configures the given {@code connectionFactory} with sensible defaults.
53+
* @param connectionFactory connection factory to configure
4354
*/
44-
public void configure(T connectionFactory) {
55+
public final void configure(T connectionFactory) {
4556
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
4657
PropertyMapper map = PropertyMapper.get();
4758
map.from(this.rabbitProperties::determineAddresses).to(connectionFactory::setAddresses);
4859
map.from(this.rabbitProperties::getAddressShuffleMode).whenNonNull()
4960
.to(connectionFactory::setAddressShuffleMode);
50-
map.from(connectionNameStrategy).whenNonNull().to(connectionFactory::setConnectionNameStrategy);
51-
configureSpecific(connectionFactory);
61+
map.from(this.connectionNameStrategy).whenNonNull().to(connectionFactory::setConnectionNameStrategy);
62+
configure(connectionFactory, this.rabbitProperties);
5263
}
5364

5465
/**
55-
* Configure the specified Rabbit connection factory with implementation specific
56-
* settings.
57-
* @param connectionFactory the connection factory instance to configure
66+
* Configures the given {@code connectionFactory} using the given
67+
* {@code rabbitProperties}.
68+
* @param connectionFactory connection factory to configure
69+
* @param rabbitProperties properties to use for the configuration
5870
*/
59-
protected abstract void configureSpecific(T connectionFactory);
71+
protected abstract void configure(T connectionFactory, RabbitProperties rabbitProperties);
6072

6173
}

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2012-2021 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+
* https://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+
117
package org.springframework.boot.autoconfigure.amqp;
218

319
import java.time.Duration;
@@ -6,23 +22,20 @@
622
import org.springframework.boot.context.properties.PropertyMapper;
723

824
/**
9-
* Configures {@link CachingConnectionFactory Rabbit CachingConnectionFactory} with
10-
* sensible defaults.
25+
* Configures Rabbit {@link CachingConnectionFactory} with sensible defaults.
1126
*
1227
* @author Chris Bono
1328
* @since 2.6.0
1429
*/
1530
public class CachingConnectionFactoryConfigurer extends AbstractConnectionFactoryConfigurer<CachingConnectionFactory> {
1631

17-
/**
18-
* Configure the specified Rabbit caching connection factory with implementation
19-
* specific settings.
20-
* @param connectionFactory the {@link CachingConnectionFactory} instance to configure
21-
*/
32+
public CachingConnectionFactoryConfigurer(RabbitProperties properties) {
33+
super(properties);
34+
}
35+
2236
@Override
23-
public void configureSpecific(CachingConnectionFactory connectionFactory) {
37+
public void configure(CachingConnectionFactory connectionFactory, RabbitProperties rabbitProperties) {
2438
PropertyMapper map = PropertyMapper.get();
25-
RabbitProperties rabbitProperties = getRabbitProperties();
2639
map.from(rabbitProperties::isPublisherReturns).to(connectionFactory::setPublisherReturns);
2740
map.from(rabbitProperties::getPublisherConfirmType).whenNonNull()
2841
.to(connectionFactory::setPublisherConfirmType);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ protected static class RabbitConnectionFactoryCreator {
9999
RabbitConnectionFactoryBeanConfigurer rabbitConnectionFactoryBeanConfigurer(RabbitProperties properties,
100100
ResourceLoader resourceLoader, ObjectProvider<CredentialsProvider> credentialsProvider,
101101
ObjectProvider<CredentialsRefreshService> credentialsRefreshService) {
102-
RabbitConnectionFactoryBeanConfigurer configurer = new RabbitConnectionFactoryBeanConfigurer();
103-
configurer.setRabbitProperties(properties);
104-
configurer.setResourceLoader(resourceLoader);
102+
RabbitConnectionFactoryBeanConfigurer configurer = new RabbitConnectionFactoryBeanConfigurer(resourceLoader,
103+
properties);
105104
configurer.setCredentialsProvider(credentialsProvider.getIfUnique());
106105
configurer.setCredentialsRefreshService(credentialsRefreshService.getIfUnique());
107106
return configurer;
@@ -111,8 +110,7 @@ RabbitConnectionFactoryBeanConfigurer rabbitConnectionFactoryBeanConfigurer(Rabb
111110
@ConditionalOnMissingBean
112111
CachingConnectionFactoryConfigurer rabbitConnectionFactoryConfigurer(RabbitProperties rabbitProperties,
113112
ObjectProvider<ConnectionNameStrategy> connectionNameStrategy) {
114-
CachingConnectionFactoryConfigurer configurer = new CachingConnectionFactoryConfigurer();
115-
configurer.setRabbitProperties(rabbitProperties);
113+
CachingConnectionFactoryConfigurer configurer = new CachingConnectionFactoryConfigurer(rabbitProperties);
116114
configurer.setConnectionNameStrategy(connectionNameStrategy.getIfUnique());
117115
return configurer;
118116
}

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

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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+
117
package org.springframework.boot.autoconfigure.amqp;
218

319
import java.time.Duration;
@@ -18,42 +34,23 @@
1834
*/
1935
public class RabbitConnectionFactoryBeanConfigurer {
2036

21-
private RabbitProperties rabbitProperties;
37+
private final RabbitProperties rabbitProperties;
2238

23-
private ResourceLoader resourceLoader;
39+
private final ResourceLoader resourceLoader;
2440

2541
private CredentialsProvider credentialsProvider;
2642

2743
private CredentialsRefreshService credentialsRefreshService;
2844

29-
public RabbitProperties getRabbitProperties() {
30-
return rabbitProperties;
31-
}
32-
33-
public void setRabbitProperties(RabbitProperties rabbitProperties) {
34-
this.rabbitProperties = rabbitProperties;
35-
}
36-
37-
public ResourceLoader getResourceLoader() {
38-
return resourceLoader;
39-
}
40-
41-
public void setResourceLoader(ResourceLoader resourceLoader) {
45+
public RabbitConnectionFactoryBeanConfigurer(ResourceLoader resourceLoader, RabbitProperties properties) {
4246
this.resourceLoader = resourceLoader;
43-
}
44-
45-
public CredentialsProvider getCredentialsProvider() {
46-
return credentialsProvider;
47+
this.rabbitProperties = properties;
4748
}
4849

4950
public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
5051
this.credentialsProvider = credentialsProvider;
5152
}
5253

53-
public CredentialsRefreshService getCredentialsRefreshService() {
54-
return credentialsRefreshService;
55-
}
56-
5754
public void setCredentialsRefreshService(CredentialsRefreshService credentialsRefreshService) {
5855
this.credentialsRefreshService = credentialsRefreshService;
5956
}
@@ -67,7 +64,7 @@ public void setCredentialsRefreshService(CredentialsRefreshService credentialsRe
6764
*/
6865
public void configure(RabbitConnectionFactoryBean factory) {
6966
Assert.notNull(factory, "RabbitConnectionFactoryBean must not be null");
70-
factory.setResourceLoader(resourceLoader);
67+
factory.setResourceLoader(this.resourceLoader);
7168
PropertyMapper map = PropertyMapper.get();
7269
map.from(this.rabbitProperties::determineHost).whenNonNull().to(factory::setHost);
7370
map.from(this.rabbitProperties::determinePort).to(factory::setPort);
@@ -97,8 +94,8 @@ public void configure(RabbitConnectionFactoryBean factory) {
9794
.to(factory::setConnectionTimeout);
9895
map.from(this.rabbitProperties::getChannelRpcTimeout).whenNonNull().asInt(Duration::toMillis)
9996
.to(factory::setChannelRpcTimeout);
100-
map.from(credentialsProvider).whenNonNull().to(factory::setCredentialsProvider);
101-
map.from(credentialsRefreshService).whenNonNull().to(factory::setCredentialsRefreshService);
97+
map.from(this.credentialsProvider).whenNonNull().to(factory::setCredentialsProvider);
98+
map.from(this.credentialsRefreshService).whenNonNull().to(factory::setCredentialsRefreshService);
10299
}
103100

104101
}

0 commit comments

Comments
 (0)