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);
}
}
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;
}
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);
}
}
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();
}
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);
}
}
Aggregations