Package org.elasticsearch.action
Class StepListener<Response>
- java.lang.Object
-
- org.elasticsearch.action.StepListener<Response>
-
- All Implemented Interfaces:
ActionListener<Response>
public final class StepListener<Response> extends java.lang.Object implements ActionListener<Response>
AStepListenerprovides 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 voidonFailure(java.lang.Exception e)A failure caused by an exception at some phase of the task.voidonResponse(Response response)Handle action response.Responseresult()Gets the result of this step.voidwhenComplete(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.
-
-
-
Method Detail
-
onResponse
public void onResponse(Response response)
Description copied from interface:ActionListenerHandle action response. This response may constitute a failure or a success but it is up to the listener to make that decision.- Specified by:
onResponsein interfaceActionListener<Response>
-
onFailure
public void onFailure(java.lang.Exception e)
Description copied from interface:ActionListenerA failure caused by an exception at some phase of the task.- Specified by:
onFailurein interfaceActionListener<Response>
-
whenComplete
public void whenComplete(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, theonResponseis called with the result; otherwise theonFailureis 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
public Response result()
Gets the result of this step. This method will throwIllegalStateExceptionif this step is not completed yet.
-
-