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>
public final class StepListener<Response> extends NotifyOnceListener<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);
}
-
Constructor Summary
Constructors Constructor Description StepListener()
-
Method Summary
Modifier and Type Method Description protected void
innerOnFailure(java.lang.Exception e)
protected void
innerOnResponse(Response response)
Response
result()
Gets the result of this step.void
whenComplete(org.elasticsearch.common.CheckedConsumer<Response,java.lang.Exception> onResponse, java.util.function.Consumer<java.lang.Exception> onFailure)
Registers the given actions which are called when this step is completed.
-
Constructor Details
-
StepListener
public StepListener()
-
-
Method Details
-
innerOnResponse
- Specified by:
innerOnResponse
in classNotifyOnceListener<Response>
-
innerOnFailure
protected void innerOnFailure(java.lang.Exception e)- Specified by:
innerOnFailure
in classNotifyOnceListener<Response>
-
whenComplete
public void whenComplete(org.elasticsearch.common.CheckedConsumer<Response,java.lang.Exception> onResponse, java.util.function.Consumer<java.lang.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
-
result
Gets the result of this step. This method will throwIllegalStateException
if this step is not completed yet.
-