public interface EagerSimpleReactStream<U> extends SimpleReactStream<U>, BlockingStream<U>, ConfigurableStream<U,java.util.concurrent.CompletableFuture<U>>, ToQueue<U>
Modifier and Type | Method and Description |
---|---|
default <T,R> EagerSimpleReactStream<R> |
allOf(java.util.stream.Collector collector,
java.util.function.Function<T,R> fn) |
default <T,R> EagerSimpleReactStream<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> EagerSimpleReactStream<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 EagerSimpleReactStream<U> |
async() |
static <U,R> EagerSimpleReactStream<R> |
bind(EagerSimpleReactStream<U> stream,
java.util.function.Function<U,SimpleReactStream<R>> flatFn)
flatMap / bind implementation that returns the correct type (SimpleReactStream)
|
default <R> R |
block(java.util.stream.Collector collector,
java.util.function.Predicate<Status> breakout) |
default java.util.List<U> |
block(java.util.function.Predicate<Status> breakout)
React and block with breakout
Sometimes you may not need to block until all the work is complete, one
result or a subset may be enough.
|
default EagerSimpleReactStream<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 EagerFutureStream<U> |
convertToEagerStream()
Convert between an Lazy and Eager future stream,
can be used to take advantages of each approach during a single Stream
|
default java.util.List<SimpleReactStream<U>> |
copySimpleReactStream(int times) |
default EagerSimpleReactStream<U> |
doOnEach(java.util.function.Function<U,U> fn)
Applies a function to this phase independent on the main flow.
|
default EagerSimpleReactStream<U> |
doOnEachSync(java.util.function.Function<U,U> fn)
Applies a function to this phase independent on the main flow, continues on the currently executing thread.
|
default EagerSimpleReactStream<U> |
filter(java.util.function.Predicate<? super U> p)
Removes elements that do not match the supplied predicate from the
dataflow
|
default EagerSimpleReactStream<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> EagerSimpleReactStream<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> EagerSimpleReactStream<R> |
flatMapCompletableFuture(java.util.function.Function<U,java.util.concurrent.CompletableFuture<R>> flatFn)
Perform a flatMap operation where the CompletableFuture type returned is flattened from the resulting Stream
If in async mode this operation is performed asyncrhonously
If in sync mode this operation is performed synchronously
|
default <R> EagerSimpleReactStream<R> |
flatMapCompletableFutureSync(java.util.function.Function<U,java.util.concurrent.CompletableFuture<R>> flatFn)
Perform a flatMap operation where the CompletableFuture type returned is flattened from the resulting Stream
This operation is performed synchronously
|
default <R> EagerSimpleReactStream<R> |
fromListCompletableFuture(java.util.List<java.util.concurrent.CompletableFuture<R>> list) |
default <R> EagerSimpleReactStream<R> |
fromStream(java.util.stream.Stream<R> stream) |
default <R> EagerSimpleReactStream<R> |
fromStreamCompletableFutureReplace(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream) |
default <R> EagerSimpleReactStream<R> |
fromStreamOfFutures(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream)
Construct a SimpleReactStream from provided Stream of CompletableFutures
|
EagerStreamWrapper |
getLastActive() |
Continueable |
getSubscription() |
static <U,R> java.util.function.Function<U,R> |
handleExceptions(java.util.function.Function<U,R> fn) |
static <U,R> EagerSimpleReactStream<R> |
join(EagerSimpleReactStream<SimpleReactStream<U>> stream)
flatten nested SimpleReactStreams
|
static java.util.concurrent.CompletableFuture[] |
lastActiveArray(EagerStreamWrapper lastActive) |
default EagerSimpleReactStream<U> |
merge(EagerSimpleReactStream<U>... s) |
static <R> EagerSimpleReactStream<R> |
merge(EagerSimpleReactStream s1,
EagerSimpleReactStream s2)
Merge this reactive dataflow with another - recommended for merging
different types.
|
default EagerSimpleReactStream<U> |
onFail(java.lang.Class<? extends java.lang.Throwable> exceptionClass,
java.util.function.Function<SimpleReactFailedStageException,U> fn)
Recover for a particular class of exceptions only.
|
default EagerSimpleReactStream<U> |
onFail(java.util.function.Function<SimpleReactFailedStageException,U> fn)
React onFail
Define a function that can be used to recover from exceptions during the
preceeding stage of the dataflow.
|
default EagerSimpleReactStream<U> |
peek(java.util.function.Consumer<? super U> consumer)
Peek asynchronously at the results in the current stage.
|
default EagerSimpleReactStream<U> |
peekSync(java.util.function.Consumer<? super U> consumer)
Synchronous peek operator
|
default <R> EagerSimpleReactStream<R> |
retry(java.util.function.Function<U,R> fn)
Will execute this phase on the RetryExecutor (default or user supplied).
|
default <T> java.util.stream.Stream<java.util.concurrent.CompletableFuture<T>> |
streamCompletableFutures() |
default EagerSimpleReactStream<U> |
sync() |
default <R> EagerSimpleReactStream<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> EagerSimpleReactStream<R> |
then(java.util.function.Function<U,R> fn,
java.util.concurrent.Executor service) |
default <R> EagerSimpleReactStream<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.
|
EagerSimpleReactStream<U> |
withAsync(boolean b) |
EagerSimpleReactStream<U> |
withErrorHandler(java.util.Optional<java.util.function.Consumer<java.lang.Throwable>> errorHandler) |
EagerSimpleReactStream<U> |
withLastActive(EagerStreamWrapper streamWrapper) |
EagerSimpleReactStream<U> |
withQueueFactory(QueueFactory<U> queue) |
EagerSimpleReactStream<U> |
withRetrier(com.nurkiewicz.asyncretry.RetryExecutor retry) |
EagerSimpleReactStream<U> |
withSubscription(Continueable sub) |
EagerSimpleReactStream<U> |
withTaskExecutor(java.util.concurrent.Executor e) |
empty, getQueueFactory, getSimpleReact, getTaskExecutor, isAsync, iterator, of, of, ofThread, ofThread, parallel, react, react, simpleReactStream, simpleReactStream, simpleReactStream, simpleReactStream, simpleReactStreamFrom, simpleReactStreamFromIterable, toQueue
block, block, blockAndExtract, blockAndExtract, first, getErrorHandler, last
getErrorHandler, getRetrier, getSimpleReact, getTaskExecutor, isAsync
addToQueue, getQueueFactory, toQueue, toQueue, toQueue
EagerSimpleReactStream<U> withLastActive(EagerStreamWrapper streamWrapper)
EagerStreamWrapper getLastActive()
getLastActive
in interface BlockingStream<U>
getLastActive
in interface SimpleReactStream<U>
EagerSimpleReactStream<U> withTaskExecutor(java.util.concurrent.Executor e)
withTaskExecutor
in interface ConfigurableStream<U,java.util.concurrent.CompletableFuture<U>>
EagerSimpleReactStream<U> withRetrier(com.nurkiewicz.asyncretry.RetryExecutor retry)
withRetrier
in interface ConfigurableStream<U,java.util.concurrent.CompletableFuture<U>>
withRetrier
in interface SimpleReactStream<U>
EagerSimpleReactStream<U> withQueueFactory(QueueFactory<U> queue)
withQueueFactory
in interface ConfigurableStream<U,java.util.concurrent.CompletableFuture<U>>
withQueueFactory
in interface SimpleReactStream<U>
EagerSimpleReactStream<U> withErrorHandler(java.util.Optional<java.util.function.Consumer<java.lang.Throwable>> errorHandler)
withErrorHandler
in interface ConfigurableStream<U,java.util.concurrent.CompletableFuture<U>>
EagerSimpleReactStream<U> withSubscription(Continueable sub)
withSubscription
in interface ConfigurableStream<U,java.util.concurrent.CompletableFuture<U>>
withSubscription
in interface SimpleReactStream<U>
EagerSimpleReactStream<U> withAsync(boolean b)
withAsync
in interface ConfigurableStream<U,java.util.concurrent.CompletableFuture<U>>
Continueable getSubscription()
getSubscription
in interface SimpleReactStream<U>
default java.util.List<SimpleReactStream<U>> copySimpleReactStream(int times)
default <R> EagerSimpleReactStream<R> then(java.util.function.Function<U,R> fn, java.util.concurrent.Executor service)
then
in interface SimpleReactStream<U>
default <R> EagerSimpleReactStream<R> thenSync(java.util.function.Function<U,R> fn)
thenSync
in interface SimpleReactStream<U>
default <T,R> EagerSimpleReactStream<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> EagerSimpleReactStream<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(EagerStreamWrapper lastActive)
default <R> EagerSimpleReactStream<R> retry(java.util.function.Function<U,R> fn)
retry
in interface SimpleReactStream<U>
fn
- Function that will be executed and retried on failuredefault <R> EagerSimpleReactStream<R> fromStream(java.util.stream.Stream<R> stream)
fromStream
in interface SimpleReactStream<U>
default <R> EagerSimpleReactStream<R> fromStreamOfFutures(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream)
stream
- JDK Stream to construct new SimpleReactStream fromdefault <R> EagerSimpleReactStream<R> fromStreamCompletableFutureReplace(java.util.stream.Stream<java.util.concurrent.CompletableFuture<R>> stream)
default <R> EagerSimpleReactStream<R> fromListCompletableFuture(java.util.List<java.util.concurrent.CompletableFuture<R>> list)
default <R> EagerSimpleReactStream<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 SimpleReactStream<U>
fn
- Function to be applied to the results of the currently active
event tasksdefault EagerSimpleReactStream<U> doOnEach(java.util.function.Function<U,U> fn)
doOnEach
in interface SimpleReactStream<U>
fn
- Function to be applied to each completablefuture on completiondefault EagerSimpleReactStream<U> doOnEachSync(java.util.function.Function<U,U> fn)
doOnEachSync
in interface SimpleReactStream<U>
fn
- Function to be applied to each completablefuture on completiondefault EagerSimpleReactStream<U> peek(java.util.function.Consumer<? super U> consumer)
peek
in interface SimpleReactStream<U>
consumer
- That will recieve current resultsdefault EagerSimpleReactStream<U> peekSync(java.util.function.Consumer<? super U> consumer)
peekSync
in interface SimpleReactStream<U>
consumer
- Peek consumerstatic <U,R> java.util.function.Function<U,R> handleExceptions(java.util.function.Function<U,R> fn)
default <R> EagerSimpleReactStream<R> flatMapCompletableFuture(java.util.function.Function<U,java.util.concurrent.CompletableFuture<R>> flatFn)
assertThat( new SimpleReact()
.of(1,2,3)
.flatMapCompletableFuture(i->CompletableFuture.completedFuture(i))
.block(),equalTo(Arrays.asList(1,2,3)));
In this example the result of the flatMapCompletableFuture is 'flattened' to the raw integer valuesflatMapCompletableFuture
in interface SimpleReactStream<U>
flatFn
- flatMap functiondefault <R> EagerSimpleReactStream<R> flatMapCompletableFutureSync(java.util.function.Function<U,java.util.concurrent.CompletableFuture<R>> flatFn)
assertThat( new SimpleReact()
.of(1,2,3)
.flatMapCompletableFutureSync(i->CompletableFuture.completedFuture(i))
.block(),equalTo(Arrays.asList(1,2,3)));
In this example the result of the flatMapCompletableFuture is 'flattened' to the raw integer valuesflatMapCompletableFutureSync
in interface SimpleReactStream<U>
flatFn
- flatMap functiondefault <R> EagerSimpleReactStream<R> flatMap(java.util.function.Function<? super U,? extends java.util.stream.Stream<? extends R>> flatFn)
flatMap
in interface SimpleReactStream<U>
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 EagerSimpleReactStream<U> filter(java.util.function.Predicate<? super U> p)
filter
in interface SimpleReactStream<U>
p
- Predicate that will be used to filter elements from the
dataflowdefault EagerSimpleReactStream<U> filterSync(java.util.function.Predicate<? super U> p)
filterSync
in interface SimpleReactStream<U>
p
- Predicate that will be used to filter elements from the
dataflowdefault <T> java.util.stream.Stream<java.util.concurrent.CompletableFuture<T>> streamCompletableFutures()
streamCompletableFutures
in interface SimpleReactStream<U>
default EagerSimpleReactStream<U> merge(EagerSimpleReactStream<U>... s)
default java.util.List<U> block(java.util.function.Predicate<Status> breakout)
List<String> strings = new SimpleReact().<Integer, Integer> react(() -> 1, () -> 2, () -> 3)
.then(it -> it * 100)
.then(it -> "*" + it)
.block(status -> status.getCompleted()>1);
In this example the current thread will unblock once more than one result
has been returned.breakout
- Predicate that determines whether the block should be
continued or removeddefault <R> R block(java.util.stream.Collector collector, java.util.function.Predicate<Status> breakout)
collector
- to perform aggregation / reduction operation on the results
(e.g. to Collect into a List or String)breakout
- Predicate that determines whether the block should be
continued or removedstatic <R> EagerSimpleReactStream<R> merge(EagerSimpleReactStream s1, EagerSimpleReactStream s2)
s1
- Reactive stage builder to merges2
- Reactive stage builder to mergedefault EagerSimpleReactStream<U> onFail(java.util.function.Function<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.onFail
in interface SimpleReactStream<U>
fn
- Recovery function, the exception is input, and the recovery
value is outputdefault EagerSimpleReactStream<U> onFail(java.lang.Class<? extends java.lang.Throwable> exceptionClass, java.util.function.Function<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 calledonFail
in interface SimpleReactStream<U>
exceptionClass
- Class of exceptions to recover fromfn
- Recovery functionstatic SimpleReactFailedStageException assureSimpleReactException(java.lang.Throwable throwable)
default EagerSimpleReactStream<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.capture
in interface SimpleReactStream<U>
errorHandler
- A consumer that recieves and deals with an unrecoverable error
in the dataflowdefault <T,R> EagerSimpleReactStream<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 inputdefault EagerFutureStream<U> convertToEagerStream()
convertToEagerStream
in interface SimpleReactStream<U>
default EagerSimpleReactStream<U> sync()
sync
in interface SimpleReactStream<U>
default EagerSimpleReactStream<U> async()
async
in interface SimpleReactStream<U>
static <U,R> EagerSimpleReactStream<R> bind(EagerSimpleReactStream<U> stream, java.util.function.Function<U,SimpleReactStream<R>> flatFn)
stream
- Stream to flatMapflatFn
- flatMap functionstatic <U,R> EagerSimpleReactStream<R> join(EagerSimpleReactStream<SimpleReactStream<U>> stream)
stream
- Stream to flatten