36
36
import java .util .List ;
37
37
import java .util .Properties ;
38
38
import java .util .concurrent .CountDownLatch ;
39
+ import java .util .concurrent .ExecutorService ;
39
40
import java .util .concurrent .Executors ;
40
41
import java .util .concurrent .TimeUnit ;
41
42
import java .util .concurrent .atomic .AtomicReference ;
@@ -151,6 +152,36 @@ public void testSimple() throws Exception {
151
152
assertEquals (0 , TestUtils .getPropertyValue (container , "consumersByQueue" , MultiValueMap .class ).size ());
152
153
template .stop ();
153
154
cf .destroy ();
155
+ executor .destroy ();
156
+ }
157
+
158
+ @ Test
159
+ public void testBadHost () throws InterruptedException {
160
+ CachingConnectionFactory cf = new CachingConnectionFactory ("this.host.does.not.exist" );
161
+ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ();
162
+ executor .setThreadNamePrefix ("client-" );
163
+ executor .afterPropertiesSet ();
164
+ cf .setExecutor (executor );
165
+ DirectMessageListenerContainer container = new DirectMessageListenerContainer (cf );
166
+ container .setQueueNames ("dummy" );
167
+ container .setConsumersPerQueue (2 );
168
+ container .setMessageListener (in -> {
169
+ });
170
+ container .setBeanName ("badHost" );
171
+ container .setConsumerTagStrategy (new Tag ());
172
+ CountDownLatch latch = new CountDownLatch (1 );
173
+ container .setApplicationEventPublisher (ev -> {
174
+ if (ev instanceof ListenerContainerConsumerFailedEvent ) {
175
+ latch .countDown ();
176
+ }
177
+ });
178
+ container .setRecoveryInterval (100 );
179
+ container .afterPropertiesSet ();
180
+ container .start ();
181
+ assertTrue (latch .await (10 , TimeUnit .SECONDS ));
182
+ container .stop ();
183
+ cf .destroy ();
184
+ executor .destroy ();
154
185
}
155
186
156
187
@ Test
@@ -222,6 +253,7 @@ public void testQueueManagement() throws Exception {
222
253
assertEquals (0 , TestUtils .getPropertyValue (container , "consumersByQueue" , MultiValueMap .class ).size ());
223
254
template .stop ();
224
255
cf .destroy ();
256
+ executor .destroy ();
225
257
}
226
258
227
259
@ Test
@@ -264,6 +296,7 @@ public void testQueueManagementQueueInstances() throws Exception {
264
296
assertEquals (0 , TestUtils .getPropertyValue (container , "consumersByQueue" , MultiValueMap .class ).size ());
265
297
template .stop ();
266
298
cf .destroy ();
299
+ executor .destroy ();
267
300
}
268
301
269
302
@ Test
@@ -306,6 +339,7 @@ public void testAddRemoveConsumers() throws Exception {
306
339
assertEquals (0 , TestUtils .getPropertyValue (container , "consumersByQueue" , MultiValueMap .class ).size ());
307
340
template .stop ();
308
341
cf .destroy ();
342
+ executor .destroy ();
309
343
}
310
344
311
345
@ Test
@@ -347,8 +381,8 @@ public void testErrorHandler() throws Exception {
347
381
.put ("x-dead-letter-routing-key" , DLQ1 )
348
382
.get ());
349
383
CachingConnectionFactory cf = new CachingConnectionFactory ("localhost" );
350
- RabbitAdmin admin = new RabbitAdmin (cf );
351
- admin .declareQueue (q1 );
384
+ RabbitAdmin rabbitAdmin = new RabbitAdmin (cf );
385
+ rabbitAdmin .declareQueue (q1 );
352
386
DirectMessageListenerContainer container = new DirectMessageListenerContainer (cf );
353
387
container .setQueueNames (Q1 );
354
388
container .setConsumersPerQueue (2 );
@@ -457,13 +491,15 @@ public void testCancelConsumerBeforeConsumeOk() throws Exception {
457
491
container .start ();
458
492
assertTrue (latch1 .await (10 , TimeUnit .SECONDS ));
459
493
Consumer consumer = consumerCaptor .getValue ();
460
- Executors .newSingleThreadExecutor ().execute (() -> {
494
+ ExecutorService exec = Executors .newSingleThreadExecutor ();
495
+ exec .execute (() -> {
461
496
container .stop ();
462
497
latch2 .countDown ();
463
498
});
464
499
assertTrue (latch2 .await (10 , TimeUnit .SECONDS ));
465
500
verify (channel ).basicCancel (tag ); // canceled properly even without consumeOk
466
501
consumer .handleCancelOk (tag );
502
+ exec .shutdownNow ();
467
503
}
468
504
469
505
@ Test
@@ -482,9 +518,9 @@ private void testRecoverDeletedQueueGuts(boolean autoDeclare) throws Exception {
482
518
if (autoDeclare ) {
483
519
GenericApplicationContext context = new GenericApplicationContext ();
484
520
context .getBeanFactory ().registerSingleton ("foo" , new Queue (Q1 ));
485
- RabbitAdmin admin = new RabbitAdmin (cf );
486
- admin .setApplicationContext (context );
487
- context .getBeanFactory ().registerSingleton ("admin" , admin );
521
+ RabbitAdmin rabbitAdmin = new RabbitAdmin (cf );
522
+ rabbitAdmin .setApplicationContext (context );
523
+ context .getBeanFactory ().registerSingleton ("admin" , rabbitAdmin );
488
524
context .refresh ();
489
525
container .setApplicationContext (context );
490
526
}
@@ -505,10 +541,10 @@ private void testRecoverDeletedQueueGuts(boolean autoDeclare) throws Exception {
505
541
assertTrue (consumersOnQueue (Q2 , 2 ));
506
542
assertTrue (activeConsumerCount (container , 2 ));
507
543
assertTrue (restartConsumerCount (container , 2 ));
508
- RabbitAdmin admin = new RabbitAdmin (cf );
544
+ RabbitAdmin rabbitAdmin = new RabbitAdmin (cf );
509
545
if (!autoDeclare ) {
510
546
Thread .sleep (2000 );
511
- admin .declareQueue (new Queue (Q1 ));
547
+ rabbitAdmin .declareQueue (new Queue (Q1 ));
512
548
}
513
549
assertTrue (consumersOnQueue (Q1 , 2 ));
514
550
assertTrue (consumersOnQueue (Q2 , 2 ));
0 commit comments