public AsyncResponseFuture(ClassLoader loader, Method method, SerializationStrategy serializationStrategy, DispatchQueue queue) { this.loader = loader; this.method = method; this.serializationStrategy = serializationStrategy; this.queue = queue; this.deferred = new Deferred<>(); }
/** * Create a new Deferred with the callback executor and scheduled executor * of this PromiseFactory object. * <p> * Use this method instead of {@link Deferred#Deferred()} to create a new * {@link Deferred} whose associated Promise uses executors other than the * default executors. * * @param <T> The value type associated with the returned Deferred. * @return A new {@link Deferred} with the callback and scheduled executors * of this PromiseFactory object */ public <T> Deferred<T> deferred() { return new Deferred<>(this); }
/** * Create a new Deferred with the callback executor and scheduled executor * of this PromiseFactory object. * <p> * Use this method instead of {@link Deferred#Deferred()} to create a new * {@link Deferred} whose associated Promise uses executors other than the * default executors. * * @param <T> The value type associated with the returned Deferred. * @return A new {@link Deferred} with the callback and scheduled executors * of this PromiseFactory object */ public <T> Deferred<T> deferred() { return new Deferred<>(this); }
/** * Create a new Deferred with the callback executor and scheduled executor * of this PromiseFactory object. * <p> * Use this method instead of {@link Deferred#Deferred()} to create a new * {@link Deferred} whose associated Promise uses executors other than the * default executors. * * @param <T> The value type associated with the returned Deferred. * @return A new {@link Deferred} with the callback and scheduled executors * of this PromiseFactory object */ public <T> Deferred<T> deferred() { return new Deferred<>(this); }
/** * Create a new Deferred with the callback executor and scheduled executor * of this PromiseFactory object. * <p> * Use this method instead of {@link Deferred#Deferred()} to create a new * {@link Deferred} whose associated Promise uses executors other than the * default executors. * * @param <T> The value type associated with the returned Deferred. * @return A new {@link Deferred} with the callback and scheduled executors * of this PromiseFactory object */ public <T> Deferred<T> deferred() { return new Deferred<>(this); }
/** * Create a new Deferred with the callback executor and scheduled executor * of this PromiseFactory object. * <p> * Use this method instead of {@link Deferred#Deferred()} to create a new * {@link Deferred} whose associated Promise uses executors other than the * default executors. * * @param <T> The value type associated with the returned Deferred. * @return A new {@link Deferred} with the callback and scheduled executors * of this PromiseFactory object */ public <T> Deferred<T> deferred() { return new Deferred<>(this); }
/** * Create a new Deferred with the callback executor and scheduled executor * of this PromiseFactory object. * <p> * Use this method instead of {@link Deferred#Deferred()} to create a new * {@link Deferred} whose associated Promise uses executors other than the * default executors. * * @param <T> The value type associated with the returned Deferred. * @return A new {@link Deferred} with the callback and scheduled executors * of this PromiseFactory object */ public <T> Deferred<T> deferred() { return new Deferred<>(this); }
/** * Create a new Deferred with the callback executor and scheduled executor * of this PromiseFactory object. * <p> * Use this method instead of {@link Deferred#Deferred()} to create a new * {@link Deferred} whose associated Promise uses executors other than the * default executors. * * @param <T> The value type associated with the returned Deferred. * @return A new {@link Deferred} with the callback and scheduled executors * of this PromiseFactory object */ public <T> Deferred<T> deferred() { return new Deferred<>(this); }
protected enum State { //disposed is a final state, normally only for factory components disposed(-1, false, false, false), //Since enable/disable on the component description are asynchronous, this tracks the component configuration state //which may differ while the enable/disable is occurring. disabled(-1, false, false, false), unsatisfiedReference(ComponentConfigurationDTO.UNSATISFIED_REFERENCE, true, false, false), satisfied(ComponentConfigurationDTO.SATISFIED, true, true, false), active(ComponentConfigurationDTO.ACTIVE, true, true, true); private final int specState; private final boolean enabled; private final boolean satisfed; private final boolean actve; private State(int specState, boolean enabled, boolean satisfied, boolean active) { this.specState = specState; this.enabled = enabled; this.satisfed = satisfied; this.actve = active; } public int getSpecState() { return specState;
protected enum State { //disposed is a final state, normally only for factory components disposed(-1, false, false, false), //Since enable/disable on the component description are asynchronous, this tracks the component configuration state //which may differ while the enable/disable is occurring. disabled(-1, false, false, false), unsatisfiedReference(ComponentConfigurationDTO.UNSATISFIED_REFERENCE, true, false, false), satisfied(ComponentConfigurationDTO.SATISFIED, true, true, false), active(ComponentConfigurationDTO.ACTIVE, true, true, true); private final int specState; private final boolean enabled; private final boolean satisfed; private final boolean actve; private State(int specState, boolean enabled, boolean satisfied, boolean active) { this.specState = specState; this.enabled = enabled; this.satisfed = satisfied; this.actve = active; } public int getSpecState() { return specState;
/** * Create a new Promise that has been resolved with the specified failure. * * @param failure The failure of the resolved Promise. Must not be null. * @param <T> The value type associated with the returned Promise. * @return A new Promise that has been resolved with the specified failure. */ public static <T> Promise<T> failed(Throwable failure) { if (failure == null) throw new NullPointerException(); Deferred<T> def = new Deferred<T>(); def.fail(failure); return def.getPromise(); }
/** * Create a new Promise that has been resolved with the specified value. * * @param value The value of the resolved Promise. * @param <T> The value type associated with the returned Promise. * @return A new Promise that has been resolved with the specified value. */ public static <T> Promise<T> resolved(T value) { Deferred<T> def = new Deferred<T>(); def.resolve(value); return def.getPromise(); }
@Override public CancellablePromiseImpl<Instant> after(long ms) { Deferred<Instant> deferred = new Deferred<>(); Instant start = Instant.now(); ScheduledFuture<?> schedule = getExecutor().schedule(() -> { deferred.resolve(start); }, ms, TimeUnit.MILLISECONDS); return new CancellablePromiseImpl<Instant>(deferred.getPromise()) { public boolean cancel() { try { return schedule.cancel(true); } catch (Exception e) { return false; } } }; }
@Override public <T> CancellablePromiseImpl<T> after(Callable<T> callable, long ms) { Deferred<T> deferred = new Deferred<>(); ScheduledFuture<?> schedule = getExecutor().schedule(() -> { try { deferred.resolve(callable.call()); } catch (Throwable e) { deferred.fail(e); } }, ms, TimeUnit.MILLISECONDS); return new CancellablePromiseImpl<T>(deferred.getPromise()) { public boolean cancel() { try { return schedule.cancel(true); } catch (Exception e) { return false; } } }; }
/** * 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; }
@Override public Promise<Void> disableComponents( final boolean async ) { synchronized (enableLock) { if ( m_disablePromise != null) { return m_disablePromise; } wait( m_enablePromise ); List<AbstractComponentManager<S>> cms; synchronized ( m_components ) { m_enabled = false; cms = getDirectComponentManagers( ); clearComponents(); } List<Promise<Void>> promises = new ArrayList<>(); for ( AbstractComponentManager<S> cm : cms ) { promises.add(cm.disable( async )); } m_disablePromise = new Deferred<List<Void>>().resolveWith(Promises.<Void, Void>all(promises)); m_enablePromise = null; return m_disablePromise; } }
public <T> Success<T, T> delay(long ms) { return (p) -> { Deferred<T> deferred = new Deferred<T>(); after(ms).then((pp) -> { deferred.resolve(p.getValue()); return null; }); return deferred.getPromise(); }; }
@Override public Promise<Void> disableComponents( final boolean async ) { synchronized (enableLock) { if ( m_disablePromise != null) { return m_disablePromise; } wait( m_enablePromise ); List<AbstractComponentManager<S>> cms; synchronized ( m_components ) { m_enabled = false; cms = getDirectComponentManagers( ); clearComponents(); } List<Promise<Void>> promises = new ArrayList<>(); for ( AbstractComponentManager<S> cm : cms ) { promises.add(cm.disable( async )); } m_disablePromise = new Deferred<List<Void>>().resolveWith(Promises.<Void, Void>all(promises)); m_enablePromise = null; return m_disablePromise; } }
/** * Return a new Promise that will fail after timeout ms with a * {@link TimeoutException} */ // @Override public <T> CancellablePromiseImpl<T> before(Promise<T> promise, long timeout) { Deferred<T> d = new Deferred<T>(); Unique only = new Unique(); after(timeout).then((p) -> { only.once(() -> d.fail(TimeoutException.SINGLETON)); return null; }); promise.then((p) -> { only.once(() -> d.resolve(p.getValue())); return null; }, (p) -> { only.once(() -> d.fail(p.getFailure())); }); return new CancellablePromiseImpl<T>(d.getPromise()) { public boolean cancel() { try { return only.once(() -> d.fail(CancelException.SINGLETON)); } catch (Exception e) { return false; } } }; }