25
25
import static org .junit .Assert .assertThat ;
26
26
import static org .junit .Assert .assertTrue ;
27
27
import static org .junit .Assert .fail ;
28
- import static org .mockito .BDDMockito .willReturn ;
29
28
import static org .mockito .Matchers .anyString ;
30
29
import static org .mockito .Mockito .mock ;
31
30
import static org .mockito .Mockito .never ;
32
31
import static org .mockito .Mockito .spy ;
33
32
import static org .mockito .Mockito .verify ;
34
33
35
- import java .io .IOException ;
36
34
import java .net .ServerSocket ;
37
35
import java .net .Socket ;
38
36
import java .util .ArrayList ;
58
56
import org .junit .rules .ExpectedException ;
59
57
60
58
import org .springframework .amqp .AmqpAuthenticationException ;
61
- import org .springframework .amqp .AmqpException ;
62
59
import org .springframework .amqp .AmqpIOException ;
63
60
import org .springframework .amqp .AmqpResourceNotAvailableException ;
64
61
import org .springframework .amqp .AmqpTimeoutException ;
76
73
77
74
import com .rabbitmq .client .Channel ;
78
75
import com .rabbitmq .client .DefaultConsumer ;
79
- import com .rabbitmq .client .Recoverable ;
80
- import com .rabbitmq .client .RecoveryListener ;
81
76
import com .rabbitmq .client .ShutdownListener ;
82
77
import com .rabbitmq .client .ShutdownSignalException ;
83
- import com .rabbitmq .client .impl .recovery .AutorecoveringChannel ;
84
- import com .rabbitmq .client .impl .recovery .AutorecoveringConnection ;
85
78
86
79
/**
87
80
* @author Dave Syer
@@ -171,13 +164,15 @@ public void testCachedConnectionsChannelLimit() throws Exception {
171
164
channels .add (connections .get (0 ).createChannel (false ));
172
165
fail ("Exception expected" );
173
166
}
174
- catch (AmqpTimeoutException e ) { }
167
+ catch (AmqpTimeoutException e ) {
168
+ }
175
169
channels .add (connections .get (1 ).createChannel (false ));
176
170
try {
177
171
channels .add (connections .get (1 ).createChannel (false ));
178
172
fail ("Exception expected" );
179
173
}
180
- catch (AmqpTimeoutException e ) { }
174
+ catch (AmqpTimeoutException e ) {
175
+ }
181
176
channels .get (0 ).close ();
182
177
channels .get (1 ).close ();
183
178
channels .add (connections .get (0 ).createChannel (false ));
@@ -324,6 +319,7 @@ public void testMixTransactionalAndNonTransactional() throws Exception {
324
319
exception .expect (AmqpIOException .class );
325
320
326
321
template2 .execute (new ChannelCallback <Void >() {
322
+
327
323
@ Override
328
324
public Void doInRabbit (Channel channel ) throws Exception {
329
325
// Should be an exception because the channel is not transactional
@@ -346,9 +342,11 @@ public void testHardErrorAndReconnectNoAuto() throws Exception {
346
342
final CountDownLatch latch = new CountDownLatch (1 );
347
343
try {
348
344
template .execute (new ChannelCallback <Object >() {
345
+
349
346
@ Override
350
347
public Object doInRabbit (Channel channel ) throws Exception {
351
348
channel .getConnection ().addShutdownListener (new ShutdownListener () {
349
+
352
350
@ Override
353
351
public void shutdownCompleted (ShutdownSignalException cause ) {
354
352
logger .info ("Error" , cause );
@@ -377,108 +375,6 @@ public void shutdownCompleted(ShutdownSignalException cause) {
377
375
assertEquals (null , result );
378
376
}
379
377
380
- @ Test
381
- public void testHardErrorAndReconnectAuto () throws Exception {
382
- this .connectionFactory .getRabbitConnectionFactory ().setAutomaticRecoveryEnabled (true );
383
- Log cfLogger = spyOnLogger (this .connectionFactory );
384
- willReturn (true ).given (cfLogger ).isDebugEnabled ();
385
- RabbitTemplate template = new RabbitTemplate (connectionFactory );
386
- RabbitAdmin admin = new RabbitAdmin (connectionFactory );
387
- Queue queue = new Queue (CF_INTEGRATION_TEST_QUEUE );
388
- admin .declareQueue (queue );
389
- final String route = queue .getName ();
390
-
391
- final CountDownLatch latch = new CountDownLatch (1 );
392
- final CountDownLatch recoveryLatch = new CountDownLatch (1 );
393
- final RecoveryListener channelRecoveryListener = new RecoveryListener () {
394
-
395
- @ Override
396
- public void handleRecoveryStarted (Recoverable recoverable ) {
397
- if (logger .isDebugEnabled ()) {
398
- logger .debug ("Channel recovery started: " + asString (recoverable ));
399
- }
400
- }
401
-
402
- @ Override
403
- public void handleRecovery (Recoverable recoverable ) {
404
- try {
405
- ((Channel ) recoverable ).basicCancel ("testHardErrorAndReconnect" );
406
- }
407
- catch (IOException e ) {
408
- }
409
- if (logger .isDebugEnabled ()) {
410
- logger .debug ("Channel recovery complete: " + asString (recoverable ));
411
- }
412
- }
413
-
414
- private String asString (Recoverable recoverable ) {
415
- // TODO: https://github.com/rabbitmq/rabbitmq-java-client/issues/217
416
- return ((AutorecoveringChannel ) recoverable ).getDelegate ().toString ();
417
- }
418
-
419
- };
420
- final RecoveryListener connectionRecoveryListener = new RecoveryListener () {
421
-
422
- @ Override
423
- public void handleRecoveryStarted (Recoverable recoverable ) {
424
- if (logger .isDebugEnabled ()) {
425
- logger .debug ("Connection recovery started: " + recoverable );
426
- }
427
- }
428
-
429
- @ Override
430
- public void handleRecovery (Recoverable recoverable ) {
431
- if (logger .isDebugEnabled ()) {
432
- logger .debug ("Connection recovery complete: " + recoverable );
433
- }
434
- recoveryLatch .countDown ();
435
- }
436
-
437
- };
438
- Object connection = ((ConnectionProxy ) this .connectionFactory .createConnection ()).getTargetConnection ();
439
- connection = TestUtils .getPropertyValue (connection , "delegate" );
440
- if (connection instanceof AutorecoveringConnection ) {
441
- ((AutorecoveringConnection ) connection ).addRecoveryListener (connectionRecoveryListener );
442
- }
443
- try {
444
- template .execute (channel -> {
445
- channel .getConnection ().addShutdownListener (cause -> {
446
- logger .info ("Error" , cause );
447
- latch .countDown ();
448
- // This will be thrown on the Connection thread just before it dies, so basically ignored
449
- throw new RuntimeException (cause );
450
- });
451
- Channel targetChannel = ((ChannelProxy ) channel ).getTargetChannel ();
452
- if (targetChannel instanceof AutorecoveringChannel ) {
453
- ((AutorecoveringChannel ) targetChannel ).addRecoveryListener (channelRecoveryListener );
454
- }
455
- else {
456
- fail ("Expected an AutorecoveringChannel" );
457
- }
458
- String tag = channel .basicConsume (route , false , "testHardErrorAndReconnect" ,
459
- new DefaultConsumer (channel ));
460
- // Consume twice with the same tag is a hard error (connection will be reset)
461
- String result = channel .basicConsume (route , false , tag , new DefaultConsumer (channel ));
462
- fail ("Expected IOException, got: " + result );
463
- return null ;
464
- });
465
- fail ("Expected AmqpIOException" );
466
- }
467
- catch (AmqpException e ) {
468
- // expected
469
- }
470
- assertTrue (recoveryLatch .await (10 , TimeUnit .SECONDS ));
471
- if (logger .isDebugEnabled ()) {
472
- logger .debug ("Resuming test after recovery complete" );
473
- }
474
- template .convertAndSend (route , "message" );
475
- assertTrue (latch .await (10 , TimeUnit .SECONDS ));
476
- String result = (String ) template .receiveAndConvert (route );
477
- assertEquals ("message" , result );
478
- result = (String ) template .receiveAndConvert (route );
479
- assertEquals (null , result );
480
- }
481
-
482
378
@ Test
483
379
public void testConnectionCloseLog () {
484
380
Log logger = spy (TestUtils .getPropertyValue (this .connectionFactory , "logger" , Log .class ));
@@ -539,7 +435,8 @@ public void hangOnClose() throws Exception {
539
435
socket .close ();
540
436
proxy .close ();
541
437
}
542
- catch (Exception ee ) { }
438
+ catch (Exception ee ) {
439
+ }
543
440
}
544
441
}
545
442
});
@@ -555,7 +452,8 @@ public void hangOnClose() throws Exception {
555
452
socket .close ();
556
453
proxy .close ();
557
454
}
558
- catch (Exception ee ) { }
455
+ catch (Exception ee ) {
456
+ }
559
457
}
560
458
}
561
459
socket .close ();
0 commit comments