public Promise<T> onResolve(Runnable callback) { return promise.onResolve(callback); }
Closer(Closeable closeable) { deferred.getPromise().onResolve(() -> { try { closeable.close(); } catch (Exception e) { // ignore } }); }
public Promise<T> onResolve(Runnable callback) { return deferred.getPromise().onResolve(callback); }
private Promise<Long> deliver(List<PushEventConsumer< ? super T>> toCall, PushEvent<T> event) { if (toCall.size() == 1) { return doCall(event, toCall.get(0)); } else { List<Promise<Long>> calls = toCall.stream().map(pec -> { if (semaphore.tryAcquire()) { return doSendWithBackPressure(pec, event) .onResolve(() -> semaphore.release()); } else { return doCall(event, pec); } }).collect(toList()); return sameThread.all(calls) .map(l -> l.stream().max(Long::compareTo).orElseGet( () -> Long.valueOf(System.nanoTime()))); } }
private Promise<Long> deliver(List<PushEventConsumer< ? super T>> toCall, PushEvent<T> event) { if (toCall.size() == 1) { return doCall(event, toCall.get(0)); } else { List<Promise<Long>> calls = toCall.stream().map(pec -> { if (semaphore.tryAcquire()) { return doSendWithBackPressure(pec, event) .onResolve(() -> semaphore.release()); } else { return doCall(event, pec); } }).collect(toList()); return sameThread.all(calls) .map(l -> l.stream().max(Long::compareTo).orElseGet( () -> Long.valueOf(System.nanoTime()))); } }
private Promise<Long> deliver(List<PushEventConsumer< ? super T>> toCall, PushEvent<T> event) { if (toCall.size() == 1) { return doCall(event, toCall.get(0)); } else { List<Promise<Long>> calls = toCall.stream().map(pec -> { if (semaphore.tryAcquire()) { return doSendWithBackPressure(pec, event) .onResolve(() -> semaphore.release()); } else { return doCall(event, pec); } }).collect(toList()); return sameThread.all(calls) .map(l -> l.stream().max(Long::compareTo).orElseGet( () -> Long.valueOf(System.nanoTime()))); } }
/** * Run the specified chain when the specified promise is resolved. * * @param promise The promise associated with the chain. * @param chain The chain to run when the promise is resolved. */ static <V> void chain(Promise<V> promise, Runnable chain) { if (promise.isDone()) { try { chain.run(); } catch (Throwable t) { uncaughtException(t); } } else { promise.onResolve(chain); } }
/** * Run the specified chain when the specified promise is resolved. * * @param promise The promise associated with the chain. * @param chain The chain to run when the promise is resolved. */ static <V> void chain(Promise<V> promise, Runnable chain) { if (promise.isDone()) { try { chain.run(); } catch (Throwable t) { uncaughtException(t); } } else { promise.onResolve(chain); } }
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)); } }
flatmap.onResolve(new Chain(flatmap)); return;
flatmap.onResolve(new Chain(flatmap)); return;
/** * Resolve this Promise with the specified Promise. * <p> * If the specified Promise is successfully resolved, this Promise is * resolved with the value of the specified Promise. If the specified * Promise is resolved with a failure, this Promise is resolved with the * failure of the specified Promise. * * @param with A Promise whose value or failure must be used to resolve this * Promise. Must not be {@code null}. * @return A Promise that is resolved only when this Promise is resolved by * the specified Promise. The returned Promise must be successfully * resolved with the value {@code null}, if this Promise was * resolved by the specified Promise. The returned Promise must be * resolved with a failure of {@link IllegalStateException}, if this * Promise was already resolved when the specified Promise was * resolved. */ Promise<Void> resolveWith(Promise< ? extends T> with) { DeferredPromiseImpl<Void> chained = deferred(); with.onResolve(chained.new ResolveWith<>(with, this)); return chained.orDone(); }
/** * Resolve this Promise with the specified Promise. * <p> * If the specified Promise is successfully resolved, this Promise is * resolved with the value of the specified Promise. If the specified * Promise is resolved with a failure, this Promise is resolved with the * failure of the specified Promise. * * @param with A Promise whose value or failure must be used to resolve this * Promise. Must not be {@code null}. * @return A Promise that is resolved only when this Promise is resolved by * the specified Promise. The returned Promise must be successfully * resolved with the value {@code null}, if this Promise was * resolved by the specified Promise. The returned Promise must be * resolved with a failure of {@link IllegalStateException}, if this * Promise was already resolved when the specified Promise was * resolved. */ Promise<Void> resolveWith(Promise< ? extends T> with) { DeferredPromiseImpl<Void> chained = deferred(); with.onResolve(chained.new ResolveWith<>(with, this)); return chained.orDone(); }
/** * Resolve this Promise with the specified Promise. * <p> * If the specified Promise is successfully resolved, this Promise is * resolved with the value of the specified Promise. If the specified * Promise is resolved with a failure, this Promise is resolved with the * failure of the specified Promise. * * @param with A Promise whose value or failure must be used to resolve this * Promise. Must not be {@code null}. * @return A Promise that is resolved only when this Promise is resolved by * the specified Promise. The returned Promise must be successfully * resolved with the value {@code null}, if this Promise was * resolved by the specified Promise. The returned Promise must be * resolved with a failure of {@link IllegalStateException}, if this * Promise was already resolved when the specified Promise was * resolved. */ Promise<Void> resolveWith(Promise< ? extends T> with) { DeferredPromiseImpl<Void> chained = deferred(); with.onResolve(chained.new ResolveWith<>(with, this)); return chained.orDone(); }
/** * Resolve this Promise with the specified Promise. * <p> * If the specified Promise is successfully resolved, this Promise is * resolved with the value of the specified Promise. If the specified * Promise is resolved with a failure, this Promise is resolved with the * failure of the specified Promise. * * @param with A Promise whose value or failure must be used to resolve this * Promise. Must not be {@code null}. * @return A Promise that is resolved only when this Promise is resolved by * the specified Promise. The returned Promise must be successfully * resolved with the value {@code null}, if this Promise was * resolved by the specified Promise. The returned Promise must be * resolved with a failure of {@link IllegalStateException}, if this * Promise was already resolved when the specified Promise was * resolved. */ Promise<Void> resolveWith(Promise< ? extends T> with) { DeferredPromiseImpl<Void> chained = deferred(); with.onResolve(chained.new ResolveWith<>(with, this)); return chained.orDone(); }
/** * Resolve this Promise with the specified Promise. * <p> * If the specified Promise is successfully resolved, this Promise is * resolved with the value of the specified Promise. If the specified * Promise is resolved with a failure, this Promise is resolved with the * failure of the specified Promise. * * @param with A Promise whose value or failure must be used to resolve this * Promise. Must not be {@code null}. * @return A Promise that is resolved only when this Promise is resolved by * the specified Promise. The returned Promise must be successfully * resolved with the value {@code null}, if this Promise was * resolved by the specified Promise. The returned Promise must be * resolved with a failure of {@link IllegalStateException}, if this * Promise was already resolved when the specified Promise was * resolved. */ Promise<Void> resolveWith(Promise< ? extends T> with) { DeferredPromiseImpl<Void> chained = deferred(); with.onResolve(chained.new ResolveWith<>(with, this)); return chained.orDone(); }
@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); } }
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)); } } }
@Override public boolean refresh() throws Exception { init(); if (!index.refresh()) { return false; } index.bridge.onResolve(() -> { for (RepositoryListenerPlugin listener : registry.getPlugins(RepositoryListenerPlugin.class)) { try { listener.repositoryRefreshed(this); } catch (Exception e) { reporter.exception(e, "Updating listener plugin %s", listener); } } }); return true; }
@Override public boolean refresh() throws Exception { init(); if (!index.refresh()) { return false; } index.bridge.onResolve(() -> { for (RepositoryListenerPlugin listener : registry.getPlugins(RepositoryListenerPlugin.class)) { try { listener.repositoryRefreshed(this); } catch (Exception e) { reporter.exception(e, "Updating listener plugin %s", listener); } } }); return true; }