public static void loadKeyListeners(final ObjectContainer container, ClientContext context) {
   ObjectSet<HasKeyListener> results = Db4oBugs.query(container, HasKeyListener.class);
   for (HasKeyListener l : results) {
     container.activate(l, 1);
     try {
       if (l.isCancelled(container)) continue;
       KeyListener listener = l.makeKeyListener(container, context, true);
       if (listener != null) {
         if (listener.isSSK())
           context.getSskFetchScheduler(listener.isRealTime()).addPersistentPendingKeys(listener);
         else
           context.getChkFetchScheduler(listener.isRealTime()).addPersistentPendingKeys(listener);
         if (logMINOR)
           Logger.minor(
               ClientRequestScheduler.class,
               "Loaded request key listener: " + listener + " for " + l);
       }
     } catch (KeyListenerConstructionException e) {
       System.err.println("FAILED TO LOAD REQUEST BLOOM FILTERS:");
       e.printStackTrace();
       Logger.error(
           ClientRequestSchedulerCore.class, "FAILED TO LOAD REQUEST BLOOM FILTERS: " + e, e);
     } catch (Throwable t) {
       // Probably an error on last startup???
       Logger.error(ClientRequestSchedulerCore.class, "FAILED TO LOAD REQUEST: " + t, t);
       System.err.println("FAILED TO LOAD REQUEST: " + t);
       t.printStackTrace();
     }
     container.deactivate(l, 1);
   }
 }
 /**
  * Register a group of requests (not inserts): a GotKeyListener and/or one or more SendableGet's.
  *
  * @param listener Listens for specific keys. Can be null if the listener is already registered
  *     i.e. on retrying.
  * @param getters The actual requests to register to the request sender queue.
  * @param persistent True if the request is persistent.
  * @param onDatabaseThread True if we are running on the database thread. NOTE:
  *     delayedStoreCheck/probablyNotInStore is unnecessary because we only register the listener
  *     once.
  * @throws FetchException
  */
 public void register(
     final HasKeyListener hasListener,
     final SendableGet[] getters,
     final boolean persistent,
     ObjectContainer container,
     final BlockSet blocks,
     final boolean noCheckStore)
     throws KeyListenerConstructionException {
   if (logMINOR) Logger.minor(this, "register(" + persistent + "," + hasListener + "," + getters);
   if (isInsertScheduler) {
     IllegalStateException e = new IllegalStateException("finishRegister on an insert scheduler");
     throw e;
   }
   if (persistent) {
     innerRegister(hasListener, getters, blocks, noCheckStore, container);
   } else {
     final KeyListener listener;
     if (hasListener != null) {
       listener = hasListener.makeKeyListener(container, clientContext, false);
       if (listener != null) schedTransient.addPendingKeys(listener);
       else Logger.normal(this, "No KeyListener for " + hasListener);
     } else listener = null;
     if (getters != null && !noCheckStore) {
       for (SendableGet getter : getters) datastoreChecker.queueTransientRequest(getter, blocks);
     } else {
       boolean anyValid = false;
       for (int i = 0; i < getters.length; i++) {
         if (!(getters[i].isCancelled(null)
             || getters[i].getCooldownTime(container, clientContext, System.currentTimeMillis())
                 != 0)) anyValid = true;
       }
       finishRegister(getters, false, container, anyValid, null);
     }
   }
 }
  private void innerRegister(
      final HasKeyListener hasListener,
      final SendableGet[] getters,
      final BlockSet blocks,
      boolean noCheckStore,
      ObjectContainer container)
      throws KeyListenerConstructionException {
    final KeyListener listener;
    if (hasListener != null) {
      if (hasListener.isCancelled(container)) {
        if (logMINOR)
          Logger.minor(this, "Key listener is cancelled, not registering: " + hasListener);
        return;
      }
      container.store(hasListener);
      listener = hasListener.makeKeyListener(container, clientContext, false);
      schedCore.addPendingKeys(listener);
    } else listener = null;

    // Avoid NPEs due to deactivation.
    if (getters != null) {
      for (SendableGet getter : getters) {
        container.activate(getter, 1);
        container.store(getter);
      }
    }

    if (isInsertScheduler) throw new IllegalStateException("finishRegister on an insert scheduler");
    if (!noCheckStore) {
      // Check the datastore before proceding.
      for (SendableGet getter : getters) {
        container.activate(getter, 1);
        datastoreChecker.queuePersistentRequest(getter, blocks, container, clientContext);
        container.deactivate(getter, 1);
      }

    } else {
      // We have already checked the datastore, this is a retry, the listener hasn't been
      // unregistered.
      this.finishRegister(getters, true, container, true, null);
    }
  }
 public static void loadKeyListeners(final ObjectContainer container, ClientContext context) {
   ObjectSet<HasKeyListener> results = Db4oBugs.query(container, HasKeyListener.class);
   while (true) {
     HasKeyListener l;
     try {
       if (!results.hasNext()) break;
       l = results.next();
     } catch (IllegalArgumentException e) {
       throw new Db4oException("Something is broken: " + e, e);
       // Allow caller to terminate database.
       // IllegalArgumentException isn't caught, but here it is exclusively caused by corrupt
       // database and/or database bugs. :(
     }
     container.activate(l, 1);
     try {
       if (l.isCancelled(container)) continue;
       KeyListener listener = l.makeKeyListener(container, context, true);
       if (listener != null) {
         if (listener.isSSK())
           context.getSskFetchScheduler(listener.isRealTime()).addPersistentPendingKeys(listener);
         else
           context.getChkFetchScheduler(listener.isRealTime()).addPersistentPendingKeys(listener);
         if (logMINOR)
           Logger.minor(
               ClientRequestScheduler.class,
               "Loaded request key listener: " + listener + " for " + l);
       }
     } catch (KeyListenerConstructionException e) {
       System.err.println("FAILED TO LOAD REQUEST BLOOM FILTERS:");
       e.printStackTrace();
       Logger.error(
           ClientRequestSchedulerCore.class, "FAILED TO LOAD REQUEST BLOOM FILTERS: " + e, e);
     } catch (Throwable t) {
       // Probably an error on last startup???
       Logger.error(ClientRequestSchedulerCore.class, "FAILED TO LOAD REQUEST: " + t, t);
       System.err.println("FAILED TO LOAD REQUEST: " + t);
       t.printStackTrace();
     }
     container.deactivate(l, 1);
   }
 }