4242import java .util .Collections ;
4343import java .util .HashMap ;
4444import java .util .Map ;
45- import java .util .concurrent .atomic .AtomicLong ;
4645import java .util .concurrent .TimeoutException ;
4746
4847
@@ -236,20 +235,7 @@ public void releaseChannelNumber() {
236235 // We're in normal running mode.
237236
238237 if (method instanceof Channel .Close ) {
239- releaseChannelNumber ();
240- ShutdownSignalException signal = new ShutdownSignalException (false ,
241- false ,
242- command ,
243- this );
244- synchronized (_channelMutex ) {
245- try {
246- processShutdownSignal (signal , true , false );
247- quiescingTransmit (new Channel .CloseOk ());
248- } finally {
249- notifyOutstandingRpc (signal );
250- }
251- }
252- notifyListeners ();
238+ asyncShutdown (command );
253239 return true ;
254240 } else if (method instanceof Basic .Deliver ) {
255241 Basic .Deliver m = (Basic .Deliver ) method ;
@@ -355,13 +341,11 @@ public void releaseChannelNumber() {
355341 return false ;
356342 }
357343 } else {
358- // We're in quiescing mode.
344+ // We're in quiescing mode == !isOpen()
359345
360346 if (method instanceof Channel .Close ) {
361- // We're already shutting down, so just send back an ok.
362- synchronized (_channelMutex ) {
363- quiescingTransmit (new Channel .CloseOk ());
364- }
347+ // We are already shutting down, but we cannot assume no Rpc is waiting.
348+ asyncShutdown (command );
365349 return true ;
366350 } else if (method instanceof Channel .CloseOk ) {
367351 // We're quiescing, and we see a channel.close-ok:
@@ -378,6 +362,23 @@ public void releaseChannelNumber() {
378362 }
379363 }
380364
365+ private void asyncShutdown (Command command ) throws IOException {
366+ releaseChannelNumber ();
367+ ShutdownSignalException signal = new ShutdownSignalException (false ,
368+ false ,
369+ command ,
370+ this );
371+ synchronized (_channelMutex ) {
372+ try {
373+ processShutdownSignal (signal , true , false );
374+ quiescingTransmit (new Channel .CloseOk ());
375+ } finally {
376+ notifyOutstandingRpc (signal );
377+ }
378+ }
379+ notifyListeners ();
380+ }
381+
381382 /** Public API - {@inheritDoc} */
382383 public void close ()
383384 throws IOException
@@ -419,7 +420,7 @@ public void close(int closeCode,
419420 throws IOException
420421 {
421422 // First, notify all our dependents that we are shutting down.
422- // This clears _isOpen , so no further work from the
423+ // This clears isOpen() , so no further work from the
423424 // application side will be accepted, and any inbound commands
424425 // will be discarded (unless they're channel.close-oks).
425426 Channel .Close reason = new Channel .Close (closeCode , closeMessage , 0 , 0 );
@@ -442,8 +443,8 @@ public void close(int closeCode,
442443 }
443444
444445 // Now that we're in quiescing state, channel.close was sent and
445- // we wait for the reply. We ignore the result. (It's always
446- // close-ok.)
446+ // we wait for the reply. We ignore the result.
447+ // (It's NOT always close-ok.)
447448 notify = true ;
448449 k .getReply (-1 );
449450 } catch (TimeoutException ise ) {
@@ -891,4 +892,5 @@ public Channel.FlowOk getFlow() {
891892 public long getNextPublishSeqNo () {
892893 return nextPublishSeqNo ;
893894 }
894- }
895+
896+ }
0 commit comments