Skip to content

Commit 80fd0fd

Browse files
garyrussellartembilan
authored andcommitted
GH-734: Option to suppress declaring Collections
Fixes #734 Add `declareCollections` flag to admin (default false). (cherry picked from commit 5423233)
1 parent 8e9fee7 commit 80fd0fd

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitAdmin.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
22+
import java.util.Collections;
2223
import java.util.HashMap;
2324
import java.util.LinkedList;
2425
import java.util.List;
@@ -109,6 +110,8 @@ public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Applicat
109110

110111
private ApplicationEventPublisher applicationEventPublisher;
111112

113+
private boolean declareCollections = true;
114+
112115
private volatile DeclarationExceptionEvent lastDeclarationExceptionEvent;
113116

114117
/**
@@ -154,6 +157,17 @@ public void setIgnoreDeclarationExceptions(boolean ignoreDeclarationExceptions)
154157
this.ignoreDeclarationExceptions = ignoreDeclarationExceptions;
155158
}
156159

160+
/**
161+
* Set to false to disable declaring collections of {@link Declarable}.
162+
* Since the admin has to iterate over all Collection beans, this may
163+
* cause undesirable side-effects in some cases. Default true.
164+
* @param declareCollections set to false to prevent declarations of collections.
165+
* @since 1.7.7
166+
*/
167+
public void setDeclareCollections(boolean declareCollections) {
168+
this.declareCollections = declareCollections;
169+
}
170+
157171
/**
158172
* @return the last {@link DeclarationExceptionEvent} that was detected in this admin.
159173
*
@@ -416,6 +430,7 @@ public void afterPropertiesSet() {
416430
* Declares all the exchanges, queues and bindings in the enclosing application context, if any. It should be safe
417431
* (but unnecessary) to call this method more than once.
418432
*/
433+
@Override
419434
public void initialize() {
420435

421436
if (this.applicationContext == null) {
@@ -432,8 +447,9 @@ public void initialize() {
432447
this.applicationContext.getBeansOfType(Binding.class).values());
433448

434449
@SuppressWarnings("rawtypes")
435-
Collection<Collection> collections = this.applicationContext.getBeansOfType(Collection.class, false, false)
436-
.values();
450+
Collection<Collection> collections = this.declareCollections
451+
? this.applicationContext.getBeansOfType(Collection.class, false, false).values()
452+
: Collections.emptyList();
437453
for (Collection<?> collection : collections) {
438454
if (collection.size() > 0 && collection.iterator().next() instanceof Declarable) {
439455
for (Object declarable : collection) {

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,19 @@ public void testMultiEntities() {
264264
ctx.close();
265265
}
266266

267+
@Test
268+
public void testMultiEntitiesSuppressed() {
269+
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(Config1.class);
270+
RabbitAdmin admin = ctx.getBean(RabbitAdmin.class);
271+
assertNotNull(admin.getQueueProperties("q1"));
272+
assertNull(admin.getQueueProperties("q2"));
273+
assertNull(admin.getQueueProperties("q3"));
274+
assertNull(admin.getQueueProperties("q4"));
275+
admin.deleteQueue("q1");
276+
admin.deleteExchange("e1");
277+
ctx.close();
278+
}
279+
267280
@Test
268281
public void testAvoidHangAMQP_508() {
269282
CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
@@ -417,6 +430,18 @@ public List<Declarable> ds() {
417430

418431
}
419432

433+
@Configuration
434+
public static class Config1 extends Config {
435+
436+
@Override
437+
public RabbitAdmin admin(ConnectionFactory cf) {
438+
RabbitAdmin admin = super.admin(cf);
439+
admin.setDeclareCollections(false);
440+
return admin;
441+
}
442+
443+
}
444+
420445
private static final class EventPublisher implements ApplicationEventPublisher {
421446

422447
private final List<DeclarationExceptionEvent> events;

src/reference/asciidoc/amqp.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,6 +3719,8 @@ public static class Config {
37193719
}
37203720
-----
37213721

3722+
IMPORTANT: This feature can cause undesirable side effects in some cases, because the admin has to iterate over all `Collection<?>` beans.
3723+
Starting with _versions 1.7.7, 2.0.4_, this feature can be disabled by setting the admin property `declareCollections` to `false`.
37223724

37233725
[[conditional-declaration]]
37243726
===== Conditional Declaration

0 commit comments

Comments
 (0)