public interface SimpleReactStream<U> extends BlockingStream<U>, ConfigurableStream.SimpleReactConfigurableStream<U>, ToQueue<U>
ConfigurableStream.SimpleReactConfigurableStream<U>
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.
|
default <R> SimpleReactStream<R> |
anyOf(java.util.function.Function<U,R> fn)
React to the completion of any of the events in the previous stage.
|
static SimpleReactFailedStageException |
assureSimpleReactException(java.lang.Throwable throwable) |
default SimpleReactStream<U> |
async() |
static <U,R> SimpleReactStream<R> |
bind(SimpleReactStream<U> stream,
java.util.function.Function<U,SimpleReactStream<R>> flatFn)
flatMap / bind implementation that returns the correct type (SimpleReactStream)
|
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.
|
default SimpleReactStream<U> |
doOnEach(java.util.function.Function<U,U> fn)
Can only be used on Eager Streams
Applies a function to this phase independent on the main flow.
|
default SimpleReactStream<U> |
doOnEachSync(java.util.function.Function<U,U> fn) |
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 SimpleReactStream<U> |
filterSync(java.util.function.Predicate<? super U> p)
Synchronous filtering operation
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> |
fromListCompletableFuture(java.util.List<java.util.concurrent.CompletableFuture<R>> list) |
default <R> SimpleReactStream<R> |
fromStream(java.util.stream.Stream<R> stream) |
default <R> SimpleReactStream<R> |
fromStreamCompletableFutureReplace(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream) |
default <R> SimpleReactStream<R> |
fromStreamOfFutures(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream)
Construct a SimpleReactStream from provided Stream of CompletableFutures
|
java.util.List |
getOriginalFutures() |
Continueable |
getSubscription() |
static <U,R> java.util.function.Function<U,R> |
handleExceptions(java.util.function.Function<U,R> fn) |
static <U,R> SimpleReactStream<R> |
join(SimpleReactStream<SimpleReactStream<U>> stream)
flatten nested SimpleReactStreams
|
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(java.util.Iterator<T> iterator)
Wrap an Iterator into a FutureStream.
|
static <T> SimpleReactStream<T> |
of(java.util.stream.Stream<T> stream)
Wrap a Stream into a SimpleReactStream.
|
static <T> SimpleReactStream<T> |
of(T... values) |
static <T> SimpleReactStream<T> |
of(T value) |
static <T> SimpleReactStream<T> |
ofIterable(java.lang.Iterable<T> iterable)
Wrap an Iterable into a FutureStream.
|
default SimpleReactStream<U> |
onFail(java.lang.Class<? extends java.lang.Throwable> exceptionClass,
java.util.function.Function<? extends SimpleReactFailedStageException,U> fn)
Recover for a particular class of exceptions only.
|
default 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 SimpleReactStream<U> |
peekSync(java.util.function.Consumer<? super U> consumer)
Synchronous peek operator
|
static <T> SimpleReactStream<T> |
react(java.util.function.Supplier<T>... values)
Create a parallel asynchronous stream
|
static <T> SimpleReactStream<T> |
react(java.util.function.Supplier<T> value)
Create a parallel asynchronous stream
|
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() |
default <T> java.util.stream.Stream<java.util.concurrent.CompletableFuture<T>> |
streamCompletableFutures() |
default SimpleReactStream<U> |
sync() |
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> SimpleReactStream<R> |
then(java.util.function.Function<U,R> fn,
java.util.concurrent.Executor service) |
default <R> SimpleReactStream<R> |
thenSync(java.util.function.Function<U,R> fn) |
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.
|
block, block, block, block, blockAndExtract, blockAndExtract, first, last
withAsync, withErrorHandler, withLastActive, withLazyCollector, withQueueFactory, withRetrier, withSubscription, withTaskExecutor, withWaitStrategy
getErrorHandler, getLastActive, getRetrier, getSimpleReact, getTaskExecutor, isAsync, isEager
getQueueFactory, toQueue, toQueue, toQueue
Continueable getSubscription()
default <R> SimpleReactStream<R> then(java.util.function.Function<U,R> fn, java.util.concurrent.Executor service)
default <R> SimpleReactStream<R> thenSync(java.util.function.Function<U,R> fn)
default <T,R> SimpleReactStream<R> allOf(java.util.stream.Collector collector, java.util.function.Function<T,R> fn)
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 inputdefault <R> SimpleReactStream<R> anyOf(java.util.function.Function<U,R> fn)
fn
- Function to apply when any of the previous events completestatic 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> fromStreamOfFutures(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream)
stream
- JDK Stream to construct new SimpleReactStream fromdefault <R> SimpleReactStream<R> fromStreamCompletableFutureReplace(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream)
default <R> SimpleReactStream<R> fromListCompletableFuture(java.util.List<java.util.concurrent.CompletableFuture<R>> list)
default <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 fn
- Function to be applied to the results of the currently active
event tasksdefault SimpleReactStream<U> doOnEach(java.util.function.Function<U,U> fn)
fn
- Function to be applied to each completablefuture on completiondefault SimpleReactStream<U> doOnEachSync(java.util.function.Function<U,U> fn)
default SimpleReactStream<U> peek(java.util.function.Consumer<? super U> consumer)
consumer
- That will recieve current resultsdefault SimpleReactStream<U> peekSync(java.util.function.Consumer<? super U> consumer)
consumer
- Peek consumerstatic <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 Streamstatic <U,R> SimpleReactStream<R> bind(SimpleReactStream<U> stream, java.util.function.Function<U,SimpleReactStream<R>> flatFn)
stream
- Stream to flatMapflatFn
- flatMap functionstatic <U,R> SimpleReactStream<R> join(SimpleReactStream<SimpleReactStream<U>> stream)
stream
- Stream to flattenjava.util.List getOriginalFutures()
default <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> filter(java.util.function.Predicate<? super U> p)
p
- Predicate that will be used to filter elements from the
dataflowdefault SimpleReactStream<U> filterSync(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 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 outputdefault SimpleReactStream<U> onFail(java.lang.Class<? extends java.lang.Throwable> exceptionClass, java.util.function.Function<? extends SimpleReactFailedStageException,U> fn)
onFail(IOException.class, recoveryFunction1)
.onFail(Throwable.class,recovertyFunction2)
For an IOException recoveryFunction1 will be executed
but with the definitions reveresed
onFail(Throwable.class,recovertyFunction2)
.onFail(IOException.class, recoveryFunction1)
recoveryFunction1 will not be calledexceptionClass
- Class of exceptions to recover fromfn
- Recovery functionstatic 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 <T> SimpleReactStream<T> react(java.util.function.Supplier<T> value)
Stream.of(Object)
@SafeVarargs static <T> SimpleReactStream<T> react(java.util.function.Supplier<T>... values)
Stream.of(Object[])
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> of(java.util.stream.Stream<T> stream)
static <T> SimpleReactStream<T> ofIterable(java.lang.Iterable<T> iterable)
static <T> SimpleReactStream<T> of(java.util.Iterator<T> iterator)
default SimpleReactStream<U> sync()
default SimpleReactStream<U> async()