Class 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 new Multi invoking the given @{code action} when a failure event is received.
      Multi<T> call​(java.util.function.Supplier<Uni<?>> action)
      Produces a new Multi invoking the given @{code action} when a failure event is received.
      Multi<T> invoke​(java.lang.Runnable callback)
      Produces a new Multi invoking the given callback when the upstream Multi emits a failure (matching the predicate if set), and ignoring the failure in the callback.
      Multi<T> invoke​(java.util.function.Consumer<java.lang.Throwable> callback)
      Produces a new Multi invoking the given callback when the upstream Multi emits a failure (matching the predicate if set).
      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 given Multi.
      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 another Multi.
      Multi<T> recoverWithMulti​(java.util.function.Supplier<Multi<? extends T>> supplier)
      Recovers from the received failure (matching the predicate if set) with another Multi.
      MultiRetry<T> retry()
      Configures the retry strategy.
      Multi<T> transform​(java.util.function.Function<? super java.lang.Throwable,​? extends java.lang.Throwable> mapper)
      Produces a new Multi invoking the given function when the current Multi propagates a failure.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MultiOnFailure

        public MultiOnFailure​(Multi<T> upstream,
                              java.util.function.Predicate<? super java.lang.Throwable> predicate)
    • Method Detail

      • invoke

        @CheckReturnValue
        public Multi<T> invoke​(java.util.function.Consumer<java.lang.Throwable> callback)
        Produces a new Multi invoking the given callback when the upstream Multi 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 be null
        Returns:
        the new Multi
      • invoke

        @CheckReturnValue
        public Multi<T> invoke​(java.lang.Runnable callback)
        Produces a new Multi invoking the given callback when the upstream Multi 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 be null
        Returns:
        the new Multi
      • call

        @CheckReturnValue
        public Multi<T> call​(java.util.function.Function<java.lang.Throwable,​Uni<?>> action)
        Produces a new Multi invoking the given @{code action} when a failure event is received.

        Unlike invoke(Consumer), the passed function returns a Uni. When the produced Uni sends its result, the result is discarded, and the original failure is forwarded downstream. If the produced Uni fails, a CompositeException 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.

        Parameters:
        action - the function taking the failure and returning a Uni, must not be null
        Returns:
        the new Multi
      • call

        @CheckReturnValue
        public Multi<T> call​(java.util.function.Supplier<Uni<?>> action)
        Produces a new Multi invoking the given @{code action} when a failure event is received.

        Unlike invoke(Consumer), the passed function returns a Uni. When the produced Uni sends its result, the result is discarded, and the original failure is forwarded downstream. If the produced Uni fails, a CompositeException 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.

        Parameters:
        action - the supplier returning a Uni, must not be null
        Returns:
        the new Multi
      • transform

        @CheckReturnValue
        public Multi<T> transform​(java.util.function.Function<? super java.lang.Throwable,​? extends java.lang.Throwable> mapper)
        Produces a new Multi invoking the given function when the current Multi 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 be null, must not return null
        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 be null
        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 be null, must not return null.
        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 be null, must not return null.
        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 another Multi. This multi is produced by the given function. The function must not return null.

        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 be null, must not return null.
        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 another Multi. This multi is produced by the given function. The supplier must not return null.

        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 be null, must not return null.
        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 given Multi.

        In case of failure, the downstream switches to the given Multi.

        Parameters:
        fallback - the fallback Multi. Must not be null.
        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.