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); } } }
/** Closes any tasks currently in progress */ @Override public void close() { isOpen.set(false); Iterator<Future<?>> iterator = futures.iterator(); while (iterator.hasNext()) { Future<?> future = iterator.next(); iterator.remove(); if (!future.isDone() && !future.isCancelled() && !future.cancel(true)) { log.warn("Could not cancel " + future); } } if (shutdownOnClose) { executorService.shutdownNow(); } }
@Test public void testCancellationAwareTask2() { Callable task1 = new CancellationAwareTask(5000); ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask", 1); executor.submit(task1); Callable task2 = new BasicTestTask(); Future future = executor.submit(task2); assertFalse(future.isDone()); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Should not complete the task successfully"); } catch (CancellationException expected) { } catch (Exception e) { fail("Unexpected exception " + e); } }
@Test public void testCancellationAwareTask() throws ExecutionException, InterruptedException { CancellationAwareTask task = new CancellationAwareTask(5000); ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask"); Future future = executor.submit(task); try { future.get(2, TimeUnit.SECONDS); fail("Should throw TimeoutException!"); } catch (TimeoutException expected) { } assertFalse(future.isDone()); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Should not complete the task successfully"); } catch (CancellationException expected) { } catch (Exception e) { fail("Unexpected exception " + e); } }
@Test public void isCancelled_shouldReturnFalse_givenAFutureReturnedByExecute() throws Exception { Future<?> result = asyncExecutor.execute(() -> {}); assertFalse(result.isCancelled()); }
@Override public boolean isRunning() { return !task.isDone() && !task.isCancelled(); }