protected void processDispatch(Command command) throws IOException {
   MessageDispatch messageDispatch =
       (MessageDispatch) (command.isMessageDispatch() ? command : null);
   try {
     if (!stopping.get()) {
       if (messageDispatch != null) {
         protocolManager.preProcessDispatch(messageDispatch);
       }
       dispatch(command);
     }
   } catch (IOException e) {
     if (messageDispatch != null) {
       TransmitCallback sub = messageDispatch.getTransmitCallback();
       protocolManager.postProcessDispatch(messageDispatch);
       if (sub != null) {
         sub.onFailure();
       }
       messageDispatch = null;
       throw e;
     }
   } finally {
     if (messageDispatch != null) {
       TransmitCallback sub = messageDispatch.getTransmitCallback();
       protocolManager.postProcessDispatch(messageDispatch);
       if (sub != null) {
         sub.onSuccess();
       }
     }
   }
 }
 public void dispatchAsync(Command message) {
   if (!stopping.get()) {
     if (taskRunner == null) {
       dispatchSync(message);
     } else {
       synchronized (dispatchQueue) {
         dispatchQueue.add(message);
       }
       try {
         taskRunner.wakeup();
       } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
       }
     }
   } else {
     if (message.isMessageDispatch()) {
       MessageDispatch md = (MessageDispatch) message;
       TransmitCallback sub = md.getTransmitCallback();
       protocolManager.postProcessDispatch(md);
       if (sub != null) {
         sub.onFailure();
       }
     }
   }
 }
  protected void doStop() throws Exception {
    this.acceptorUsed.onStopped(this);
    /*
     * What's a duplex bridge? try { synchronized (this) { if (duplexBridge !=
     * null) { duplexBridge.stop(); } } } catch (Exception ignore) {
     * LOG.trace("Exception caught stopping. This exception is ignored.",
     * ignore); }
     */
    try {
      getTransportConnection().close();
    } catch (Exception e) {
      // log
    }

    if (taskRunner != null) {
      taskRunner.shutdown(1);
      taskRunner = null;
    }

    active = false;
    // Run the MessageDispatch callbacks so that message references get
    // cleaned up.
    synchronized (dispatchQueue) {
      for (Iterator<Command> iter = dispatchQueue.iterator(); iter.hasNext(); ) {
        Command command = iter.next();
        if (command.isMessageDispatch()) {
          MessageDispatch md = (MessageDispatch) command;
          TransmitCallback sub = md.getTransmitCallback();
          protocolManager.postProcessDispatch(md);
          if (sub != null) {
            sub.onFailure();
          }
        }
      }
      dispatchQueue.clear();
    }
    //
    // Remove all logical connection associated with this connection
    // from the broker.
    if (!protocolManager.isStopped()) {
      context.getStopping().set(true);
      try {
        processRemoveConnection(state.getInfo().getConnectionId(), 0L);
      } catch (Throwable ignore) {
        ignore.printStackTrace();
      }
    }
  }