Skip to content

Commit

Permalink
GH-734: Option to suppress declaring Collections
Browse files Browse the repository at this point in the history
Fixes #734

Add `declareCollections` flag to admin (default false).

(cherry picked from commit 5423233)
  • Loading branch information
garyrussell authored and artembilan committed Mar 30, 2018
1 parent d6a5a75 commit 539af7f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -110,6 +111,8 @@ public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Applicat

private ApplicationEventPublisher applicationEventPublisher;

private boolean declareCollections = true;

private volatile DeclarationExceptionEvent lastDeclarationExceptionEvent;

public RabbitAdmin(ConnectionFactory connectionFactory) {
Expand All @@ -136,6 +139,17 @@ public void setIgnoreDeclarationExceptions(boolean ignoreDeclarationExceptions)
this.ignoreDeclarationExceptions = ignoreDeclarationExceptions;
}

/**
* Set to false to disable declaring collections of {@link Declarable}.
* Since the admin has to iterate over all Collection beans, this may
* cause undesirable side-effects in some cases. Default true.
* @param declareCollections set to false to prevent declarations of collections.
* @since 1.7.7
*/
public void setDeclareCollections(boolean declareCollections) {
this.declareCollections = declareCollections;
}

/**
* @return the last {@link DeclarationExceptionEvent} that was detected in this admin.
*
Expand Down Expand Up @@ -438,6 +452,7 @@ public void onClose(Connection connection) {
* Declares all the exchanges, queues and bindings in the enclosing application context, if any. It should be safe
* (but unnecessary) to call this method more than once.
*/
@Override
public void initialize() {

if (this.applicationContext == null) {
Expand All @@ -454,8 +469,9 @@ public void initialize() {
this.applicationContext.getBeansOfType(Binding.class).values());

@SuppressWarnings("rawtypes")
Collection<Collection> collections = this.applicationContext.getBeansOfType(Collection.class, false, false)
.values();
Collection<Collection> collections = this.declareCollections
? this.applicationContext.getBeansOfType(Collection.class, false, false).values()
: Collections.emptyList();
for (Collection<?> collection : collections) {
if (collection.size() > 0 && collection.iterator().next() instanceof Declarable) {
for (Object declarable : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ public void testMultiEntities() {
ctx.close();
}

@Test
public void testMultiEntitiesSuppressed() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(Config1.class);
RabbitAdmin admin = ctx.getBean(RabbitAdmin.class);
assertNotNull(admin.getQueueProperties("q1"));
assertNull(admin.getQueueProperties("q2"));
assertNull(admin.getQueueProperties("q3"));
assertNull(admin.getQueueProperties("q4"));
admin.deleteQueue("q1");
admin.deleteExchange("e1");
ctx.close();
}

@Test
public void testAvoidHangAMQP_508() {
CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
Expand Down Expand Up @@ -376,6 +389,18 @@ public List<Declarable> ds() {

}

@Configuration
public static class Config1 extends Config {

@Override
public RabbitAdmin admin(ConnectionFactory cf) {
RabbitAdmin admin = super.admin(cf);
admin.setDeclareCollections(false);
return admin;
}

}

private static final class EventPublisher implements ApplicationEventPublisher {

private final List<DeclarationExceptionEvent> events;
Expand Down
2 changes: 2 additions & 0 deletions src/reference/asciidoc/amqp.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3309,6 +3309,8 @@ public static class Config {
}
-----

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

[[conditional-declaration]]
===== Conditional Declaration
Expand Down

0 comments on commit 539af7f

Please sign in to comment.