use of java.util.concurrent.CancellationException in project hazelcast by hazelcast.
the class TrackableJobFuture method shouldCancel.
@Override
protected boolean shouldCancel(boolean mayInterruptIfRunning) {
Address jobOwner = mapReduceService.getLocalAddress();
if (!mapReduceService.registerJobSupervisorCancellation(name, jobId, jobOwner)) {
return false;
}
JobSupervisor supervisor = mapReduceService.getJobSupervisor(name, jobId);
if (supervisor == null || !supervisor.isOwnerNode()) {
return false;
}
Exception exception = new CancellationException("Operation was cancelled by the user");
return supervisor.cancelAndNotify(exception);
}
use of java.util.concurrent.CancellationException in project hazelcast by hazelcast.
the class TrackableJobFuture method setResult.
@Override
public void setResult(Object result) {
Object finalResult = result;
if (finalResult instanceof Throwable && !(finalResult instanceof CancellationException)) {
super.setResult(new ExecutionException((Throwable) finalResult));
return;
}
// If collator is available we need to execute it now
if (collator != null) {
try {
finalResult = collator.collate(((Map) finalResult).entrySet());
} catch (Exception e) {
// Possible exception while collating
finalResult = e;
}
}
if (finalResult instanceof Throwable && !(finalResult instanceof CancellationException)) {
finalResult = new ExecutionException((Throwable) finalResult);
}
super.setResult(finalResult);
}
use of java.util.concurrent.CancellationException in project hazelcast by hazelcast.
the class LoggingScheduledExecutor method afterExecute.
@Override
protected void afterExecute(Runnable runnable, Throwable throwable) {
super.afterExecute(runnable, throwable);
Level level = FINE;
if (throwable == null && runnable instanceof ScheduledFuture && ((ScheduledFuture) runnable).isDone()) {
try {
((Future) runnable).get();
} catch (CancellationException ce) {
throwable = ce;
} catch (ExecutionException ee) {
level = SEVERE;
throwable = ee.getCause();
} catch (InterruptedException ie) {
throwable = ie;
currentThread().interrupt();
}
}
if (throwable != null) {
logger.log(level, "Failed to execute " + runnable, throwable);
}
}
use of java.util.concurrent.CancellationException in project hazelcast by hazelcast.
the class ExecutorServiceTest method testCancellationAwareTask2.
@Test
public void testCancellationAwareTask2() {
Callable task1 = new SleepingTask(Integer.MAX_VALUE);
ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask", 1);
Future future1 = executor.submit(task1);
try {
future1.get(2, TimeUnit.SECONDS);
fail("SleepingTask should not return response");
} catch (TimeoutException ignored) {
} catch (Exception e) {
if (e.getCause() instanceof RejectedExecutionException) {
fail("SleepingTask is rejected!");
}
}
assertFalse(future1.isDone());
Callable task2 = new BasicTestCallable();
Future future2 = executor.submit(task2);
assertFalse(future2.isDone());
assertTrue(future2.cancel(true));
assertTrue(future2.isCancelled());
assertTrue(future2.isDone());
try {
future2.get();
fail("Should not complete the task successfully");
} catch (CancellationException expected) {
} catch (Exception e) {
fail("Unexpected exception " + e);
}
}
use of java.util.concurrent.CancellationException in project hazelcast by hazelcast.
the class ExecutorServiceTest method testInvokeAllTimeoutCancelled.
@Test
public void testInvokeAllTimeoutCancelled() throws Exception {
ExecutorService executor = createSingleNodeExecutorService("testInvokeAll");
assertFalse(executor.isShutdown());
// only one task
ArrayList<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
tasks.add(new SleepingTask(0));
List<Future<Boolean>> futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
assertEquals(futures.size(), 1);
assertEquals(futures.get(0).get(), Boolean.TRUE);
// more tasks
tasks.clear();
for (int i = 0; i < TASK_COUNT; i++) {
tasks.add(new SleepingTask(i < 2 ? 0 : 20));
}
futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
assertEquals(futures.size(), TASK_COUNT);
for (int i = 0; i < TASK_COUNT; i++) {
if (i < 2) {
assertEquals(futures.get(i).get(), Boolean.TRUE);
} else {
boolean excepted = false;
try {
futures.get(i).get();
} catch (CancellationException e) {
excepted = true;
}
assertTrue(excepted);
}
}
}
Aggregations