Search in sources :

Example 51 with TimeoutException

use of java.util.concurrent.TimeoutException in project japid42 by branaway.

the class Mail method send.

/**
	 * Send an email
	 */
public static Future<Boolean> send(Email email) {
    try {
        email = buildMessage(email);
        // has to use this since mail.smtp is a tree in play2
        String string = getConfig("mail.smtp.host");
        if (string != null)
            if (string.equals("mock") && Play.isDev()) {
                Mock.send(email);
                return new Future<Boolean>() {

                    public boolean cancel(boolean mayInterruptIfRunning) {
                        return false;
                    }

                    public boolean isCancelled() {
                        return false;
                    }

                    public boolean isDone() {
                        return true;
                    }

                    public Boolean get() throws InterruptedException, ExecutionException {
                        return true;
                    }

                    public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                        return true;
                    }
                };
            }
        email.setMailSession(getSession());
        return sendMessage(email);
    } catch (EmailException ex) {
        throw new MailException("Cannot send email", ex);
    }
}
Also used : EmailException(org.apache.commons.mail.EmailException) TimeUnit(java.util.concurrent.TimeUnit) MailException(cn.bran.play.exceptions.MailException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 52 with TimeoutException

use of java.util.concurrent.TimeoutException in project hazelcast by hazelcast.

the class InvocationRegistry method register.

/**
     * Registers an invocation.
     *
     * @param invocation The invocation to register.
     * @return false when InvocationRegistry is not alive and registration is not successful, true otherwise
     */
public boolean register(Invocation invocation) {
    final long callId;
    try {
        boolean force = invocation.op.isUrgent() || invocation.isRetryCandidate();
        callId = callIdSequence.next(force);
    } catch (TimeoutException e) {
        throw new HazelcastOverloadException("Failed to start invocation due to overload: " + invocation, e);
    }
    try {
        // Fails with IllegalStateException if the operation is already active
        setCallId(invocation.op, callId);
    } catch (IllegalStateException e) {
        callIdSequence.complete();
        throw e;
    }
    invocations.put(callId, invocation);
    if (!alive) {
        invocation.notifyError(new HazelcastInstanceNotActiveException());
        return false;
    }
    return true;
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) HazelcastOverloadException(com.hazelcast.core.HazelcastOverloadException) TimeoutException(java.util.concurrent.TimeoutException)

Example 53 with TimeoutException

use of java.util.concurrent.TimeoutException 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 54 with TimeoutException

use of java.util.concurrent.TimeoutException in project h2o-3 by h2oai.

the class ForkJoinTask method get.

/**
     * Waits if necessary for at most the given time for the computation
     * to complete, and then retrieves its result, if available.
     *
     * @param timeout the maximum time to wait
     * @param unit the time unit of the timeout argument
     * @return the computed result
     * @throws CancellationException if the computation was cancelled
     * @throws ExecutionException if the computation threw an
     * exception
     * @throws InterruptedException if the current thread is not a
     * member of a ForkJoinPool and was interrupted while waiting
     * @throws TimeoutException if the wait timed out
     */
public final V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    if (Thread.interrupted())
        throw new InterruptedException();
    // Messy in part because we measure in nanosecs, but wait in millisecs
    int s;
    long ns, ms;
    if ((s = status) >= 0 && (ns = unit.toNanos(timeout)) > 0L) {
        long deadline = System.nanoTime() + ns;
        ForkJoinPool p = null;
        ForkJoinPool.WorkQueue w = null;
        Thread t = Thread.currentThread();
        if (t instanceof ForkJoinWorkerThread) {
            ForkJoinWorkerThread wt = (ForkJoinWorkerThread) t;
            p = wt.pool;
            w = wt.workQueue;
            // no retries on failure
            s = p.helpJoinOnce(w, this);
        }
        boolean canBlock = false;
        boolean interrupted = false;
        try {
            while ((s = status) >= 0) {
                if (w != null && w.runState < 0)
                    cancelIgnoringExceptions(this);
                else if (!canBlock) {
                    if (p == null || p.tryCompensate(this, null))
                        canBlock = true;
                } else {
                    if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) > 0L && U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
                        synchronized (this) {
                            if (status >= 0) {
                                try {
                                    wait(ms);
                                } catch (InterruptedException ie) {
                                    if (p == null)
                                        interrupted = true;
                                }
                            } else
                                notifyAll();
                        }
                    }
                    if ((s = status) < 0 || interrupted || (ns = deadline - System.nanoTime()) <= 0L)
                        break;
                }
            }
        } finally {
            if (p != null && canBlock)
                p.incrementActiveCount();
        }
        if (interrupted)
            throw new InterruptedException();
    }
    if ((s &= DONE_MASK) != NORMAL) {
        Throwable ex;
        if (s == CANCELLED)
            throw new CancellationException();
        if (s != EXCEPTIONAL)
            throw new TimeoutException();
        if ((ex = getThrowableException()) != null)
            throw new ExecutionException(ex);
    }
    return getRawResult();
}
Also used : CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 55 with TimeoutException

use of java.util.concurrent.TimeoutException in project h2o-2 by h2oai.

the class h2odriver method doWait.

public MasterPayload doWait() throws Exception {
    ZooKeeper z = ZooKeeperFactory.makeZk(_zk);
    byte[] payload;
    payload = z.getData(_zkroot, null, null);
    ClusterPayload cp = ClusterPayload.fromPayload(payload, ClusterPayload.class);
    z.close();
    assert (cp.numNodes > 0);
    long startMillis = System.currentTimeMillis();
    while (true) {
        z = ZooKeeperFactory.makeZk(_zk);
        if (z.exists(_zkroot, false) == null) {
            z.close();
            throw new Exception("ZooKeeper node does not exist: " + _zkroot);
        }
        try {
            payload = z.getData(_zkroot + "/master", null, null);
            MasterPayload mp = MasterPayload.fromPayload(payload, MasterPayload.class);
            z.close();
            Thread.sleep(CLOUD_FORMATION_SETTLE_DOWN_SECONDS);
            return mp;
        } catch (KeeperException.NoNodeException e) {
        // This is OK, do nothing
        }
        long now = System.currentTimeMillis();
        if (Math.abs(now - startMillis) > (_cloudFormationTimeoutSeconds * 1000)) {
            z.close();
            throw new TimeoutException("Timed out waiting for cloud to form");
        }
        Thread.sleep(1000);
    }
}
Also used : ClusterPayload(water.zookeeper.nodes.ClusterPayload) MasterPayload(water.zookeeper.nodes.MasterPayload) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)788 ExecutionException (java.util.concurrent.ExecutionException)249 IOException (java.io.IOException)184 Test (org.junit.Test)149 ArrayList (java.util.ArrayList)75 CountDownLatch (java.util.concurrent.CountDownLatch)73 ExecutorService (java.util.concurrent.ExecutorService)71 Future (java.util.concurrent.Future)54 CancellationException (java.util.concurrent.CancellationException)44 Test (org.testng.annotations.Test)44 List (java.util.List)39 HashMap (java.util.HashMap)38 Map (java.util.Map)38 File (java.io.File)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)36 TimeUnit (java.util.concurrent.TimeUnit)34 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 URI (java.net.URI)21 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)21