2222import java .util .Collections ;
2323import java .util .HashMap ;
2424import java .util .Iterator ;
25- import java .util .LinkedHashMap ;
25+ import java .util .LinkedHashSet ;
2626import java .util .LinkedList ;
2727import java .util .List ;
2828import java .util .Map ;
29- import java .util .Map .Entry ;
3029import java .util .Properties ;
30+ import java .util .Set ;
3131import java .util .concurrent .TimeoutException ;
3232import java .util .concurrent .atomic .AtomicBoolean ;
3333import java .util .concurrent .atomic .AtomicReference ;
@@ -128,7 +128,7 @@ public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Applicat
128128
129129 private final ConnectionFactory connectionFactory ;
130130
131- private final Map < String , Declarable > manualDeclarables = Collections .synchronizedMap (new LinkedHashMap <>());
131+ private final Set < Declarable > manualDeclarables = Collections .synchronizedSet (new LinkedHashSet <>());
132132
133133 private String beanName ;
134134
@@ -229,7 +229,7 @@ public void declareExchange(final Exchange exchange) {
229229 this .rabbitTemplate .execute (channel -> {
230230 declareExchanges (channel , exchange );
231231 if (this .redeclareManualDeclarations ) {
232- this .manualDeclarables .put ( exchange . getName (), exchange );
232+ this .manualDeclarables .add ( exchange );
233233 }
234234 return null ;
235235 });
@@ -259,13 +259,16 @@ public boolean deleteExchange(final String exchangeName) {
259259 }
260260
261261 private void removeExchangeBindings (final String exchangeName ) {
262- this .manualDeclarables .remove (exchangeName );
263262 synchronized (this .manualDeclarables ) {
264- Iterator <Entry <String , Declarable >> iterator = this .manualDeclarables .entrySet ().iterator ();
263+ this .manualDeclarables .stream ()
264+ .filter (dec -> dec instanceof Exchange && ((Exchange ) dec ).getName ().equals (exchangeName ))
265+ .collect (Collectors .toSet ())
266+ .forEach (ex -> this .manualDeclarables .remove (ex ));
267+ Iterator <Declarable > iterator = this .manualDeclarables .iterator ();
265268 while (iterator .hasNext ()) {
266- Entry < String , Declarable > next = iterator .next ();
267- if (next . getValue () instanceof Binding ) {
268- Binding binding = (Binding ) next . getValue () ;
269+ Declarable next = iterator .next ();
270+ if (next instanceof Binding ) {
271+ Binding binding = (Binding ) next ;
269272 if ((!binding .isDestinationQueue () && binding .getDestination ().equals (exchangeName ))
270273 || binding .getExchange ().equals (exchangeName )) {
271274 iterator .remove ();
@@ -298,7 +301,7 @@ public String declareQueue(final Queue queue) {
298301 DeclareOk [] declared = declareQueues (channel , queue );
299302 String result = declared .length > 0 ? declared [0 ].getQueue () : null ;
300303 if (this .redeclareManualDeclarations ) {
301- this .manualDeclarables .put ( result , queue );
304+ this .manualDeclarables .add ( queue );
302305 }
303306 return result ;
304307 });
@@ -358,13 +361,16 @@ public void deleteQueue(final String queueName, final boolean unused, final bool
358361 }
359362
360363 private void removeQueueBindings (final String queueName ) {
361- this .manualDeclarables .remove (queueName );
362364 synchronized (this .manualDeclarables ) {
363- Iterator <Entry <String , Declarable >> iterator = this .manualDeclarables .entrySet ().iterator ();
365+ this .manualDeclarables .stream ()
366+ .filter (dec -> dec instanceof Queue && ((Queue ) dec ).getName ().equals (queueName ))
367+ .collect (Collectors .toSet ())
368+ .forEach (q -> this .manualDeclarables .remove (q ));
369+ Iterator <Declarable > iterator = this .manualDeclarables .iterator ();
364370 while (iterator .hasNext ()) {
365- Entry < String , Declarable > next = iterator .next ();
366- if (next . getValue () instanceof Binding ) {
367- Binding binding = (Binding ) next . getValue () ;
371+ Declarable next = iterator .next ();
372+ if (next instanceof Binding ) {
373+ Binding binding = (Binding ) next ;
368374 if (binding .isDestinationQueue () && binding .getDestination ().equals (queueName )) {
369375 iterator .remove ();
370376 }
@@ -405,7 +411,7 @@ public void declareBinding(final Binding binding) {
405411 this .rabbitTemplate .execute (channel -> {
406412 declareBindings (channel , binding );
407413 if (this .redeclareManualDeclarations ) {
408- this .manualDeclarables .put ( binding . toString (), binding );
414+ this .manualDeclarables .add ( binding );
409415 }
410416 return null ;
411417 });
@@ -707,7 +713,7 @@ private void redeclareManualDeclarables() {
707713 if (this .manualDeclarables .size () > 0 ) {
708714 synchronized (this .manualDeclarables ) {
709715 this .logger .debug ("Redeclaring manually declared Declarables" );
710- for (Declarable dec : this .manualDeclarables . values () ) {
716+ for (Declarable dec : this .manualDeclarables ) {
711717 if (dec instanceof Queue ) {
712718 declareQueue ((Queue ) dec );
713719 }
@@ -733,14 +739,27 @@ public void resetAllManualDeclarations() {
733739 this .manualDeclarables .clear ();
734740 }
735741
736- /**
737- * Return the manually declared AMQP objects.
738- * @return the manually declared AMQP objects.
739- * @since 2.4.13
740- */
741742 @ Override
743+ @ Deprecated
742744 public Map <String , Declarable > getManualDeclarables () {
743- return Collections .unmodifiableMap (this .manualDeclarables );
745+ Map <String , Declarable > declarables = new HashMap <>();
746+ this .manualDeclarables .forEach (declarable -> {
747+ if (declarable instanceof Exchange ) {
748+ declarables .put (((Exchange ) declarable ).getName (), declarable );
749+ }
750+ else if (declarable instanceof Queue ) {
751+ declarables .put (((Queue ) declarable ).getName (), declarable );
752+ }
753+ else if (declarable instanceof Binding ) {
754+ declarables .put (declarable .toString (), declarable );
755+ }
756+ });
757+ return declarables ;
758+ }
759+
760+ @ Override
761+ public Set <Declarable > getManualDeclarableSet () {
762+ return Collections .unmodifiableSet (this .manualDeclarables );
744763 }
745764
746765 private void processDeclarables (Collection <Exchange > contextExchanges , Collection <Queue > contextQueues ,
0 commit comments