Skip to content

Commit 61f535b

Browse files
EldarErelgaryrussell
authored andcommitted
GH-2428: Manual Declarations Recovery
fix: #2428 - recovery manual declarables without application context (#2429) * fix: recovery manual declaration without application context * test: recovery manual declaration without application context
1 parent b2ed57d commit 61f535b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ public void afterPropertiesSet() {
642642
@Override // NOSONAR complexity
643643
public void initialize() {
644644

645+
redeclareManualDeclarables();
646+
645647
if (this.applicationContext == null) {
646648
this.logger.debug("no ApplicationContext has been set, cannot auto-declare Exchanges, Queues, and Bindings");
647649
return;
@@ -694,6 +696,14 @@ public void initialize() {
694696
declareBindings(channel, bindings.toArray(new Binding[bindings.size()]));
695697
return null;
696698
});
699+
this.logger.debug("Declarations finished");
700+
701+
}
702+
703+
/**
704+
* Process manual declarables.
705+
*/
706+
private void redeclareManualDeclarables() {
697707
if (this.manualDeclarables.size() > 0) {
698708
synchronized (this.manualDeclarables) {
699709
this.logger.debug("Redeclaring manually declared Declarables");
@@ -710,7 +720,6 @@ else if (dec instanceof Exchange) {
710720
}
711721
}
712722
}
713-
this.logger.debug("Declarations finished");
714723

715724
}
716725

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,52 @@ void manualDeclarations() {
445445
cf.destroy();
446446
}
447447

448+
@Test
449+
void manualDeclarationsWithoutApplicationContext() {
450+
CachingConnectionFactory cf = new CachingConnectionFactory(
451+
RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
452+
RabbitAdmin admin = new RabbitAdmin(cf);
453+
admin.setRedeclareManualDeclarations(true);
454+
Map<?, ?> declarables = TestUtils.getPropertyValue(admin, "manualDeclarables", Map.class);
455+
assertThat(declarables).hasSize(0);
456+
RabbitTemplate template = new RabbitTemplate(cf);
457+
// manual declarations
458+
admin.declareQueue(new Queue("test1", false, true, true));
459+
admin.declareQueue(new Queue("test2", false, true, true));
460+
admin.declareExchange(new DirectExchange("ex1", false, true));
461+
admin.declareBinding(new Binding("test1", DestinationType.QUEUE, "ex1", "test", null));
462+
admin.deleteQueue("test2");
463+
template.execute(chan -> chan.queueDelete("test1"));
464+
cf.resetConnection();
465+
admin.initialize();
466+
assertThat(admin.getQueueProperties("test1")).isNotNull();
467+
assertThat(admin.getQueueProperties("test2")).isNull();
468+
assertThat(declarables).hasSize(3);
469+
// verify the exchange and binding were recovered too
470+
template.convertAndSend("ex1", "test", "foo");
471+
Object received = template.receiveAndConvert("test1", 5000);
472+
assertThat(received).isEqualTo("foo");
473+
admin.resetAllManualDeclarations();
474+
assertThat(declarables).hasSize(0);
475+
cf.resetConnection();
476+
admin.initialize();
477+
assertThat(admin.getQueueProperties("test1")).isNull();
478+
admin.declareQueue(new Queue("test1", false, true, true));
479+
admin.declareExchange(new DirectExchange("ex1", false, true));
480+
admin.declareBinding(new Binding("test1", DestinationType.QUEUE, "ex1", "test", null));
481+
admin.declareExchange(new DirectExchange("ex2", false, true));
482+
admin.declareBinding(new Binding("test1", DestinationType.QUEUE, "ex2", "test", null));
483+
admin.declareBinding(new Binding("ex1", DestinationType.EXCHANGE, "ex2", "ex1", null));
484+
assertThat(declarables).hasSize(6);
485+
admin.deleteExchange("ex2");
486+
assertThat(declarables).hasSize(3);
487+
admin.deleteQueue("test1");
488+
assertThat(declarables).hasSize(1);
489+
admin.deleteExchange("ex1");
490+
assertThat(declarables).hasSize(0);
491+
cf.destroy();
492+
}
493+
448494
@Configuration
449495
public static class Config {
450496

0 commit comments

Comments
 (0)