T
- value type of child tasksR
- value type of the bundlepublic class AsyncBundle<T,R> extends Object implements Async<R>
Async<T>
tasks as one Async<R>
action.
An AsyncBundle<T,R>
consists of multiple child tasks
of type Async<T>
;
the bundle itself is an Async<R>
,
which completes when all or some of the tasks are completed
(determined by the trigger function).
For common usages, see static methods
anyOf()
, allOf()
, someOf()
.
For example
Stream<Async<String>> tasks = ...; Async<List<String>> bundle = AsyncBundle.someOf(tasks, 2); // the bundle succeeds as soon as 2 of the tasks succeeds
The trigger function determines when and how the bundle completes. The input to the function is the results from currently completed tasks; the output of the function can be one of:
The bundle first invokes the trigger function with an empty list, then with a list of one result after the first task completion, and so on, until the bundle completes.
When a bundle completes, the remaining uncompleted tasks are cancelled; they are no longer needed by the bundle.
Constructor and Description |
---|
AsyncBundle(Stream<Async<T>> tasks,
FunctionX<List<Result<T>>,R> trigger)
Create a bundle over the tasks, with the trigger function.
|
Instance Methods | |
---|---|
List<Result<T>> |
pollTaskResults()
Return an immutable list of results from currently completed tasks.
|
Result<R> |
pollResult()
Implements
Async<R>.pollResult() . |
void |
onCompletion(Consumer<Result<R>> callback)
Implements
Async<R>.onCompletion() . |
void |
cancel(Exception reason)
Implements
Async<R>.cancel() . |
Static Methods | |
<T> AsyncBundle<T,T> |
anyOf(Stream<Async<T>> tasks)
Create a bundle that succeeds as soon as any one of the tasks succeeds.
|
<T> AsyncBundle<T,List<T>> |
allOf(Stream<Async<T>> tasks)
Create a bundle that succeeds if all of the tasks succeeds.
|
<T> AsyncBundle<T,List<T>> |
someOf(Stream<Async<T>> tasks,
int successThreshold)
Create a bundle that succeeds as soon as a number of the tasks succeeds.
|
<T> FunctionX<List<Result<T>>,List<T>> |
triggerOf(int successThreshold,
int taskCount)
Return a simple trigger function
that's based the number of success results.
|
public List<Result<T>> pollTaskResults()
public Result<R> pollResult()
Async<R>.pollResult()
.pollResult
in interface Async<R>
public void onCompletion(Consumer<Result<R>> callback)
Async<R>.onCompletion()
.onCompletion
in interface Async<R>
public void cancel(Exception reason)
Async<R>.cancel()
.
The cancel request will be forwarded to all uncompleted tasks.
public static <T> AsyncBundle<T,T> anyOf(Stream<Async<T>> tasks)
The bundle fails if none of the tasks succeeds.
This method is similar to someOf()
with successThreshold=1.
public static <T> AsyncBundle<T,List<T>> allOf(Stream<Async<T>> tasks)
The bundle fails as soon as one of the task fails.
This method is similar to someOf()
with successThreshold=number_of_tasks.
public static <T> AsyncBundle<T,List<T>> someOf(Stream<Async<T>> tasks, int successThreshold)
When the number of tasks that have succeeded reaches successThreshold
,
the bundle succeeds, with the list of values from these tasks.
The bundle fails if successThreshold
becomes unreachable (because too many tasks have failed).
See triggerOf(successThreshold, taskCount)
for details.
public static <T> FunctionX<List<Result<T>>,List<T>> triggerOf(int successThreshold, int taskCount)
Define failureMax=taskCount-successThreshold
.
Define successCount/failureCount
as number of success/failure results.
The trigger function behaves as the following
if(successCount >= successThreshold)
else if(failureCount > failureMax)
OverLimitException
(limitName="failureMax"). The bundle fails.else
Note that
successThreshold
is impossible to reach
if successThreshold>taskCount
.
In that case the trigger function fails on any input because
failureCount>failureMax
is always true.
The bundle therefore fails immediately on construction.
taskCount
- total number of tasks in the bundle