Search in sources :

Example 16 with CancellationException

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);
}
Also used : Address(com.hazelcast.nio.Address) CancellationException(java.util.concurrent.CancellationException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 17 with CancellationException

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);
}
Also used : CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 18 with CancellationException

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);
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) RunnableScheduledFuture(java.util.concurrent.RunnableScheduledFuture) Level(java.util.logging.Level) ExecutionException(java.util.concurrent.ExecutionException) ScheduledFuture(java.util.concurrent.ScheduledFuture) RunnableScheduledFuture(java.util.concurrent.RunnableScheduledFuture)

Example 19 with CancellationException

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);
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) ExecutorService(java.util.concurrent.ExecutorService) IExecutorService(com.hazelcast.core.IExecutorService) Future(java.util.concurrent.Future) ICompletableFuture(com.hazelcast.core.ICompletableFuture) Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TimeoutException(java.util.concurrent.TimeoutException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 20 with CancellationException

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);
        }
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) ExecutorService(java.util.concurrent.ExecutorService) IExecutorService(com.hazelcast.core.IExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ICompletableFuture(com.hazelcast.core.ICompletableFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

CancellationException (java.util.concurrent.CancellationException)196 ExecutionException (java.util.concurrent.ExecutionException)88 TimeoutException (java.util.concurrent.TimeoutException)50 Test (org.junit.Test)34 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)25 IOException (java.io.IOException)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 Future (java.util.concurrent.Future)23 ArrayList (java.util.ArrayList)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 Callable (java.util.concurrent.Callable)16 ExecutorService (java.util.concurrent.ExecutorService)13 File (java.io.File)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 FutureTask (java.util.concurrent.FutureTask)7 Handler (android.os.Handler)6 GwtIncompatible (com.google.common.annotations.GwtIncompatible)6