/**
  * Enqueue a connection finalizer which will be invoked after a short delay.
  *
  * @param r The connection finalizer runnable.
  */
 void registerConnectionFinalizer(Runnable r) {
   synchronized (connectionFinalizerLock) {
     if (connectionFinalizer != null) {
       connectionFinalizerPendingJobQueue.add(r);
     } else {
       // Already finalized - invoked immediately.
       r.run();
     }
   }
 }
    @Override
    public void run() {
      if (!connectionFinalizerActiveJobQueue.isEmpty()) {
        for (Runnable r : connectionFinalizerActiveJobQueue) {
          r.run();
        }
        connectionFinalizerActiveJobQueue.clear();
      }

      // Switch the lists.
      synchronized (connectionFinalizerLock) {
        List<Runnable> tmp = connectionFinalizerActiveJobQueue;
        connectionFinalizerActiveJobQueue = connectionFinalizerPendingJobQueue;
        connectionFinalizerPendingJobQueue = tmp;
      }
    }
  /** {@inheritDoc} */
  @Override
  public void finalizeConnectionHandler(LocalizableMessage finalizeReason) {
    shutdownRequested = true;
    currentConfig.removeLDAPChangeListener(this);

    if (connMonitor != null) {
      DirectoryServer.deregisterMonitorProvider(connMonitor);
    }

    if (statTracker != null) {
      DirectoryServer.deregisterMonitorProvider(statTracker);
    }

    DirectoryServer.deregisterSupportedLDAPVersion(2, this);
    DirectoryServer.deregisterSupportedLDAPVersion(3, this);

    try {
      selector.wakeup();
    } catch (Exception e) {
      logger.traceException(e);
    }

    for (LDAPRequestHandler requestHandler : requestHandlers) {
      requestHandler.processServerShutdown(finalizeReason);
    }

    // Shutdown the connection finalizer and ensure that any pending
    // unclosed connections are closed.
    synchronized (connectionFinalizerLock) {
      connectionFinalizer.shutdown();
      connectionFinalizer = null;

      Runnable r = new ConnectionFinalizerRunnable();
      r.run(); // Flush active queue.
      r.run(); // Flush pending queue.
    }
  }