Interface FlowListener


@NoImplement @MinMuleVersion("4.5.0") public interface FlowListener
Allows to execute custom logic when the flow on which an operation is being executed finishes.

Operation can declare an argument of this type and the runtime will automatically inject an implementation which the operation can use.

An example use case is an operation which needs to await for the owning flow to finish in order to execute some clean-up, or wants to know the final outcome in order to log it or audit it.

For example, let's see a very simple logging case


  public void listeningOperation(FlowListener listener) {
    listener.onSuccess(message -> LOGGER.debug("Response obtained", message.getPayload().getValue()));
    listener.onError(exception -> LOGGER.debug("Flow failed", exception));
    listener.onComplete(() -> doCleanUp());
    }
  }
 

Instances are not reusable and should not be cached. Instances are also not thread-safe. No instance should be used in a thread different from the one executing the operation.

Since:
1.0
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Executes the given handler when the Event on which the operation was executed is terminated.
    void
    Executes the given handler when the flow that owns the listening operation fails.
    void
    onSuccess(Consumer<org.mule.runtime.api.message.Message> handler)
    Executes the given handler when the flow that owns the listening operation finishes.
  • Method Details

    • onSuccess

      void onSuccess(Consumer<org.mule.runtime.api.message.Message> handler)
      Executes the given handler when the flow that owns the listening operation finishes.

      If the method is invoked several times on the same instance, the last handler wins and the prior ones get discarded.

      The handler should be written in a way in which it doesn't fail. If it does fail, the runtime will log and discard the error. Be a good citizen, do not put the runtime into that situation.

      Parameters:
      handler - a Message Consumer
    • onError

      void onError(Consumer<Exception> handler)
      Executes the given handler when the flow that owns the listening operation fails.

      If the method is invoked several times on the same instance, the last handler wins and the prior ones get discarded.

      The handler should be written in a way in which it doesn't fail. If it does fail, the runtime will log and discard the error. Be a good citizen, do not put the runtime into that situation.

      Parameters:
      handler - an Exception Consumer
    • onComplete

      void onComplete(Runnable handler)
      Executes the given handler when the Event on which the operation was executed is terminated.

      Unlike onSuccess(Consumer) and onError(Consumer), this one doesn't depend on the flow's response but in the completion of the event. This includes all other asynchronous tasks, flow-ref invocations, etc.

      Since this method is equivalent to a java finally block, it is ideal for performing cleanup and resource deallocation tasks.

      The handler should be written in a way in which it doesn't fail. If it does fail, the runtime will log and discard the error. Be a good citizen, do not put the runtime into that situation.

      Parameters:
      handler -