@@ -103,6 +103,9 @@ public class AMQConnection extends ShutdownNotifierComponent implements Connecti
103103 */
104104 public BlockingCell <Object > _appContinuation = new BlockingCell <Object >();
105105
106+ /** Flag indicating whether the client received Connection.Close message from the broker */
107+ public boolean _brokerInitiatedShutdown = false ;
108+
106109 /**
107110 * Protected API - respond, in the driver thread, to a ShutdownSignal.
108111 * @param channelNumber the number of the channel to disconnect
@@ -191,6 +194,7 @@ public AMQConnection(ConnectionParameters params,
191194 _missedHeartbeats = 0 ;
192195 _heartbeat = 0 ;
193196 _exceptionHandler = exceptionHandler ;
197+ _brokerInitiatedShutdown = false ;
194198
195199 new MainLoop (); // start the main loop going
196200
@@ -457,15 +461,15 @@ public MainLoop() {
457461 }
458462 }
459463 } catch (EOFException ex ) {
460- shutdown (ex , false , ex );
464+ if (!_brokerInitiatedShutdown )
465+ shutdown (ex , false , ex , true );
461466 } catch (Throwable ex ) {
462467 _exceptionHandler .handleUnexpectedConnectionDriverException (AMQConnection .this ,
463468 ex );
464- shutdown (ex , false , ex );
469+ shutdown (ex , false , ex , true );
465470 } finally {
466471 // Finally, shut down our underlying data connection.
467472 _frameHandler .close ();
468-
469473 _appContinuation .set (null );
470474 notifyListeners ();
471475 }
@@ -551,18 +555,22 @@ public boolean processControlCommand(Command c)
551555 }
552556
553557 public void handleConnectionClose (Command closeCommand ) {
554- shutdown (closeCommand , false , null );
558+ ShutdownSignalException sse = shutdown (closeCommand , false , null , false );
555559 try {
556560 _channel0 .quiescingTransmit (new AMQImpl .Connection .CloseOk ());
557561 } catch (IOException ioe ) {
558562 Utility .emptyStatement ();
559563 }
560- _heartbeat = 0 ; // Do not try to send heartbeats after CloseOk
561- new SocketCloseWait ();
564+ _heartbeat = 0 ; // Do not try to send heartbeats after CloseOk
565+ _brokerInitiatedShutdown = true ;
566+ new SocketCloseWait (sse );
562567 }
563568
564569 private class SocketCloseWait extends Thread {
565- public SocketCloseWait () {
570+ private ShutdownSignalException cause ;
571+
572+ public SocketCloseWait (ShutdownSignalException sse ) {
573+ cause = sse ;
566574 start ();
567575 }
568576
@@ -575,6 +583,7 @@ public SocketCloseWait() {
575583 _frameHandler .close ();
576584 } finally {
577585 _running = false ;
586+ _channel0 .notifyOutstandingRpc (cause );
578587 }
579588 }
580589 }
@@ -583,12 +592,14 @@ public SocketCloseWait() {
583592 * Protected API - causes all attached channels to terminate with
584593 * a ShutdownSignal built from the argument, and stops this
585594 * connection from accepting further work from the application.
595+ *
596+ * @return a shutdown signal built using the given arguments
586597 */
587- public void shutdown (Object reason ,
598+ public ShutdownSignalException shutdown (Object reason ,
588599 boolean initiatedByApplication ,
589- Throwable cause )
600+ Throwable cause ,
601+ boolean notifyRpc )
590602 {
591-
592603 ShutdownSignalException sse = new ShutdownSignalException (true ,initiatedByApplication ,
593604 reason , this );
594605 sse .initCause (cause );
@@ -600,15 +611,10 @@ public void shutdown(Object reason,
600611 } catch (AlreadyClosedException ace ) {
601612 if (initiatedByApplication )
602613 throw ace ;
603- } finally {
604- try {
605- _channel0 .processShutdownSignal (sse );
606- } catch (AlreadyClosedException ace ) {
607- if (initiatedByApplication )
608- throw ace ;
609- }
610614 }
615+ _channel0 .processShutdownSignal (sse , !initiatedByApplication , notifyRpc );
611616 _channelManager .handleSignal (sse );
617+ return sse ;
612618 }
613619
614620 /**
@@ -687,7 +693,7 @@ public void close(int closeCode,
687693 try {
688694 AMQImpl .Connection .Close reason =
689695 new AMQImpl .Connection .Close (closeCode , closeMessage , 0 , 0 );
690- shutdown (reason , initiatedByApplication , cause );
696+ shutdown (reason , initiatedByApplication , cause , true );
691697 AMQChannel .SimpleBlockingRpcContinuation k =
692698 new AMQChannel .SimpleBlockingRpcContinuation ();
693699 _channel0 .quiescingRpc (reason , k );
0 commit comments