- All Known Subinterfaces:
PersistentTasksService.WaitForPersistentTaskListener<P>
,RejectAwareActionListener<T>
- All Known Implementing Classes:
AbstractThreadedActionListener
,ChannelActionListener
,ContextPreservingActionListener
,CountDownActionListener
,DelegatingActionListener
,FinalizeSnapshotContext
,GetSnapshotInfoContext
,GroupedActionListener
,LatchedActionListener
,ListenableActionFuture
,ListenableFuture
,LoggingTaskListener
,PlainActionFuture
,RefCountAwareThreadedActionListener
,RestActionListener
,RestActions.NodesResponseRestListener
,RestBuilderListener
,RestChunkedToXContentListener
,RestRefCountedChunkedToXContentListener
,RestResponseListener
,RestToXContentListener
,SearchExecutionStatsCollector
,SearchProgressActionListener
,SnapshotShardContext
,SubscribableListener
,ThreadedActionListener
public interface ActionListener<Response>
A listener for action responses or failures.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <Response> ActionListener<Response>
assertOnce
(ActionListener<Response> delegate) static <Response> void
completeWith
(ActionListener<Response> listener, CheckedSupplier<Response, ? extends Exception> supplier) Completes the given listener with the result from the provided supplier accordingly.default <T> ActionListener<T>
Creates a new listener, wrapping this one, that overridesonResponse(Response)
handling with the givenbc
consumer.default <T> ActionListener<T>
delegateFailureAndWrap
(CheckedBiConsumer<ActionListener<Response>, T, ? extends Exception> bc) Same asdelegateFailure(BiConsumer)
except that any failure thrown bybc
or the original listener'sonResponse(Response)
will be passed to the original listener'sonFailure(Exception)
.default ActionListener<Response>
Creates a listener that delegates all responses it receives to this instance.default <T> ActionListener<T>
map
(CheckedFunction<T, Response, Exception> fn) Creates a listener that wraps this listener, mapping response values via the given mapping function and passing along exceptions to this instance.static <T> ActionListener<T>
noop()
static <Response> ActionListener<Response>
notifyOnce
(ActionListener<Response> delegate) Wraps a given listener and returns a new listener which makes sureonResponse(Object)
andonFailure(Exception)
of the provided listener will be called at most once.void
A failure caused by an exception at some phase of the task.static <Response> void
onFailure
(Iterable<ActionListener<Response>> listeners, Exception failure) Notifies every given listener with the failure passed toonFailure(Exception)
.static <Response> void
onResponse
(Iterable<ActionListener<Response>> listeners, Response response) Notifies every given listener with the response passed toonResponse(Object)
.void
onResponse
(Response response) Handle action response.static <Response> ActionListener<Response>
releaseAfter
(ActionListener<Response> delegate, Releasable releaseAfter) Wraps a given listener and returns a new listener which releases the providedreleaseAfter
resource when the listener is notified via either#onResponse
or#onFailure
.static <Response> ActionListener<Response>
releasing
(Releasable releasable) Creates a listener which releases the given resource on completion (whether success or failure)static <R extends RefCounted>
voidrespondAndRelease
(ActionListener<R> listener, R response) Shorthand for resolving givenlistener
with givenresponse
and decrementing the response's ref count by one afterwards.static <T,
L extends ActionListener<T>>
voidrun
(L listener, CheckedConsumer<L, ? extends Exception> action) Execute the given action in atry/catch
block which feeds all exceptions to the given listener'sonFailure(java.lang.Exception)
method.static <Response> ActionListener<Response>
runAfter
(ActionListener<Response> delegate, Runnable runAfter) Wraps a given listener and returns a new listener which executes the providedrunAfter
callback when the listener is notified via either#onResponse
or#onFailure
.static <Response> ActionListener<Response>
runBefore
(ActionListener<Response> delegate, CheckedRunnable<?> runBefore) Wraps a given listener and returns a new listener which executes the providedrunBefore
callback before the listener is notified via either#onResponse
or#onFailure
.static <Response> ActionListener<Response>
Creates a listener that executes the given runnable on completion (whether successful or otherwise).static <T,
R extends AutoCloseable>
voidrunWithResource
(ActionListener<T> listener, CheckedSupplier<R, ? extends Exception> resourceSupplier, CheckedBiConsumer<ActionListener<T>, R, ? extends Exception> action) Execute the given action in an (async equivalent of a) try-with-resources block which closes the supplied resource on completion, and feeds all exceptions to the given listener'sonFailure(java.lang.Exception)
method.default <T> ActionListener<T>
Same asmap(CheckedFunction)
except thatfn
is expected to never throw.static <Response> ActionListener<Response>
wrap
(CheckedConsumer<Response, ? extends Exception> onResponse, Consumer<Exception> onFailure) Creates a listener that executes the appropriate consumer when the response (or failure) is received.
-
Method Details
-
onResponse
Handle action response. This response may constitute a failure or a success but it is up to the listener to make that decision. -
onFailure
A failure caused by an exception at some phase of the task. -
noop
- Returns:
- a listener that does nothing
-
map
Creates a listener that wraps this listener, mapping response values via the given mapping function and passing along exceptions to this instance. Notice that it is considered a bug if the listener's onResponse or onFailure fails. onResponse failures will not call onFailure. If the function fails, the listener's onFailure handler will be called. The principle is that the mapped listener will handle exceptions from the mapping functionfn
but it is the responsibility ofdelegate
to handle its own exceptions inside `onResponse` and `onFailure`.- Type Parameters:
T
- Response type of the wrapped listener- Parameters:
fn
- Function to apply to listener response- Returns:
- a listener that maps the received response and then passes it to this instance
-
safeMap
Same asmap(CheckedFunction)
except thatfn
is expected to never throw. -
delegateResponse
default ActionListener<Response> delegateResponse(BiConsumer<ActionListener<Response>, Exception> bc) Creates a listener that delegates all responses it receives to this instance.- Parameters:
bc
- BiConsumer invoked with delegate listener and exception- Returns:
- Delegating listener
-
delegateFailure
Creates a new listener, wrapping this one, that overridesonResponse(Response)
handling with the givenbc
consumer.onFailure(Exception)
handling is delegated to the original listener. Exceptions inonResponse(Response)
are forbidden.- Type Parameters:
T
- Type of the delegating listener's response- Parameters:
bc
-BiConsumer
invoked viaonResponse(Response)
with the original listener and the response with which the new listener was completed.- Returns:
- a new listener that delegates failures to this listener and runs
bc
on a response.
-
delegateFailureAndWrap
default <T> ActionListener<T> delegateFailureAndWrap(CheckedBiConsumer<ActionListener<Response>, T, ? extends Exception> bc) Same asdelegateFailure(BiConsumer)
except that any failure thrown bybc
or the original listener'sonResponse(Response)
will be passed to the original listener'sonFailure(Exception)
. -
releasing
Creates a listener which releases the given resource on completion (whether success or failure) -
running
Creates a listener that executes the given runnable on completion (whether successful or otherwise).- Type Parameters:
Response
- the type of the response, which is ignored.- Parameters:
runnable
- the runnable that will be called in event of success or failure. This must not throw.- Returns:
- a listener that executes the given runnable on completion (whether successful or otherwise).
-
wrap
static <Response> ActionListener<Response> wrap(CheckedConsumer<Response, ? extends Exception> onResponse, Consumer<Exception> onFailure) Creates a listener that executes the appropriate consumer when the response (or failure) is received. This listener is "wrapped" in the sense that an exception from theonResponse
consumer is passed into theonFailure
consumer.If the
onFailure
argument islistener::onFailure
for some otherActionListener
, prefer to usedelegateFailureAndWrap(org.elasticsearch.common.CheckedBiConsumer<org.elasticsearch.action.ActionListener<Response>, T, ? extends java.lang.Exception>)
instead for performance reasons.- Type Parameters:
Response
- the type of the response- Parameters:
onResponse
- the checked consumer of the response, executed when the listener is completed successfully. If it throws an exception, the exception is passed to theonFailure
consumer.onFailure
- the consumer of the failure, executed when the listener is completed with an exception (or it is completed successfully but theonResponse
consumer threw an exception).- Returns:
- a listener that executes the appropriate consumer when the response (or failure) is received.
-
onResponse
Notifies every given listener with the response passed toonResponse(Object)
. If a listener itself throws an exception the exception is forwarded toonFailure(Exception)
. If in turnonFailure(Exception)
fails all remaining listeners will be processed and the caught exception will be re-thrown. -
onFailure
Notifies every given listener with the failure passed toonFailure(Exception)
. If a listener itself throws an exception all remaining listeners will be processed and the caught exception will be re-thrown. -
runAfter
static <Response> ActionListener<Response> runAfter(ActionListener<Response> delegate, Runnable runAfter) Wraps a given listener and returns a new listener which executes the providedrunAfter
callback when the listener is notified via either#onResponse
or#onFailure
. -
releaseAfter
static <Response> ActionListener<Response> releaseAfter(ActionListener<Response> delegate, Releasable releaseAfter) Wraps a given listener and returns a new listener which releases the providedreleaseAfter
resource when the listener is notified via either#onResponse
or#onFailure
. -
runBefore
static <Response> ActionListener<Response> runBefore(ActionListener<Response> delegate, CheckedRunnable<?> runBefore) Wraps a given listener and returns a new listener which executes the providedrunBefore
callback before the listener is notified via either#onResponse
or#onFailure
. If the callback throws an exception then it will be passed to the listener's#onFailure
and its#onResponse
will not be executed. -
notifyOnce
Wraps a given listener and returns a new listener which makes sureonResponse(Object)
andonFailure(Exception)
of the provided listener will be called at most once. -
completeWith
static <Response> void completeWith(ActionListener<Response> listener, CheckedSupplier<Response, ? extends Exception> supplier) Completes the given listener with the result from the provided supplier accordingly. This method is mainly used to complete a listener with a block of synchronous code. If the supplier fails, the listener's onFailure handler will be called. It is the responsibility ofdelegate
to handle its own exceptions inside `onResponse` and `onFailure`. -
respondAndRelease
Shorthand for resolving givenlistener
with givenresponse
and decrementing the response's ref count by one afterwards. -
assertOnce
- Returns:
- A listener which (if assertions are enabled) wraps around the given delegate and asserts that it is only called once.
-
run
static <T,L extends ActionListener<T>> void run(L listener, CheckedConsumer<L, ? extends Exception> action) Execute the given action in atry/catch
block which feeds all exceptions to the given listener'sonFailure(java.lang.Exception)
method. -
runWithResource
static <T,R extends AutoCloseable> void runWithResource(ActionListener<T> listener, CheckedSupplier<R, ? extends Exception> resourceSupplier, CheckedBiConsumer<ActionListener<T>, R, ? extends Exception> action) Execute the given action in an (async equivalent of a) try-with-resources block which closes the supplied resource on completion, and feeds all exceptions to the given listener'sonFailure(java.lang.Exception)
method.
-