public interface SimpleReactStream<U> extends LazyStream<U>, BlockingStream<U>, ConfigurableStream<U>, EagerOrLazyToQueue<U>
exceptionSoftener| Modifier and Type | Method and Description |
|---|---|
default <T,R> SimpleReactStream<R> |
allOf(java.util.stream.Collector collector,
java.util.function.Function<T,R> fn) |
default <T,R> SimpleReactStream<R> |
allOf(java.util.function.Function<java.util.List<T>,R> fn)
React and allOf
allOf is a non-blocking equivalent of block.
|
static SimpleReactFailedStageException |
assureSimpleReactException(java.lang.Throwable throwable) |
default SimpleReactStream<U> |
capture(java.util.function.Consumer<? extends java.lang.Throwable> errorHandler)
React capture
While onFail is used for disaster recovery (when it is possible to
recover) - capture is used to capture those occasions where the full
pipeline has failed and is unrecoverable.
|
static <T> SimpleReactStream<T> |
empty() |
default SimpleReactStream<U> |
filter(java.util.function.Predicate<? super U> p)
Removes elements that do not match the supplied predicate from the
dataflow
|
default <R> SimpleReactStream<R> |
flatMap(java.util.function.Function<? super U,? extends java.util.stream.Stream<? extends R>> flatFn)
Allows aggregate values in a Stream to be flatten into a single Stream.
|
default <R> SimpleReactStream<R> |
fromStream(java.util.stream.Stream<R> stream) |
default <R> SimpleReactStream<R> |
fromStreamCompletableFuture(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream)
Construct a SimpleReactStream from provided Stream of CompletableFutures
|
static <T> SimpleReactStream<T> |
futureStream(java.lang.Iterable<T> iterable)
Wrap an Iterable into a FutureStream.
|
static <T> SimpleReactStream<T> |
futureStream(java.util.Iterator<T> iterator)
Wrap an Iterator into a FutureStream.
|
static <T> SimpleReactStream<T> |
futureStream(java.util.stream.Stream<T> stream) |
static <T> SimpleReactStream<T> |
futureStream(java.util.stream.Stream<T> stream,
boolean eager)
Wrap a Stream into a SimpleReactStream.
|
static <U,R> java.util.function.Function<U,R> |
handleExceptions(java.util.function.Function<U,R> fn) |
static java.util.concurrent.CompletableFuture[] |
lastActiveArray(StreamWrapper lastActive) |
default SimpleReactStream<U> |
merge(SimpleReactStream<U> s)
Merge this reactive dataflow with another of the same type.
|
static <R> SimpleReactStream<R> |
merge(SimpleReactStream s1,
SimpleReactStream s2)
Merge this reactive dataflow with another - recommended for merging
different types.
|
static <T> SimpleReactStream<T> |
of(T... values) |
static <T> SimpleReactStream<T> |
of(T value) |
default <U> SimpleReactStream<U> |
onFail(java.util.function.Function<? extends SimpleReactFailedStageException,U> fn)
React onFail
Define a function that can be used to recover from exceptions during the
preceeding stage of the dataflow.
|
static SimpleReact |
parallelBuilder() |
static SimpleReact |
parallelBuilder(int parallelism)
Construct a new SimpleReact builder, with a new task executor and retry executor
with configured number of threads
|
static SimpleReact |
parallelCommonBuilder() |
static <U> SimpleReactStream<U> |
parallelOf(U... array)
Construct a SimpleReact Stage from a supplied array
|
default SimpleReactStream<U> |
peek(java.util.function.Consumer<? super U> consumer)
Peek asynchronously at the results in the current stage.
|
default <R> SimpleReactStream<R> |
retry(java.util.function.Function<U,R> fn)
Will execute this phase on the RetryExecutor (default or user supplied).
|
static SimpleReact |
sequentialBuilder() |
static SimpleReact |
sequentialCommonBuilder() |
static SimpleReact |
simple(java.util.concurrent.ExecutorService executor) |
static SimpleReact |
simple(java.util.concurrent.ExecutorService executor,
com.nurkiewicz.asyncretry.RetryExecutor retry) |
static SimpleReact |
simple(com.nurkiewicz.asyncretry.RetryExecutor retry) |
default <T> java.util.stream.Stream<java.util.concurrent.CompletableFuture<T>> |
streamCompletableFutures() |
default <R> SimpleReactStream<R> |
then(java.util.function.Function<U,R> fn)
React then
Unlike 'with' this method is fluent, and returns another Stage Builder
that can represent the next stage in the dataflow.
|
default <R> java.util.List<java.util.concurrent.CompletableFuture<R>> |
with(java.util.function.Function<U,R> fn)
React with
Asynchronously apply the function supplied to the currently active event
tasks in the dataflow.
|
getLastActive, getLazyCollector, getWaitStrategy, run, run, run, run, runOnCurrentaggregateResults, block, block, block, block, block, blockAndExtract, blockAndExtract, capture, collectResults, first, getSafe, last, submitAndBlockgetErrorHandler, getLastActive, getRetrier, getSimpleReact, getTaskExecutor, isEager, withEager, withErrorHandler, withLastActive, withLazyCollector, withQueueFactory, withRetrier, withTaskExecutor, withWaitStrategyisEager, toQueuegetQueueFactorygetQueueFactorydefault <T,R> SimpleReactStream<R> allOf(java.util.stream.Collector collector, java.util.function.Function<T,R> fn)
allOf in interface EagerToQueue<U>allOf in interface LazyToQueue<U>collector - to perform aggregation / reduction operation on the results
from active stage (e.g. to Collect into a List or String)fn - Function that receives the results of all currently active
tasks as inputstatic java.util.concurrent.CompletableFuture[] lastActiveArray(StreamWrapper lastActive)
default <R> SimpleReactStream<R> retry(java.util.function.Function<U,R> fn)
fn - Function that will be executed and retried on failuredefault <R> SimpleReactStream<R> fromStream(java.util.stream.Stream<R> stream)
default <R> SimpleReactStream<R> fromStreamCompletableFuture(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream)
stream - JDK Stream to construct new SimpleReactStream fromdefault <R> SimpleReactStream<R> then(java.util.function.Function<U,R> fn)
new SimpleReact().<Integer, Integer> react(() -> 1, () -> 2, () -> 3)
.then((it) -> it * 100)
.then((it) -> "*" + it)
React then allows event reactors to be chained. Unlike React with, which
returns a collection of Future references, React then is a fluent
interface that returns the React builder - allowing further reactors to
be added to the chain.
React then does not block.
React with can be called after React then which gives access to the full
CompleteableFuture api. CompleteableFutures can be passed back into
SimpleReact via SimpleReact.react(streamOfCompleteableFutures);
See this blog post for examples of what can be achieved via
CompleteableFuture :- http://www.nurkiewicz.com/2013/12/promises-and-completablefuture.htm
l then in interface EagerToQueue<U>then in interface LazyToQueue<U>fn - Function to be applied to the results of the currently active
event tasksstatic <U,R> java.util.function.Function<U,R> handleExceptions(java.util.function.Function<U,R> fn)
default <R> SimpleReactStream<R> flatMap(java.util.function.Function<? super U,? extends java.util.stream.Stream<? extends R>> flatFn)
flatFn - Function that coverts a value (e.g. a Collection) into a Streamdefault <R> java.util.List<java.util.concurrent.CompletableFuture<R>> with(java.util.function.Function<U,R> fn)
List<CompletableFuture<Integer>> futures = new SimpleReact().<Integer, Integer> react(() -> 1, () -> 2, () -> 3)
.with((it) -> it * 100);
In this instance, 3 suppliers generate 3 numbers. These may be executed
in parallel, when they complete each number will be multiplied by 100 -
as a separate parrellel task (handled by a ForkJoinPool or configurable
task executor). A List of Future objects will be returned immediately
from Simple React and the tasks will be executed asynchronously.
React with does not block.fn - Function to be applied to the results of the currently active
event tasksdefault SimpleReactStream<U> peek(java.util.function.Consumer<? super U> consumer)
consumer - That will recieve current resultsdefault SimpleReactStream<U> filter(java.util.function.Predicate<? super U> p)
p - Predicate that will be used to filter elements from the
dataflowdefault <T> java.util.stream.Stream<java.util.concurrent.CompletableFuture<T>> streamCompletableFutures()
default SimpleReactStream<U> merge(SimpleReactStream<U> s)
s - Reactive stage builder to merge withstatic <R> SimpleReactStream<R> merge(SimpleReactStream s1, SimpleReactStream s2)
s1 - Reactive stage builder to merges2 - Reactive stage builder to mergedefault <U> SimpleReactStream<U> onFail(java.util.function.Function<? extends SimpleReactFailedStageException,U> fn)
List<String> strings = new SimpleReact().<Integer, Integer> react(() -> 100, () -> 2, () -> 3)
.then(it -> {
if (it == 100)
throw new RuntimeException("boo!");
return it;
})
.onFail(e -> 1)
.then(it -> "*" + it)
.block();
In this example onFail recovers from the RuntimeException thrown when the
input to the first 'then' stage is 100.fn - Recovery function, the exception is input, and the recovery
value is outputstatic SimpleReactFailedStageException assureSimpleReactException(java.lang.Throwable throwable)
default SimpleReactStream<U> capture(java.util.function.Consumer<? extends java.lang.Throwable> errorHandler)
List<String> strings = new SimpleReact().<Integer, Integer> react(() -> 1, () -> 2, () -> 3)
.then(it -> it * 100)
.then(it -> {
if (it == 100)
throw new RuntimeException("boo!");
return it;
})
.onFail(e -> 1)
.then(it -> "*" + it)
.then(it -> {
if ("*200".equals(it))
throw new RuntimeException("boo!");
return it;
})
.capture(e -> logger.error(e.getMessage(),e))
.block();
In this case, strings will only contain the two successful results (for
()->1 and ()->3), an exception for the chain starting from Supplier
()->2 will be logged by capture. Capture will not capture the
exception thrown when an Integer value of 100 is found, but will catch
the exception when the String value "*200" is passed along the chain.errorHandler - A consumer that recieves and deals with an unrecoverable error
in the dataflowdefault <T,R> SimpleReactStream<R> allOf(java.util.function.Function<java.util.List<T>,R> fn)
boolean blocked[] = {false};
new SimpleReact().<Integer, Integer> react(() -> 1, () -> 2, () -> 3)
.then(it -> {
try {
Thread.sleep(50000);
} catch (Exception e) {
}
blocked[0] =true;
return 10;
})
.allOf( it -> it.size());
assertThat(blocked[0],is(false));
In this example, the current thread will continue and assert that it is
not blocked, allOf could continue and be executed in a separate thread.fn - Function that recieves the results of all currently active
tasks as inputstatic <U> SimpleReactStream<U> parallelOf(U... array)
array - Array of value to form the reactive stream / sequencestatic SimpleReact parallelBuilder()
SimpleReact.SimpleReact()static SimpleReact parallelBuilder(int parallelism)
parallelism - Number of threads task executor should havestatic SimpleReact parallelCommonBuilder()
see RetryBuilder#getDefaultInstance()static SimpleReact sequentialBuilder()
static SimpleReact sequentialCommonBuilder()
static SimpleReact simple(java.util.concurrent.ExecutorService executor)
executor - Executor this SimpleReact instance will use to execute
concurrent tasks.static SimpleReact simple(com.nurkiewicz.asyncretry.RetryExecutor retry)
retry - RetryExecutor this SimpleReact instance will use to retry
concurrent tasks.static SimpleReact simple(java.util.concurrent.ExecutorService executor, com.nurkiewicz.asyncretry.RetryExecutor retry)
executor - Executor this SimpleReact instance will use to execute
concurrent tasks.retry - RetryExecutor this SimpleReact instance will use to retry
concurrent tasks.static <T> SimpleReactStream<T> of(T value)
Stream.of(Object)@SafeVarargs static <T> SimpleReactStream<T> of(T... values)
Stream.of(Object[])static <T> SimpleReactStream<T> empty()
Stream.empty()static <T> SimpleReactStream<T> futureStream(java.util.stream.Stream<T> stream)
static <T> SimpleReactStream<T> futureStream(java.util.stream.Stream<T> stream, boolean eager)
static <T> SimpleReactStream<T> futureStream(java.lang.Iterable<T> iterable)
static <T> SimpleReactStream<T> futureStream(java.util.Iterator<T> iterator)