|
19 | 19 |
|
20 | 20 | package org.elasticsearch.action.support; |
21 | 21 |
|
| 22 | +import org.apache.logging.log4j.Logger; |
| 23 | +import org.elasticsearch.action.ActionListener; |
| 24 | +import org.elasticsearch.action.ListenableActionFuture; |
| 25 | +import org.elasticsearch.common.logging.Loggers; |
22 | 26 | import org.elasticsearch.threadpool.ThreadPool; |
23 | 27 |
|
24 | | -public class PlainListenableActionFuture<T> extends AbstractListenableActionFuture<T, T> { |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
25 | 30 |
|
26 | | - public PlainListenableActionFuture(ThreadPool threadPool) { |
27 | | - super(threadPool); |
| 31 | +public class PlainListenableActionFuture<T> extends AdapterActionFuture<T, T> implements ListenableActionFuture<T> { |
| 32 | + |
| 33 | + volatile Object listeners; |
| 34 | + boolean executedListeners = false; |
| 35 | + |
| 36 | + private PlainListenableActionFuture() {} |
| 37 | + |
| 38 | + /** |
| 39 | + * This method returns a listenable future. The listeners will be called on completion of the future. |
| 40 | + * The listeners will be executed by the same thread that completes the future. |
| 41 | + * |
| 42 | + * @param <T> the result of the future |
| 43 | + * @return a listenable future |
| 44 | + */ |
| 45 | + public static <T> PlainListenableActionFuture<T> newListenableFuture() { |
| 46 | + return new PlainListenableActionFuture<>(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * This method returns a listenable future. The listeners will be called on completion of the future. |
| 51 | + * The listeners will be executed on the LISTENER thread pool. |
| 52 | + * @param threadPool the thread pool used to execute listeners |
| 53 | + * @param <T> the result of the future |
| 54 | + * @return a listenable future |
| 55 | + */ |
| 56 | + public static <T> PlainListenableActionFuture<T> newDispatchingListenableFuture(ThreadPool threadPool) { |
| 57 | + return new DispatchingListenableActionFuture<>(threadPool); |
28 | 58 | } |
29 | 59 |
|
30 | 60 | @Override |
31 | | - protected T convert(T response) { |
32 | | - return response; |
| 61 | + public void addListener(final ActionListener<T> listener) { |
| 62 | + internalAddListener(listener); |
33 | 63 | } |
34 | 64 |
|
| 65 | + @Override |
| 66 | + protected void done() { |
| 67 | + super.done(); |
| 68 | + synchronized (this) { |
| 69 | + executedListeners = true; |
| 70 | + } |
| 71 | + Object listeners = this.listeners; |
| 72 | + if (listeners != null) { |
| 73 | + if (listeners instanceof List) { |
| 74 | + List list = (List) listeners; |
| 75 | + for (Object listener : list) { |
| 76 | + executeListener((ActionListener<T>) listener); |
| 77 | + } |
| 78 | + } else { |
| 79 | + executeListener((ActionListener<T>) listeners); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected T convert(T listenerResponse) { |
| 86 | + return listenerResponse; |
| 87 | + } |
| 88 | + |
| 89 | + private void internalAddListener(ActionListener<T> listener) { |
| 90 | + boolean executeImmediate = false; |
| 91 | + synchronized (this) { |
| 92 | + if (executedListeners) { |
| 93 | + executeImmediate = true; |
| 94 | + } else { |
| 95 | + Object listeners = this.listeners; |
| 96 | + if (listeners == null) { |
| 97 | + listeners = listener; |
| 98 | + } else if (listeners instanceof List) { |
| 99 | + ((List) this.listeners).add(listener); |
| 100 | + } else { |
| 101 | + Object orig = listeners; |
| 102 | + listeners = new ArrayList<>(2); |
| 103 | + ((List) listeners).add(orig); |
| 104 | + ((List) listeners).add(listener); |
| 105 | + } |
| 106 | + this.listeners = listeners; |
| 107 | + } |
| 108 | + } |
| 109 | + if (executeImmediate) { |
| 110 | + executeListener(listener); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void executeListener(final ActionListener<T> listener) { |
| 115 | + try { |
| 116 | + // we use a timeout of 0 to by pass assertion forbidding to call actionGet() (blocking) on a network thread. |
| 117 | + // here we know we will never block |
| 118 | + listener.onResponse(actionGet(0)); |
| 119 | + } catch (Exception e) { |
| 120 | + listener.onFailure(e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static final class DispatchingListenableActionFuture<T> extends PlainListenableActionFuture<T> { |
| 125 | + |
| 126 | + private static final Logger logger = Loggers.getLogger(DispatchingListenableActionFuture.class); |
| 127 | + private final ThreadPool threadPool; |
| 128 | + |
| 129 | + private DispatchingListenableActionFuture(ThreadPool threadPool) { |
| 130 | + this.threadPool = threadPool; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void addListener(final ActionListener<T> listener) { |
| 135 | + super.addListener(new ThreadedActionListener<>(logger, threadPool, ThreadPool.Names.LISTENER, listener, false)); |
| 136 | + } |
| 137 | + } |
35 | 138 | } |
0 commit comments