/** * Creates an instantly successful promise containing the given value. * * @param successValue the value to fulfill the promise with */ public Promise(V successValue) { success(successValue); }
@Override public void onSuccess(V value) throws Exception { promise.success(value); }
@Override public void onSuccess(V value) throws Exception { try { promise.success(mapper.apply(value)); } catch (Exception e) { promise.fail(e); } }
@Override public void onSuccess(@Nullable V value) throws Exception { if (!result.isFailed()) { // onSuccess can be called from any thread -> sync on resultList... synchronized (resultList) { resultList.set(currentIndex, value); } // Keep track how many results we're waiting for and forward the result when we're finished. latch.countDown(); if (latch.getCount() <= 0) { result.success(resultList); } } }
/** * Transforms a collection of items into a promise for a list of results while invoking an async function for * each item. * * @param input the items to iterate over * @param toPromiseHandler the async function which returns a promise to indicate completion * @param <I> the input type * @param <O> the output type generated by the async function * @return a promise containing the invocation results of the async function for each item in the input */ public static <I, O> Promise<List<O>> sequence(Iterable<I> input, Function<I, Promise<O>> toPromiseHandler) { Promise<List<O>> result = new Promise<>(); List<O> buffer = new ArrayList<>(); processChain(input, toPromiseHandler, (ignored, output) -> buffer.add(output)).onSuccess(() -> result.success( buffer)).onFailure(result::fail); return result; }
/** * Forks the given computation and returns a {@link Promise} for the computed value. * <p> * Forks the computation, which means that the current <tt>CallContext</tt> is transferred to the new thread, * and returns the result of the computation as promise. * <p> * If the executor for the given category is saturated (all threads are active and the queue is full) this * will drop the computation and the promise will be sent a <tt>RejectedExecutionException</tt>. * * @param category the category which implies which executor to use. * @param computation the computation which eventually yields the resulting value * @param <V> the type of the resulting value * @return a promise which will either be eventually supplied with the result of the computation or with an error */ public <V> Promise<V> fork(String category, final Supplier<V> computation) { final Promise<V> result = new Promise<>(); executor(category).dropOnOverload(() -> result.fail(new RejectedExecutionException())).fork(() -> { try { result.success(computation.get()); } catch (Exception t) { result.fail(t); } }); return result; }
/** * Updates the entity in the database asynchronous using a dedicated thread pool. * * @param entity the entity to be written into the DB * @param <E> the type of the entity to update * @return a {@link Promise} handling the update process */ public <E extends Entity> Promise<E> updateAsync(E entity) { Promise<E> promise = new Promise<>(); tasks.executor(ASYNC_UPDATER).start(() -> { try { update(entity); promise.success(entity); } catch (Exception e) { promise.fail(e); } }); return promise; }