public Throwable getFailure() throws InterruptedException { return promise.getFailure(); }
@Override public void fail(Promise<?> resolved) throws Exception { result.fail(resolved.getFailure()); } });
@Override public void fail(Promise<?> resolved) throws Exception { result.fail(resolved.getFailure()); } });
@Override public void fail(Promise<?> resolved) throws Exception { result.fail(resolved.getFailure()); } });
public Throwable getFailure() throws InterruptedException { return deferred.getPromise().getFailure(); }
@Override public void fail(Promise<?> resolved) throws Exception { @SuppressWarnings({"not thrown", "all"}) Throwable fail = fallback.getFailure(); if (fail != null) { result.fail(resolved.getFailure()); } else { result.resolve(fallback.getValue()); } } });
public void run() { R value = null; Throwable f; final boolean interrupted = Thread.interrupted(); try { f = promise.getFailure(); if (f == null) { value = promise.getValue(); } else if (failure != null) { f = failure; } } catch (Throwable e) { f = e; // propagate new exception } finally { if (interrupted) { // restore interrupt status Thread.currentThread().interrupt(); } } chained.resolve(value, f); } }
/** * Make a promise that times out */ @Tooltip(description = "Create a Cancellable Promise that times out after a number of ms", deflt = "2000", type = "number") public CancellablePromise<Void> before(int id, int ms) { Deferred<Void> deferred = new Deferred<Void>(); Promise<Void> promise = deferred.getPromise(); CancellablePromise<Void> before = scheduler.before(promise, ms >= 1000 ? ms : 1000); before.then((p) -> { assert false : "Should never be resolved"; return null; }, (p) -> { if (p.getFailure() == TimeoutException.SINGLETON) { out.println("Timed out " + id); } else if (p.getFailure() == CancelException.SINGLETON) { out.println("Got canceled " + id); } else assert false : "Should not happen since we never fail it"; }); return before; }
public void fail(Promise<?> resolved) throws Exception { Throwable failure; try { failure = resolved.getFailure(); } catch (Throwable e) { chained.resolve(null, e); return; } fallback.onResolve(new Chain<T>(chained, fallback, failure)); } }
@Override public void fail(Promise<?> resolved) throws Exception { if (isDone()) { result.fail(new IllegalStateException("associated Promise already resolved")); } PromiseImpl.this.fail(resolved.getFailure()); result.resolve(null); } });
private Promise<List<Artifact>> getArtifacts(Set<URI> cycles, final Collection<URI> uris) { Deferred<List<Artifact>> deferred = promiseFactory.deferred(); promiseFactory.executor() .execute(() -> { try { deferred.resolveWith(uris.stream() .map(uri -> getArtifacts(cycles, base.resolve(uri)).recover(failed -> { if (!defaults.contains(uri)) { logger.info("Failed to get artifacts for %s", uri, failed.getFailure()); } return Collections.emptyList(); })) .collect(toPromise(promiseFactory)) .map(ll -> ll.stream() .flatMap(List::stream) .collect(toList()))); } catch (Throwable e) { deferred.fail(e); } }); return deferred.getPromise(); }
private Promise<List<Artifact>> getArtifacts(Set<URI> cycles, final Collection<URI> uris) { Deferred<List<Artifact>> deferred = promiseFactory.deferred(); promiseFactory.executor() .execute(() -> { try { deferred.resolveWith(uris.stream() .map(uri -> getArtifacts(cycles, base.resolve(uri)).recover(failed -> { if (!defaults.contains(uri)) { logger.info("Failed to get artifacts for %s", uri, failed.getFailure()); } return Collections.emptyList(); })) .collect(toPromise(promiseFactory)) .map(ll -> ll.stream() .flatMap(List::stream) .collect(toList()))); } catch (Throwable e) { deferred.fail(e); } }); return deferred.getPromise(); }
@SuppressWarnings("unchecked") protected void doService(SerializationStrategy serializationStrategy, ClassLoader loader, Method method, Object target, DataByteArrayInputStream requestStream, final DataByteArrayOutputStream responseStream, final Runnable onComplete) { final AsyncServiceResponse helper = new AsyncServiceResponse(loader, method, responseStream, onComplete, serializationStrategy); try { Class<?>[] types = method.getParameterTypes(); final Object[] args = new Object[types.length]; serializationStrategy.decodeRequest(loader, types, requestStream, args); final Promise<Object> promise = (Promise<Object>)method.invoke(target, args); promise.onResolve(() -> { try{ helper.send(promise.getFailure(), promise.getFailure()==null ? promise.getValue() : null); } catch (Exception e){ helper.send(e, null); } }); } catch (Throwable t) { helper.send(t, null); } }
/** * Return a holder of the result of the specified Promise. * * @since 1.1 */ static <R> Result<R> collect(Promise< ? extends R> promise) { if (promise instanceof PromiseImpl) { @SuppressWarnings("unchecked") PromiseImpl<R> impl = (PromiseImpl<R>) promise; return impl.collect(); } if (!promise.isDone()) { return new Result<R>(new AssertionError("promise not resolved")); } final boolean interrupted = Thread.interrupted(); try { Throwable fail = promise.getFailure(); if (fail == null) { return new Result<R>(promise.getValue()); } return new Result<R>(fail); } catch (Throwable e) { return new Result<R>(e); // propagate new exception } finally { if (interrupted) { // restore interrupt status Thread.currentThread().interrupt(); } } } }
/** * Return a holder of the result of the specified Promise. * * @since 1.1 */ static <R> Result<R> collect(Promise< ? extends R> promise) { if (promise instanceof PromiseImpl) { @SuppressWarnings("unchecked") PromiseImpl<R> impl = (PromiseImpl<R>) promise; return impl.collect(); } if (!promise.isDone()) { return new Result<R>(new AssertionError("promise not resolved")); } final boolean interrupted = Thread.interrupted(); try { Throwable fail = promise.getFailure(); if (fail == null) { return new Result<R>(promise.getValue()); } return new Result<R>(fail); } catch (Throwable e) { return new Result<R>(e); // propagate new exception } finally { if (interrupted) { // restore interrupt status Thread.currentThread().interrupt(); } } } }
/** * Return a holder of the result of the specified Promise. * * @since 1.1 */ static <R> Result<R> collect(Promise< ? extends R> promise) { if (promise instanceof PromiseImpl) { @SuppressWarnings("unchecked") PromiseImpl<R> impl = (PromiseImpl<R>) promise; return impl.collect(); } if (!promise.isDone()) { return new Result<R>(new AssertionError("promise not resolved")); } final boolean interrupted = Thread.interrupted(); try { Throwable fail = promise.getFailure(); if (fail == null) { return new Result<R>(promise.getValue()); } return new Result<R>(fail); } catch (Throwable e) { return new Result<R>(e); // propagate new exception } finally { if (interrupted) { // restore interrupt status Thread.currentThread().interrupt(); } } } }
@Override public void fail(Promise<?> resolved) throws Exception { try { T recover = recovery.apply(resolved); if (recover != null) { result.resolve(recover); } else { result.fail(resolved.getFailure()); } } catch (Throwable t) { result.fail(t); } } });
@Override public void fail(Promise<?> resolved) throws Exception { try { Promise<? extends T> recover = recovery.apply(resolved); if (recover != null) { result.resolveWith(recover); } else { result.fail(resolved.getFailure()); } } catch (Throwable t) { result.fail(t); } } });
public void fail(Promise<?> resolved) throws Exception { T recovered; Throwable failure; try { recovered = recovery.apply(resolved); failure = resolved.getFailure(); } catch (Throwable e) { chained.resolve(null, e); return; } if (recovered == null) { chained.resolve(null, failure); } else { chained.resolve(recovered, null); } } }
public void fail(Promise<?> resolved) throws Exception { Promise<? extends T> recovered; Throwable failure; try { recovered = recovery.apply(resolved); failure = resolved.getFailure(); } catch (Throwable e) { chained.resolve(null, e); return; } if (recovered == null) { chained.resolve(null, failure); } else { recovered.onResolve(new Chain<T>(chained, recovered)); } } }