3232
3333import com .rabbitmq .client .AMQP ;
3434import com .rabbitmq .client .Address ;
35+ import com .rabbitmq .client .AlreadyClosedException ;
3536import com .rabbitmq .client .Channel ;
3637import com .rabbitmq .client .Command ;
3738import com .rabbitmq .client .Connection ;
5960 * int ticket = ch1.accessRequest(realmName);
6061 * </pre>
6162 */
62- public class AMQConnection implements Connection {
63+ public class AMQConnection extends ShutdownNotifierComponent implements Connection {
6364 /** Timeout used while waiting for AMQP handshaking to complete (milliseconds) */
6465 public static final int HANDSHAKE_TIMEOUT = 10000 ;
6566
@@ -88,19 +89,12 @@ public class AMQConnection implements Connection {
8889 /** Flag controlling the main driver loop's termination */
8990 public volatile boolean _running = false ;
9091
91- /**
92- * When this value is null, the connection is in an "open"
93- * state. When non-null, the connection is in "closed" state, and
94- * this value indicates the circumstances of the shutdown.
95- */
96- public volatile ShutdownSignalException _shutdownCause = null ;
97-
9892 /** Maximum frame length, or zero if no limit is set */
9993 public int _frameMax ;
10094
10195 /** Handler for (otherwise-unhandled) exceptions that crop up in the mainloop. */
10296 public final ExceptionHandler _exceptionHandler ;
103-
97+
10498 /**
10599 * Protected API - respond, in the driver thread, to a ShutdownSignal.
106100 * @param channelNumber the number of the channel to disconnect
@@ -109,15 +103,11 @@ public final void disconnectChannel(int channelNumber) {
109103 _channelManager .disconnectChannel (channelNumber );
110104 }
111105
112- public boolean isOpen () {
113- return _shutdownCause == null ;
114- }
115-
116106 public void ensureIsOpen ()
117- throws IllegalStateException
107+ throws AlreadyClosedException
118108 {
119109 if (!isOpen ()) {
120- throw new IllegalStateException ("Attempt to use closed connection" );
110+ throw new AlreadyClosedException ("Attempt to use closed connection" );
121111 }
122112 }
123113
@@ -340,15 +330,13 @@ public Address[] open(final ConnectionParameters params, boolean insist)
340330 _frameHandler .setTimeout (HANDSHAKE_TIMEOUT );
341331 _frameHandler .sendHeader ();
342332
343- if (!isOpen ()) {
344- // See bug 17389. The MainLoop could have shut down already in
345- // which case we don't want to wait forever for a reply.
346-
347- // There is no race if the MainLoop shuts down after enqueuing
348- // the RPC because if that happens the channel will correctly
349- // pass the exception into RPC, waking it up.
350- throw _shutdownCause ;
351- }
333+ // See bug 17389. The MainLoop could have shut down already in
334+ // which case we don't want to wait forever for a reply.
335+
336+ // There is no race if the MainLoop shuts down after enqueuing
337+ // the RPC because if that happens the channel will correctly
338+ // pass the exception into RPC, waking it up.
339+ ensureIsOpen ();
352340
353341 AMQP .Connection .Start connStart =
354342 (AMQP .Connection .Start ) connStartBlocker .getReply ().getMethod ();
@@ -561,6 +549,7 @@ public void handleConnectionClose(Command closeCommand) {
561549 Utility .emptyStatement ();
562550 }
563551 shutdown (closeCommand , false , null );
552+ notifyListeners ();
564553 }
565554
566555 /**
@@ -616,6 +605,7 @@ public void close(int closeCode,
616605 } finally {
617606 _running = false ;
618607 }
608+ notifyListeners ();
619609 }
620610
621611 @ Override public String toString () {
0 commit comments