private void collectCountFromImexService(
      ServiceType service,
      String query,
      PsicquicCountResults results,
      PsicquicSimpleClient client) {
    final String imexQuery = createImexQuery(query);
    results.setImex(true);

    try {

      long count = client.countByQuery(imexQuery);

      results.setImexCount((int) count);
      results.setImexResponding(true);

    } catch (IOException e) {
      log.error(
          "Problem connecting to PSICQUIC service '"
              + service.getName()
              + "': / proxy "
              + intactViewConfiguration.getProxyHost()
              + ":"
              + intactViewConfiguration.getProxyPort(),
          e);

      results.setImexResponding(false);
    }
  }
  public void checkAndResumePsicquicTasks() {
    List<Future> currentRunningTasks = new ArrayList<Future>(runningTasks);

    for (Future<PsicquicCountResults> f : currentRunningTasks) {
      try {
        PsicquicCountResults results = f.get(threadTimeOut, TimeUnit.SECONDS);

        if (results.isImex()) {
          if (results.isImexResponding() && results.getImexCount() > 0) {
            countInOtherImexDatabases += results.getImexCount();
            otherImexDatabasesWithResults++;
          } else if (!results.isImexResponding()) {
            nonRespondingImexDatabases++;
          }
        }

        if (results.isServiceResponding() && results.getPsicquicCount() > 0) {
          countInOtherDatabases += results.getPsicquicCount();
          otherDatabasesWithResults++;
        } else if (!results.isServiceResponding()) {
          nonRespondingDatabases++;
        }

        runningTasks.remove(f);
      } catch (InterruptedException e) {
        log.error("The psicquic task was interrupted, we cancel the task.", e);
        this.nonRespondingDatabases++;
        this.nonRespondingImexDatabases++;
        if (!f.isCancelled()) {
          f.cancel(false);
        }
        runningTasks.remove(f);
      } catch (ExecutionException e) {
        log.error("The psicquic task could not be executed, we cancel the task.", e);
        if (!f.isCancelled()) {
          f.cancel(false);
        }
        runningTasks.remove(f);
      } catch (TimeoutException e) {
        log.error("Service task stopped because of time out " + threadTimeOut + "seconds.");
        this.nonRespondingDatabases++;
        this.nonRespondingImexDatabases++;

        if (!f.isCancelled()) {
          f.cancel(false);
        }
        runningTasks.remove(f);
      } catch (Throwable e) {
        log.error("The psicquic task could not be executed, we cancel the task.", e);
        if (!f.isCancelled()) {
          f.cancel(false);
        }
        runningTasks.remove(f);
      }
    }
  }
  private PsicquicCountResults processPsicquicQueries(
      ServiceType service, String query, PsicquicSimpleClient client) {
    PsicquicCountResults results = new PsicquicCountResults();

    if (isImexService(service)) {
      results.setImex(true);
      collectCountFromImexService(service, query, results, client);
    }

    collectCountFromPsicquicService(service, query, results, client);

    return results;
  }