@Override
 public void stateChanged(StateChangeEvent event) {
   if (event.getNewState() == ReplicaState.REMOVED) {
     PnfsId pnfsId = event.getPnfsId();
     stageRequests.cancel(pnfsId);
     flushRequests.cancel(pnfsId);
   }
 }
 @Override
 public void beforeStop() {
   /* Marks the containers as being shut down and cancels all requests, but
    * doesn't wait for termination.
    */
   flushRequests.shutdown();
   stageRequests.shutdown();
   removeRequests.shutdown();
 }
 /**
  * Flushes a set of files to nearline storage.
  *
  * @param hsmType type of nearline storage
  * @param files files to flush
  * @param callback callback notified for every file flushed
  */
 public void flush(
     String hsmType, Iterable<PnfsId> files, CompletionHandler<Void, PnfsId> callback) {
   try {
     NearlineStorage nearlineStorage = hsmSet.getNearlineStorageByType(hsmType);
     checkArgument(nearlineStorage != null, "No such nearline storage: " + hsmType);
     flushRequests.addAll(nearlineStorage, files, callback);
   } catch (RuntimeException e) {
     for (PnfsId pnfsId : files) {
       callback.failed(e, pnfsId);
     }
   }
 }
  @PreDestroy
  public void shutdown() throws InterruptedException {
    flushRequests.shutdown();
    stageRequests.shutdown();
    removeRequests.shutdown();

    if (timeoutFuture != null) {
      timeoutFuture.cancel(false);
    }
    repository.removeListener(this);

    /* Waits for all requests to have finished. This is blocking to avoid that the
     * repository gets closed nearline storage requests have had a chance to finish.
     */
    long deadline = System.currentTimeMillis() + 3000;
    if (flushRequests.awaitTermination(
        deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS)) {
      if (stageRequests.awaitTermination(
          deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS)) {
        removeRequests.awaitTermination(
            deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
      }
    }
  }
 public int getStoreQueueSize() {
   return flushRequests.getCount(AbstractRequest.State.QUEUED);
 }
 public int getActiveStoreJobs() {
   return flushRequests.getCount(AbstractRequest.State.ACTIVE)
       + flushRequests.getCount(AbstractRequest.State.CANCELED);
 }