Class MultiOnFailure<T>
- java.lang.Object
-
- io.smallrye.mutiny.groups.MultiOnFailure<T>
-
- Type Parameters:
T
- the type of item
public class MultiOnFailure<T> extends java.lang.Object
Configures the failure handler.The upstream multi has sent us a failure, this class lets you decide what need to be done in this case. Typically, you can recover with a fallback item (
recoverWithItem(Object)
), or with another Multi (recoverWithMulti(Multi)
), or simply completes the stream (recoverWithCompletion()
). You can also retry (retry()
). Maybe, you just want to look at the failure (invoke(Consumer)
).You can configure the type of failure on which your handler is called using:
multi.onFailure(IOException.class).recoverWithItem("boom") multi.onFailure(IllegalStateException.class).recoverWithItem("kaboom") multi.onFailure(NoMoreDataException.class).recoverWithCompletion() multi.onFailure(t -> accept(t)).recoverWithItem("another boom")
-
-
Constructor Summary
Constructors Constructor Description MultiOnFailure(Multi<T> upstream, java.util.function.Predicate<? super java.lang.Throwable> predicate)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Multi<T>
call(java.util.function.Function<java.lang.Throwable,Uni<?>> action)
Produces a newMulti
invoking the given @{code action} when afailure
event is received.Multi<T>
call(java.util.function.Supplier<Uni<?>> action)
Produces a newMulti
invoking the given @{code action} when afailure
event is received.Multi<T>
invoke(java.lang.Runnable callback)
Multi<T>
invoke(java.util.function.Consumer<java.lang.Throwable> callback)
Multi<T>
recoverWithCompletion()
Recovers from the received failure (matching the predicate if set) by completing the stream.Multi<T>
recoverWithItem(java.util.function.Function<? super java.lang.Throwable,? extends T> function)
Recovers from the received failure (matching the predicate if set) by using a item generated by the given function.Multi<T>
recoverWithItem(java.util.function.Supplier<T> supplier)
Recovers from the received failure (matching the predicate if set) by using a item generated by the given supplier.Multi<T>
recoverWithItem(T fallback)
Recovers from the received failure (matching the predicate if set) by using a fallback item.Multi<T>
recoverWithMulti(Multi<? extends T> fallback)
Recovers from the received failure (matching the predicate if set) with another givenMulti
.Multi<T>
recoverWithMulti(java.util.function.Function<? super java.lang.Throwable,Multi<? extends T>> function)
Recovers from the received failure (matching the predicate if set) with anotherMulti
.Multi<T>
recoverWithMulti(java.util.function.Supplier<Multi<? extends T>> supplier)
Recovers from the received failure (matching the predicate if set) with anotherMulti
.MultiRetry<T>
retry()
Configures the retry strategy.Multi<T>
transform(java.util.function.Function<? super java.lang.Throwable,? extends java.lang.Throwable> mapper)
-
-
-
Method Detail
-
invoke
@CheckReturnValue public Multi<T> invoke(java.util.function.Consumer<java.lang.Throwable> callback)
Produces a newMulti
invoking the given callback when the upstreamMulti
emits a failure (matching the predicate if set).If the callback throws an exception, a
CompositeException
is propagated downstream. This exception is composed by the received failure and the thrown exception.- Parameters:
callback
- the callback, must not benull
- Returns:
- the new
Multi
-
invoke
@CheckReturnValue public Multi<T> invoke(java.lang.Runnable callback)
Produces a newMulti
invoking the given callback when the upstreamMulti
emits a failure (matching the predicate if set), and ignoring the failure in the callback.If the callback throws an exception, a
CompositeException
is propagated downstream. This exception is composed by the received failure and the thrown exception.- Parameters:
callback
- the callback, must not benull
- Returns:
- the new
Multi
-
call
@CheckReturnValue public Multi<T> call(java.util.function.Function<java.lang.Throwable,Uni<?>> action)
Produces a newMulti
invoking the given @{code action} when afailure
event is received.Unlike
invoke(Consumer)
, the passed function returns aUni
. When the producedUni
sends its result, the result is discarded, and the originalfailure
is forwarded downstream. If the producedUni
fails, aCompositeException
composed by the original failure and the caught failure is propagated downstream.If the asynchronous action throws an exception, this exception is propagated downstream as a
CompositeException
composed with the original failure and the caught exception.This method preserves the order of the items, meaning that the downstream received the items in the same order as the upstream has emitted them.
-
call
@CheckReturnValue public Multi<T> call(java.util.function.Supplier<Uni<?>> action)
Produces a newMulti
invoking the given @{code action} when afailure
event is received.Unlike
invoke(Consumer)
, the passed function returns aUni
. When the producedUni
sends its result, the result is discarded, and the originalfailure
is forwarded downstream. If the producedUni
fails, aCompositeException
composed by the original failure and the caught failure is propagated downstream.If the asynchronous action throws an exception, this exception is propagated downstream as a
CompositeException
composed with the original failure and the caught exception.This method preserves the order of the items, meaning that the downstream received the items in the same order as the upstream has emitted them.
-
transform
@CheckReturnValue public Multi<T> transform(java.util.function.Function<? super java.lang.Throwable,? extends java.lang.Throwable> mapper)
Produces a newMulti
invoking the given function when the currentMulti
propagates a failure. The function can transform the received failure into another exception that will be fired as failure downstream.- Parameters:
mapper
- the mapper function, must not benull
, must not returnnull
- Returns:
- the new
Multi
-
recoverWithItem
@CheckReturnValue public Multi<T> recoverWithItem(T fallback)
Recovers from the received failure (matching the predicate if set) by using a fallback item.- Parameters:
fallback
- the fallback, can benull
- Returns:
- the new
Multi
that would emit the given fallback in case the upstream sends us a failure.
-
recoverWithItem
@CheckReturnValue public Multi<T> recoverWithItem(java.util.function.Supplier<T> supplier)
Recovers from the received failure (matching the predicate if set) by using a item generated by the given supplier. The supplier is called when the failure is received.If the supplier throws an exception, a
CompositeException
containing both the received failure and the thrown exception is propagated downstream.- Parameters:
supplier
- the supplier providing the fallback item. Must not benull
, must not returnnull
.- Returns:
- the new
Multi
that would emit the produced item in case the upstream sends a failure.
-
recoverWithItem
@CheckReturnValue public Multi<T> recoverWithItem(java.util.function.Function<? super java.lang.Throwable,? extends T> function)
Recovers from the received failure (matching the predicate if set) by using a item generated by the given function. The function is called when the failure is received.If the function throws an exception, a
CompositeException
containing both the received failure and the thrown exception is propagated downstream.- Parameters:
function
- the function providing the fallback item. Must not benull
, must not returnnull
.- Returns:
- the new
Multi
that would emit the produced item in case the upstream sends a failure.
-
recoverWithCompletion
@CheckReturnValue public Multi<T> recoverWithCompletion()
Recovers from the received failure (matching the predicate if set) by completing the stream. Upon failure, it sends the completion event downstream.- Returns:
- the new
Multi
that would complete in case the upstream sends a failure.
-
recoverWithMulti
@CheckReturnValue public Multi<T> recoverWithMulti(java.util.function.Function<? super java.lang.Throwable,Multi<? extends T>> function)
Recovers from the received failure (matching the predicate if set) with anotherMulti
. Thismulti
is produced by the given function. The function must not returnnull
.In case of failure, the downstream switches to the produced
Multi
.If the function throws an exception, a
CompositeException
containing both the received failure and the thrown exception is propagated downstream.- Parameters:
function
- the function providing the fallback Multi. Must not benull
, must not returnnull
.- Returns:
- the new
Multi
that would emit events from the multi produced by the given function in case the upstream sends a failure.
-
recoverWithMulti
@CheckReturnValue public Multi<T> recoverWithMulti(java.util.function.Supplier<Multi<? extends T>> supplier)
Recovers from the received failure (matching the predicate if set) with anotherMulti
. Thismulti
is produced by the given function. The supplier must not returnnull
.In case of failure, the downstream switches to the produced
Multi
.If the supplier throws an exception, a
CompositeException
containing both the received failure and the thrown exception is propagated downstream.- Parameters:
supplier
- the function supplier the fallback Multi. Must not benull
, must not returnnull
.- Returns:
- the new
Multi
that would emit events from the multi produced by the given supplier in case the upstream sends a failure.
-
recoverWithMulti
@CheckReturnValue public Multi<T> recoverWithMulti(Multi<? extends T> fallback)
Recovers from the received failure (matching the predicate if set) with another givenMulti
.In case of failure, the downstream switches to the given
Multi
.- Parameters:
fallback
- the fallback Multi. Must not benull
.- Returns:
- the new
Multi
that would emit events from the passed multi in case the upstream sends a failure.
-
retry
@CheckReturnValue public MultiRetry<T> retry()
Configures the retry strategy.- Returns:
- the object to configure the retry.
-
-