22
22
import java .util .Collections ;
23
23
import java .util .HashMap ;
24
24
import java .util .Iterator ;
25
- import java .util .LinkedHashMap ;
25
+ import java .util .LinkedHashSet ;
26
26
import java .util .LinkedList ;
27
27
import java .util .List ;
28
28
import java .util .Map ;
29
- import java .util .Map .Entry ;
30
29
import java .util .Properties ;
30
+ import java .util .Set ;
31
31
import java .util .concurrent .TimeoutException ;
32
32
import java .util .concurrent .atomic .AtomicBoolean ;
33
33
import java .util .concurrent .atomic .AtomicReference ;
@@ -128,7 +128,7 @@ public class RabbitAdmin implements AmqpAdmin, ApplicationContextAware, Applicat
128
128
129
129
private final ConnectionFactory connectionFactory ;
130
130
131
- private final Map < String , Declarable > manualDeclarables = Collections .synchronizedMap (new LinkedHashMap <>());
131
+ private final Set < Declarable > manualDeclarables = Collections .synchronizedSet (new LinkedHashSet <>());
132
132
133
133
private String beanName ;
134
134
@@ -229,7 +229,7 @@ public void declareExchange(final Exchange exchange) {
229
229
this .rabbitTemplate .execute (channel -> {
230
230
declareExchanges (channel , exchange );
231
231
if (this .redeclareManualDeclarations ) {
232
- this .manualDeclarables .put ( exchange . getName (), exchange );
232
+ this .manualDeclarables .add ( exchange );
233
233
}
234
234
return null ;
235
235
});
@@ -259,13 +259,16 @@ public boolean deleteExchange(final String exchangeName) {
259
259
}
260
260
261
261
private void removeExchangeBindings (final String exchangeName ) {
262
- this .manualDeclarables .remove (exchangeName );
263
262
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 ();
265
268
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 ;
269
272
if ((!binding .isDestinationQueue () && binding .getDestination ().equals (exchangeName ))
270
273
|| binding .getExchange ().equals (exchangeName )) {
271
274
iterator .remove ();
@@ -298,7 +301,7 @@ public String declareQueue(final Queue queue) {
298
301
DeclareOk [] declared = declareQueues (channel , queue );
299
302
String result = declared .length > 0 ? declared [0 ].getQueue () : null ;
300
303
if (this .redeclareManualDeclarations ) {
301
- this .manualDeclarables .put ( result , queue );
304
+ this .manualDeclarables .add ( queue );
302
305
}
303
306
return result ;
304
307
});
@@ -358,13 +361,16 @@ public void deleteQueue(final String queueName, final boolean unused, final bool
358
361
}
359
362
360
363
private void removeQueueBindings (final String queueName ) {
361
- this .manualDeclarables .remove (queueName );
362
364
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 ();
364
370
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 ;
368
374
if (binding .isDestinationQueue () && binding .getDestination ().equals (queueName )) {
369
375
iterator .remove ();
370
376
}
@@ -405,7 +411,7 @@ public void declareBinding(final Binding binding) {
405
411
this .rabbitTemplate .execute (channel -> {
406
412
declareBindings (channel , binding );
407
413
if (this .redeclareManualDeclarations ) {
408
- this .manualDeclarables .put ( binding . toString (), binding );
414
+ this .manualDeclarables .add ( binding );
409
415
}
410
416
return null ;
411
417
});
@@ -707,7 +713,7 @@ private void redeclareManualDeclarables() {
707
713
if (this .manualDeclarables .size () > 0 ) {
708
714
synchronized (this .manualDeclarables ) {
709
715
this .logger .debug ("Redeclaring manually declared Declarables" );
710
- for (Declarable dec : this .manualDeclarables . values () ) {
716
+ for (Declarable dec : this .manualDeclarables ) {
711
717
if (dec instanceof Queue ) {
712
718
declareQueue ((Queue ) dec );
713
719
}
@@ -733,14 +739,27 @@ public void resetAllManualDeclarations() {
733
739
this .manualDeclarables .clear ();
734
740
}
735
741
736
- /**
737
- * Return the manually declared AMQP objects.
738
- * @return the manually declared AMQP objects.
739
- * @since 2.4.13
740
- */
741
742
@ Override
743
+ @ Deprecated
742
744
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 );
744
763
}
745
764
746
765
private void processDeclarables (Collection <Exchange > contextExchanges , Collection <Queue > contextQueues ,
0 commit comments