Package org.elasticsearch.action
Class StepListener<Response>
java.lang.Object
org.elasticsearch.action.NotifyOnceListener<Response>
org.elasticsearch.action.StepListener<Response>
- All Implemented Interfaces:
ActionListener<Response>
A
StepListener
provides a simple way to write a flow consisting of
multiple asynchronous steps without having nested callbacks. For example:
void asyncFlowMethod(... ActionListener<R> flowListener) {
StepListener<R1> step1 = new StepListener<>();
asyncStep1(..., step1);
StepListener<R2> step2 = new StepListener<>();
step1.whenComplete(r1 -> {
asyncStep2(r1, ..., step2);
}, flowListener::onFailure);
step2.whenComplete(r2 -> {
R1 r1 = step1.result();
R r = combine(r1, r2);
flowListener.onResponse(r);
}, flowListener::onFailure);
}
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.elasticsearch.action.ActionListener
ActionListener.Delegating<Response,DelegateResponse>, ActionListener.DelegatingActionListener<T>, ActionListener.DelegatingFailureActionListener<T,R>, ActionListener.MappedActionListener<Response,MappedResponse>, ActionListener.RunAfterActionListener<T>, ActionListener.RunBeforeActionListener<T>
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener(ActionListener<Response> listener)
Registers the given listener to be notified with the result of this step.asFuture()
Returns the future associated with the given step listenerprotected void
protected void
innerOnResponse(Response response)
result()
Gets the result of this step.<OtherResponse, OuterResponse>
StepListener<OuterResponse>thenCombine(StepListener<OtherResponse> other, BiFunction<Response,OtherResponse,OuterResponse> fn)
Combines this listener with another one, waiting for both to successfully complete and combining their results.void
whenComplete(org.elasticsearch.core.CheckedConsumer<Response,Exception> onResponse, Consumer<Exception> onFailure)
Registers the given actions which are called when this step is completed.Methods inherited from class org.elasticsearch.action.NotifyOnceListener
onFailure, onResponse
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.elasticsearch.action.ActionListener
delegateFailure, delegateResponse, map
-
Constructor Details
-
StepListener
public StepListener()
-
-
Method Details
-
innerOnResponse
- Specified by:
innerOnResponse
in classNotifyOnceListener<Response>
-
innerOnFailure
- Specified by:
innerOnFailure
in classNotifyOnceListener<Response>
-
whenComplete
public void whenComplete(org.elasticsearch.core.CheckedConsumer<Response,Exception> onResponse, Consumer<Exception> onFailure)Registers the given actions which are called when this step is completed. If this step is completed successfully, theonResponse
is called with the result; otherwise theonFailure
is called with the failure.- Parameters:
onResponse
- is called when this step is completed successfullyonFailure
- is called when this step is completed with a failure
-
thenCombine
public <OtherResponse, OuterResponse> StepListener<OuterResponse> thenCombine(StepListener<OtherResponse> other, BiFunction<Response,OtherResponse,OuterResponse> fn)Combines this listener with another one, waiting for both to successfully complete and combining their results.- Parameters:
other
- the other step listener to combine withfn
- the function that combines the results- Returns:
- the combined listener
-
asFuture
Returns the future associated with the given step listener -
result
Gets the result of this step. This method will throwIllegalStateException
if this step is not completed yet. -
addListener
Registers the given listener to be notified with the result of this step.
-