code: public void closeSelectionKey(SelectionKey key) {
      if (key.attachment() instanceof Session) {
        Session session = (Session) key.attachment();
        if (session != null) {
          session.close();
        }
      }
    }
docs: Cancel selection key