Skip to content

Commit 20943cc

Browse files
garyrussellartembilan
authored andcommitted
Add ContainerCustomizer
- deprecate containerConfigurer `Consumer<C>` to more easily facilitate Boot auto configuration. * Add docs.
1 parent e707f51 commit 20943cc

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-9
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/AbstractRabbitListenerContainerFactory.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public abstract class AbstractRabbitListenerContainerFactory<C extends AbstractM
119119

120120
private RecoveryCallback<?> recoveryCallback;
121121

122-
private Consumer<C> containerConfigurer;
122+
private ContainerCustomizer<C> containerCustomizer;
123123

124124
private boolean batchListener;
125125

@@ -351,9 +351,21 @@ public void setReplyRecoveryCallback(RecoveryCallback<?> recoveryCallback) {
351351
* exposed by this container factory.
352352
* @param configurer the configurer;
353353
* @since 2.1.1
354+
* @deprecated in favor of {@link #setContainerCustomizer(ContainerCustomizer)}.
354355
*/
356+
@Deprecated
355357
public void setContainerConfigurer(Consumer<C> configurer) {
356-
this.containerConfigurer = configurer;
358+
this.containerCustomizer = container -> configurer.accept(container);
359+
}
360+
361+
/**
362+
* Set a {@link ContainerCustomizer} that is invoked after a container is created and
363+
* configured to enable further customization of the container.
364+
* @param containerCustomizer the customizer.
365+
* @since 2.2.2
366+
*/
367+
public void setContainerCustomizer(ContainerCustomizer<C> containerCustomizer) {
368+
this.containerCustomizer = containerCustomizer;
357369
}
358370

359371
/**
@@ -447,8 +459,8 @@ public C createListenerContainer(RabbitListenerEndpoint endpoint) {
447459
}
448460
initializeContainer(instance, endpoint);
449461

450-
if (this.containerConfigurer != null) {
451-
this.containerConfigurer.accept(instance);
462+
if (this.containerCustomizer != null) {
463+
this.containerCustomizer.configure(instance);
452464
}
453465

454466
return instance;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2019 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+
17+
package org.springframework.amqp.rabbit.config;
18+
19+
import org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
20+
21+
/**
22+
* Called by the container factory after the container is created and configured.
23+
*
24+
* @param <C> the container type.
25+
*
26+
* @author Gary Russell
27+
* @since 2.2.2
28+
*
29+
*/
30+
@FunctionalInterface
31+
public interface ContainerCustomizer<C extends AbstractMessageListenerContainer> {
32+
33+
/**
34+
* Configure the container.
35+
* @param container the container.
36+
*/
37+
void configure(C container);
38+
39+
}

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/ConsumerBatchingTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
183183
factory.setBatchListener(true);
184184
factory.setConsumerBatchEnabled(true);
185185
factory.setBatchSize(4);
186-
factory.setContainerConfigurer(
186+
factory.setContainerCustomizer(
187187
container -> container.setMicrometerTags(Collections.singletonMap("extraTag", "foo")));
188188
return factory;
189189
}

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ public SimpleRabbitListenerContainerFactory simpleJsonListenerContainerFactory()
15541554
messageConverter.getJavaTypeMapper().addTrustedPackages("*");
15551555
factory.setMessageConverter(messageConverter);
15561556
factory.setReceiveTimeout(10L);
1557-
factory.setContainerConfigurer(
1557+
factory.setContainerCustomizer(
15581558
container -> container.setMicrometerTags(Collections.singletonMap("extraTag", "foo")));
15591559
return factory;
15601560
}
@@ -1915,7 +1915,7 @@ public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
19151915
factory.setConnectionFactory(rabbitConnectionFactory());
19161916
factory.setMessageConverter(jsonConverter());
19171917
factory.setReceiveTimeout(10L);
1918-
factory.setContainerConfigurer(container -> container.setMicrometerEnabled(false));
1918+
factory.setContainerCustomizer(container -> container.setMicrometerEnabled(false));
19191919
return factory;
19201920
}
19211921

@@ -2004,7 +2004,7 @@ public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
20042004
factory.setConnectionFactory(rabbitConnectionFactory());
20052005
factory.setMessageConverter(xmlConverter());
20062006
factory.setReceiveTimeout(10L);
2007-
factory.setContainerConfigurer(container -> container.setMicrometerEnabled(false));
2007+
factory.setContainerCustomizer(container -> container.setMicrometerEnabled(false));
20082008
return factory;
20092009
}
20102010

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/RabbitListenerContainerFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void createContainerFullConfig() {
103103
this.factory.setRecoveryBackOff(recoveryBackOff);
104104
this.factory.setMissingQueuesFatal(true);
105105
this.factory.setAfterReceivePostProcessors(afterReceivePostProcessor);
106-
this.factory.setContainerConfigurer(c -> c.setShutdownTimeout(10_000));
106+
this.factory.setContainerCustomizer(c -> c.setShutdownTimeout(10_000));
107107

108108
assertThat(this.factory.getAdviceChain()).isEqualTo(new Advice[]{advice});
109109

src/reference/asciidoc/amqp.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,7 @@ public class AppConfig {
21892189
factory.setConnectionFactory(connectionFactory());
21902190
factory.setConcurrentConsumers(3);
21912191
factory.setMaxConcurrentConsumers(10);
2192+
factory.setContainerCustomizer(container -> /* customize the container */);
21922193
return factory;
21932194
}
21942195
}
@@ -2200,6 +2201,9 @@ It creates `DirectMessageListenerContainer` instances.
22002201

22012202
NOTE: For information to help you choose between `SimpleRabbitListenerContainerFactory` and `DirectRabbitListenerContainerFactory`, see <<choose-container>>.
22022203

2204+
Starting wih version 2.2.2, you can provide a `ContainerCustomizer` implementation (as shown above).
2205+
This can be used to further configure the container after it has been created and configured; you can use this, for example, to set properties that are not exposed by the container factory.
2206+
22032207
By default, the infrastructure looks for a bean named `rabbitListenerContainerFactory` as the source for the factory to use to create message listener containers.
22042208
In this case, and ignoring the RabbitMQ infrastructure setup, the `processOrder` method can be invoked with a core poll size of three threads and a maximum pool size of ten threads.
22052209

0 commit comments

Comments
 (0)