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 TypeMethodDescriptionvoidonComplete(Runnable handler) Executes the givenhandlerwhen theEventon which the operation was executed is terminated.voidExecutes the givenhandlerwhen the flow that owns the listening operation fails.voidExecutes the givenhandlerwhen the flow that owns the listening operation finishes.
-
Method Details
-
onSuccess
Executes the givenhandlerwhen 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
handlershould 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- aMessageConsumer
-
onError
Executes the givenhandlerwhen 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
handlershould 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. -
onComplete
Executes the givenhandlerwhen theEventon which the operation was executed is terminated.Unlike
onSuccess(Consumer)andonError(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
finallyblock, it is ideal for performing cleanup and resource deallocation tasks.The
handlershould 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-
-