Skip to content

Commit cf05e28

Browse files
Enhance ActionListener documentation (#104239)
Improve comments for ActionListener#delegateFailure and SubscribableListener. Closes #103829
1 parent ae87d32 commit cf05e28

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

server/src/main/java/org/elasticsearch/action/ActionListener.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,21 @@ default ActionListener<Response> delegateResponse(BiConsumer<ActionListener<Resp
8989
}
9090

9191
/**
92-
* Creates a listener that delegates all exceptions it receives to another listener.
92+
* Creates a new listener, wrapping this one, that overrides {@link #onResponse} handling with the given {@code bc} consumer.
93+
* {@link #onFailure(Exception)} handling is delegated to the original listener. Exceptions in {@link #onResponse} are forbidden.
9394
*
94-
* @param bc BiConsumer invoked with delegate listener and response
95+
* @param bc {@link BiConsumer} invoked via {@link #onResponse} with the original listener and the response with which the new listener
96+
* was completed.
9597
* @param <T> Type of the delegating listener's response
96-
* @return Delegating listener
98+
* @return a new listener that delegates failures to this listener and runs {@code bc} on a response.
9799
*/
98100
default <T> ActionListener<T> delegateFailure(BiConsumer<ActionListener<Response>, T> bc) {
99101
return new ActionListenerImplementations.DelegatingFailureActionListener<>(this, bc);
100102
}
101103

102104
/**
103-
* Same as {@link #delegateFailure(BiConsumer)} except that any failure thrown by {@code bc} or the delegate listener's
104-
* {@link #onResponse} will be passed to the delegate listeners {@link #onFailure(Exception)}.
105+
* Same as {@link #delegateFailure(BiConsumer)} except that any failure thrown by {@code bc} or the original listener's
106+
* {@link #onResponse} will be passed to the original listener's {@link #onFailure(Exception)}.
105107
*/
106108
default <T> ActionListener<T> delegateFailureAndWrap(CheckedBiConsumer<ActionListener<Response>, T, ? extends Exception> bc) {
107109
return new ActionListenerImplementations.ResponseWrappingActionListener<>(this, bc);
@@ -150,7 +152,7 @@ public String toString() {
150152
* the sense that an exception from the {@code onResponse} consumer is passed into the {@code onFailure} consumer.
151153
* <p>
152154
* If the {@code onFailure} argument is {@code listener::onFailure} for some other {@link ActionListener}, prefer to use
153-
* {@link #delegateFailureAndWrap} instead.
155+
* {@link #delegateFailureAndWrap} instead for performance reasons.
154156
* @param onResponse the checked consumer of the response, executed when the listener is completed successfully. If it throws an
155157
* exception, the exception is passed to the {@code onFailure} consumer.
156158
* @param onFailure the consumer of the failure, executed when the listener is completed with an exception (or it is completed

server/src/main/java/org/elasticsearch/action/ActionListenerImplementations.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ public String toString() {
197197
}
198198
}
199199

200+
/**
201+
* Replaces the onResponse handling of a given ActionListener with a lambda that receives both the original listener and a response.
202+
* This is useful when a listener is needed to do some additional work with a response before passing a response on to the original
203+
* listener.
204+
*/
200205
static final class DelegatingFailureActionListener<T, R> extends DelegatingActionListener<T, R> {
201206

202207
private final BiConsumer<ActionListener<R>, T> bc;
@@ -221,6 +226,10 @@ public String toString() {
221226
}
222227
}
223228

229+
/**
230+
* The same as {@link DelegatingFailureActionListener} with the addition of exception handling in {@link #onResponse(Object)} to forward
231+
* any exceptions to {@link #onFailure(Exception)}.
232+
*/
224233
static final class ResponseWrappingActionListener<T, R> extends DelegatingActionListener<T, R> {
225234

226235
private final CheckedBiConsumer<ActionListener<R>, T, ? extends Exception> bc;

server/src/main/java/org/elasticsearch/action/DelegatingActionListener.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
import static org.elasticsearch.action.ActionListenerImplementations.safeOnFailure;
1212

1313
/**
14-
* A wrapper around an {@link ActionListener} which delegates failures safely to the inner listener's {@link ActionListener#onFailure}
15-
* method and which has a {@link #toString()} implementation which describes this class and the delegate.
14+
* A wrapper around an {@link ActionListener} {@code L} that by default delegates failures to {@code L}'s {@link ActionListener#onFailure}
15+
* method. The wrapper also provides a {@link #toString()} implementation that describes this class and the delegate.
16+
* <p>
17+
* This is a useful base class for creating ActionListener wrappers that override the {@link #onResponse} handling, with access to
18+
* {@code L}, while retaining all of {@code L}'s other handling. It can also be useful to override other methods to do new work with access
19+
* to {@code L}.
1620
*/
1721
public abstract class DelegatingActionListener<Response, DelegateResponse> implements ActionListener<Response> {
1822

server/src/main/java/org/elasticsearch/action/support/SubscribableListener.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
* An {@link ActionListener} to which other {@link ActionListener} instances can subscribe, such that when this listener is completed it
3535
* fans-out its result to the subscribed listeners.
3636
* <p>
37-
* Similar to {@link ListenableActionFuture} and {@link ListenableFuture} except for its handling of exceptions: if this listener is
38-
* completed exceptionally then the exception is passed to subscribed listeners without modification.
37+
* Exceptions are passed to subscribed listeners without modification. {@link ListenableActionFuture} and {@link ListenableFuture} are child
38+
* classes that provide additional exception handling.
3939
* <p>
40-
* Often this will be used to chain together a sequence of async actions, similarly to {@link CompletionStage} (without the
41-
* {@code catch (Throwable t)}), such as in the following example:
40+
* A sequence of async steps can be chained together using a series of {@link SubscribableListener}s, similar to {@link CompletionStage}
41+
* (without the {@code catch (Throwable t)}). Listeners can be created for each step, where the next step subscribes to the result of the
42+
* previous, using utilities like {@link #andThen(CheckedBiConsumer)}. The following example demonstrates how this might be used:
4243
* <pre>{@code
4344
* private void exampleAsyncMethod(String request, List<Long> items, ActionListener<Boolean> finalListener) {
4445
* SubscribableListener

0 commit comments

Comments
 (0)