1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
import java.util.ArrayList; |
10 |
import java.util.function.Consumer; |
11 |
import java.util.function.Function; |
12 |
import java.util.stream.Stream; |
13 |
|
14 |
/** |
15 |
* Interrelated interfaces and static methods for establishing |
16 |
* flow-controlled components in which {@link Publisher Publishers} |
17 |
* produce items consumed by one or more {@link Subscriber |
18 |
* Subscribers}, each managed by a {@link Subscription |
19 |
* Subscription}. |
20 |
* |
21 |
* <p>These interfaces correspond to the <a |
22 |
* href="http://www.reactive-streams.org/"> reactive-streams</a> |
23 |
* specification. They apply in both concurrent and distributed |
24 |
* asynchronous settings: All (seven) methods are defined in {@code |
25 |
* void} "oneway" message style. Communication relies on a simple form |
26 |
* of flow control (method {@link Subscription#request}) that can be |
27 |
* used to avoid resource management problems that may otherwise occur |
28 |
* in "push" based systems. |
29 |
* |
30 |
* <p><b>Examples.</b> A {@link Publisher} usually defines its own |
31 |
* {@link Subscription} implementation; constructing one in method |
32 |
* {@code subscribe} and issuing it to the calling {@link |
33 |
* Subscriber}. It publishes items to the subscriber asynchronously, |
34 |
* normally using an {@link Executor}. For example, here is a very |
35 |
* simple publisher that only issues (when requested) a single {@code |
36 |
* TRUE} item to a single subscriber. Because the subscriber receives |
37 |
* only a single item, this class does not use buffering and ordering |
38 |
* control required in most implementations (for example {@link |
39 |
* SubmissionPublisher}). |
40 |
* |
41 |
* <pre> {@code |
42 |
* class OneShotPublisher implements Publisher<Boolean> { |
43 |
* final ExecutorService executor = ForkJoinPool.commonPool(); // daemon-based |
44 |
* boolean subscribed = false; // true after first subscribe |
45 |
* public synchronized void subscribe(Subscriber<? super Boolean> subscriber) { |
46 |
* if (subscribed) |
47 |
* subscriber.onError(new IllegalStateException()); // only one allowed |
48 |
* else { |
49 |
* subscribed = true; |
50 |
* subscriber.onSubscribe(new OneShotSubscription(subscriber, executor)); |
51 |
* } |
52 |
* } |
53 |
* static class OneShotSubscription implements Subscription { |
54 |
* final Subscriber<? super Boolean> subscriber; |
55 |
* final ExecutorService executor; |
56 |
* Future<?> future; // to allow cancellation |
57 |
* boolean completed = false; |
58 |
* OneShotSubscription(Subscriber<? super Boolean> subscriber, |
59 |
* ExecutorService executor) { |
60 |
* this.subscriber = subscriber; |
61 |
* this.executor = executor; |
62 |
* } |
63 |
* public synchronized void request(long n) { |
64 |
* if (n != 0 && !completed) { |
65 |
* completed = true; |
66 |
* future = executor.submit(() -> { |
67 |
* if (n < 0) |
68 |
* subscriber.onError(new IllegalArgumentException()); |
69 |
* else { |
70 |
* subscriber.onNext(Boolean.TRUE); |
71 |
* subscriber.onComplete(); |
72 |
* }}); |
73 |
* } |
74 |
* } |
75 |
* public synchronized void cancel() { |
76 |
* completed = true; |
77 |
* if (future != null) future.cancel(false); |
78 |
* } |
79 |
* } |
80 |
* }}</pre> |
81 |
* |
82 |
* <p>A {@link Subscriber} arranges that items be requested and |
83 |
* processed. Items (invocations of {@link Subscriber#onNext}) are |
84 |
* not issued unless requested, but multiple items may be requested. |
85 |
* Many Subscriber implementations can arrange this in the style of |
86 |
* the following example, where a buffer size of 1 single-steps, and |
87 |
* larger sizes usually allow for more efficient overlapped processing |
88 |
* with less communication; for example with a value of 64, this keeps |
89 |
* total outstanding requests between 32 and 64. (See also {@link |
90 |
* #consume(long, Publisher, Consumer)} that automates a common case.) |
91 |
* Because Subscriber method invocations for a given {@link |
92 |
* Subscription} are strictly ordered, there is no need for these |
93 |
* methods to use locks or volatiles unless a Subscriber maintains |
94 |
* multiple Subscriptions (in which case it is better to instead |
95 |
* define multiple Subscribers, each with its own Subscription). |
96 |
* |
97 |
* <pre> {@code |
98 |
* class SampleSubscriber<T> implements Subscriber<T> { |
99 |
* final Consumer<? super T> consumer; |
100 |
* Subscription subscription; |
101 |
* final long bufferSize; |
102 |
* long count; |
103 |
* SampleSubscriber(long bufferSize, Consumer<? super T> consumer) { |
104 |
* this.bufferSize = bufferSize; |
105 |
* this.consumer = consumer; |
106 |
* } |
107 |
* public void onSubscribe(Subscription subscription) { |
108 |
* (this.subscription = subscription).request(bufferSize); |
109 |
* count = bufferSize - bufferSize / 2; // re-request when half consumed |
110 |
* } |
111 |
* public void onNext(T item) { |
112 |
* if (--count <= 0) |
113 |
* subscription.request(count = bufferSize - bufferSize / 2); |
114 |
* consumer.accept(item); |
115 |
* } |
116 |
* public void onError(Throwable ex) { ex.printStackTrace(); } |
117 |
* public void onComplete() {} |
118 |
* }}</pre> |
119 |
* |
120 |
* <p>When flow control is inapplicable, a subscriber may initially |
121 |
* request an effectively unbounded number of items, as in: |
122 |
* |
123 |
* <pre> {@code |
124 |
* class UnboundedSubscriber<T> implements Subscriber<T> { |
125 |
* public void onSubscribe(Subscription subscription) { |
126 |
* subscription.request(Long.MAX_VALUE); // effectively unbounded |
127 |
* } |
128 |
* public void onNext(T item) { use(item); } |
129 |
* public void onError(Throwable ex) { ex.printStackTrace(); } |
130 |
* public void onComplete() {} |
131 |
* void use(T item) { ... } |
132 |
* }}</pre> |
133 |
* |
134 |
* @author Doug Lea |
135 |
* @since 1.9 |
136 |
*/ |
137 |
public final class Flow { |
138 |
|
139 |
private Flow() {} // uninstantiable |
140 |
|
141 |
/** |
142 |
* A producer of items (and related control messages) received by |
143 |
* Subscribers. Each current {@link Subscriber} receives the same |
144 |
* items (via method {@code onNext}) in the same order, unless |
145 |
* drops or errors are encountered. If a Publisher encounters an |
146 |
* error that does not allow items to be issued to a Subscriber, |
147 |
* that Subscriber receives {@code onError}, and then receives no |
148 |
* further messages. Otherwise, when it is known that no further |
149 |
* messages will be issued to it, a subscriber receives {@code |
150 |
* onComplete}. Publishers ensure that Subscriber method |
151 |
* invocations for each subscription are strictly ordered in <a |
152 |
* href="package-summary.html#MemoryVisibility"><i>happens-before</i></a> |
153 |
* order. |
154 |
* |
155 |
* <p>Publishers may vary in policy about whether drops (failures |
156 |
* to issue an item because of resource limitations) are treated |
157 |
* as unrecoverable errors. Publishers may also vary about |
158 |
* whether Subscribers receive items that were produced or |
159 |
* available before they subscribed. |
160 |
* |
161 |
* @param <T> the published item type |
162 |
*/ |
163 |
@FunctionalInterface |
164 |
public static interface Publisher<T> { |
165 |
/** |
166 |
* Adds the given Subscriber if possible. If already |
167 |
* subscribed, or the attempt to subscribe fails due to policy |
168 |
* violations or errors, the Subscriber's {@code onError} |
169 |
* method is invoked with an {@link IllegalStateException}. |
170 |
* Otherwise, the Subscriber's {@code onSubscribe} method is |
171 |
* invoked with a new {@link Subscription}. Subscribers may |
172 |
* enable receiving items by invoking the {@code request} |
173 |
* method of this Subscription, and may unsubscribe by |
174 |
* invoking its {@code cancel} method. |
175 |
* |
176 |
* @param subscriber the subscriber |
177 |
* @throws NullPointerException if subscriber is null |
178 |
*/ |
179 |
public void subscribe(Subscriber<? super T> subscriber); |
180 |
} |
181 |
|
182 |
/** |
183 |
* A receiver of messages. The methods in this interface are |
184 |
* invoked in strict sequential order for each {@link |
185 |
* Subscription}. |
186 |
* |
187 |
* @param <T> the subscribed item type |
188 |
*/ |
189 |
public static interface Subscriber<T> { |
190 |
/** |
191 |
* Method invoked prior to invoking any other Subscriber |
192 |
* methods for the given Subscription. If this method throws |
193 |
* an exception, resulting behavior is not guaranteed, but may |
194 |
* cause the Subscription to be cancelled. |
195 |
* |
196 |
* <p>Typically, implementations of this method invoke {@code |
197 |
* subscription.request} to enable receiving items. |
198 |
* |
199 |
* @param subscription a new subscription |
200 |
*/ |
201 |
public void onSubscribe(Subscription subscription); |
202 |
|
203 |
/** |
204 |
* Method invoked with a Subscription's next item. If this |
205 |
* method throws an exception, resulting behavior is not |
206 |
* guaranteed, but may cause the Subscription to be cancelled. |
207 |
* |
208 |
* @param item the item |
209 |
*/ |
210 |
public void onNext(T item); |
211 |
|
212 |
/** |
213 |
* Method invoked upon an unrecoverable error encountered by a |
214 |
* Publisher or Subscription, after which no other Subscriber |
215 |
* methods are invoked by the Subscription. If this method |
216 |
* itself throws an exception, resulting behavior is |
217 |
* undefined. |
218 |
* |
219 |
* @param throwable the exception |
220 |
*/ |
221 |
public void onError(Throwable throwable); |
222 |
|
223 |
/** |
224 |
* Method invoked when it is known that no additional |
225 |
* Subscriber method invocations will occur for a Subscription |
226 |
* that is not already terminated by error, after which no |
227 |
* other Subscriber methods are invoked by the Subscription. |
228 |
* If this method throws an exception, resulting behavior is |
229 |
* undefined. |
230 |
*/ |
231 |
public void onComplete(); |
232 |
} |
233 |
|
234 |
/** |
235 |
* Message control linking a {@link Publisher} and {@link |
236 |
* Subscriber}. Subscribers receive items only when requested, |
237 |
* and may cancel at any time. The methods in this interface are |
238 |
* intended to be invoked only by their Subscribers; usages in |
239 |
* other contexts have undefined effects. |
240 |
*/ |
241 |
public static interface Subscription { |
242 |
/** |
243 |
* Adds the given number {@code n} of items to the current |
244 |
* unfulfilled demand for this subscription. If {@code n} is |
245 |
* negative, the Subscriber will receive an {@code onError} |
246 |
* signal with an {@link IllegalArgumentException} |
247 |
* argument. Otherwise, the Subscriber will receive up to |
248 |
* {@code n} additional {@code onNext} invocations (or fewer |
249 |
* if terminated). |
250 |
* |
251 |
* @param n the increment of demand; a value of {@code |
252 |
* Long.MAX_VALUE} may be considered as effectively unbounded |
253 |
*/ |
254 |
public void request(long n); |
255 |
|
256 |
/** |
257 |
* Causes the Subscriber to (eventually) stop receiving |
258 |
* messages. Implementation is best-effort -- additional |
259 |
* messages may be received after invoking this method. A |
260 |
* cancelled subscription need not ever receive an {@code |
261 |
* onComplete} signal. |
262 |
*/ |
263 |
public void cancel(); |
264 |
} |
265 |
|
266 |
/** |
267 |
* A component that acts as both a Subscriber and Publisher. |
268 |
* |
269 |
* @param <T> the subscribed item type |
270 |
* @param <R> the published item type |
271 |
*/ |
272 |
public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> { |
273 |
} |
274 |
|
275 |
// Support for static methods |
276 |
|
277 |
static final long DEFAULT_BUFFER_SIZE = 64L; |
278 |
|
279 |
abstract static class CompletableSubscriber<T,U> implements Subscriber<T>, |
280 |
Consumer<T> { |
281 |
final CompletableFuture<U> status; |
282 |
Subscription subscription; |
283 |
long requestSize; |
284 |
long count; |
285 |
CompletableSubscriber(long bufferSize, CompletableFuture<U> status) { |
286 |
this.status = status; |
287 |
this.requestSize = bufferSize; |
288 |
} |
289 |
public final void onSubscribe(Subscription subscription) { |
290 |
(this.subscription = subscription).request(requestSize); |
291 |
count = requestSize -= (requestSize >>> 1); |
292 |
} |
293 |
public final void onError(Throwable ex) { |
294 |
status.completeExceptionally(ex); |
295 |
} |
296 |
public void onNext(T item) { |
297 |
try { |
298 |
if (--count <= 0) |
299 |
subscription.request(count = requestSize); |
300 |
accept(item); |
301 |
} catch (Throwable ex) { |
302 |
status.completeExceptionally(ex); |
303 |
} |
304 |
} |
305 |
} |
306 |
|
307 |
static final class ConsumeSubscriber<T> extends CompletableSubscriber<T,Void> { |
308 |
final Consumer<? super T> consumer; |
309 |
ConsumeSubscriber(long bufferSize, |
310 |
CompletableFuture<Void> status, |
311 |
Consumer<? super T> consumer) { |
312 |
super(bufferSize, status); |
313 |
this.consumer = consumer; |
314 |
} |
315 |
public void accept(T item) { consumer.accept(item); } |
316 |
public void onComplete() { status.complete(null); } |
317 |
} |
318 |
|
319 |
/** |
320 |
* Creates and subscribes a Subscriber that consumes all items |
321 |
* from the given publisher using the given Consumer function, and |
322 |
* using the given bufferSize for buffering. Returns a |
323 |
* CompletableFuture that is completed normally when the publisher |
324 |
* signals {@code onComplete}, or completed exceptionally upon any |
325 |
* error, including an exception thrown by the Consumer (in which |
326 |
* case the subscription is cancelled if not already terminated). |
327 |
* |
328 |
* @param <T> the published item type |
329 |
* @param bufferSize the request size for subscriptions |
330 |
* @param publisher the publisher |
331 |
* @param consumer the function applied to each onNext item |
332 |
* @return a CompletableFuture that is completed normally |
333 |
* when the publisher signals onComplete, and exceptionally |
334 |
* upon any error |
335 |
* @throws NullPointerException if publisher or consumer are null |
336 |
* @throws IllegalArgumentException if bufferSize not positive |
337 |
*/ |
338 |
public static <T> CompletableFuture<Void> consume( |
339 |
long bufferSize, Publisher<T> publisher, Consumer<? super T> consumer) { |
340 |
if (bufferSize <= 0L) |
341 |
throw new IllegalArgumentException("bufferSize must be positive"); |
342 |
if (publisher == null || consumer == null) |
343 |
throw new NullPointerException(); |
344 |
CompletableFuture<Void> status = new CompletableFuture<>(); |
345 |
publisher.subscribe(new ConsumeSubscriber<T>( |
346 |
bufferSize, status, consumer)); |
347 |
return status; |
348 |
} |
349 |
|
350 |
/** |
351 |
* Equivalent to {@link #consume(long, Publisher, Consumer)} |
352 |
* with a buffer size of 64. |
353 |
* |
354 |
* @param <T> the published item type |
355 |
* @param publisher the publisher |
356 |
* @param consumer the function applied to each onNext item |
357 |
* @return a CompletableFuture that is completed normally |
358 |
* when the publisher signals onComplete, and exceptionally |
359 |
* upon any error |
360 |
* @throws NullPointerException if publisher or consumer are null |
361 |
*/ |
362 |
public static <T> CompletableFuture<Void> consume( |
363 |
Publisher<T> publisher, Consumer<? super T> consumer) { |
364 |
return consume(DEFAULT_BUFFER_SIZE, publisher, consumer); |
365 |
} |
366 |
|
367 |
/** |
368 |
* Temporary implementation for Stream, collecting all items |
369 |
* and then applying stream operation. |
370 |
*/ |
371 |
static final class StreamSubscriber<T,R> extends CompletableSubscriber<T,R> { |
372 |
final Function<? super Stream<T>, ? extends R> fn; |
373 |
final ArrayList<T> items; |
374 |
StreamSubscriber(long bufferSize, |
375 |
CompletableFuture<R> status, |
376 |
Function<? super Stream<T>, ? extends R> fn) { |
377 |
super(bufferSize, status); |
378 |
this.fn = fn; |
379 |
this.items = new ArrayList<T>(); |
380 |
} |
381 |
public void accept(T item) { items.add(item); } |
382 |
public void onComplete() { status.complete(fn.apply(items.stream())); } |
383 |
} |
384 |
|
385 |
/** |
386 |
* Creates and subscribes a Subscriber that applies the given |
387 |
* stream operation to items, and uses the given bufferSize for |
388 |
* buffering. Returns a CompletableFuture that is completed |
389 |
* normally with the result of this function when the publisher |
390 |
* signals {@code onComplete}, or is completed exceptionally upon |
391 |
* any error. |
392 |
* |
393 |
* <p><b>Preliminary release note:</b> Currently, this method |
394 |
* collects all items before executing the stream |
395 |
* computation. Improvements are pending Stream integration. |
396 |
* |
397 |
* @param <T> the published item type |
398 |
* @param <R> the result type of the stream function |
399 |
* @param bufferSize the request size for subscriptions |
400 |
* @param publisher the publisher |
401 |
* @param streamFunction the operation on elements |
402 |
* @return a CompletableFuture that is completed normally with the |
403 |
* result of the given function as result when the publisher signals |
404 |
* onComplete, and exceptionally upon any error |
405 |
* @throws NullPointerException if publisher or function are null |
406 |
* @throws IllegalArgumentException if bufferSize not positive |
407 |
*/ |
408 |
public static <T,R> CompletableFuture<R> stream( |
409 |
long bufferSize, Publisher<T> publisher, |
410 |
Function<? super Stream<T>, ? extends R> streamFunction) { |
411 |
if (bufferSize <= 0L) |
412 |
throw new IllegalArgumentException("bufferSize must be positive"); |
413 |
if (publisher == null || streamFunction == null) |
414 |
throw new NullPointerException(); |
415 |
CompletableFuture<R> status = new CompletableFuture<>(); |
416 |
publisher.subscribe(new StreamSubscriber<T,R>( |
417 |
bufferSize, status, streamFunction)); |
418 |
return status; |
419 |
} |
420 |
|
421 |
/** |
422 |
* Equivalent to {@link #stream(long, Publisher, Function)} |
423 |
* with a buffer size of 64. |
424 |
* |
425 |
* @param <T> the published item type |
426 |
* @param <R> the result type of the stream function |
427 |
* @param publisher the publisher |
428 |
* @param streamFunction the operation on elements |
429 |
* @return a CompletableFuture that is completed normally with the |
430 |
* result of the given function as result when the publisher signals |
431 |
* onComplete, and exceptionally upon any error |
432 |
* @throws NullPointerException if publisher or function are null |
433 |
*/ |
434 |
public static <T,R> CompletableFuture<R> stream( |
435 |
Publisher<T> publisher, |
436 |
Function<? super Stream<T>,? extends R> streamFunction) { |
437 |
return stream(DEFAULT_BUFFER_SIZE, publisher, streamFunction); |
438 |
} |
439 |
} |