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} "one-way" 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 |
* private final ExecutorService executor = ForkJoinPool.commonPool(); // daemon-based |
44 |
* private 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 |
* private final Subscriber<? super Boolean> subscriber; |
55 |
* private final ExecutorService executor; |
56 |
* private Future<?> future; // to allow cancellation |
57 |
* private 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 |
* if (n < 0) { |
67 |
* IllegalStateException ex = new IllegalStateException(); |
68 |
* executor.execute(() -> subscriber.onError(ex)); |
69 |
* } |
70 |
* else { |
71 |
* future = executor.submit(() -> { |
72 |
* subscriber.onNext(Boolean.TRUE); |
73 |
* subscriber.onComplete(); |
74 |
* }); |
75 |
* } |
76 |
* } |
77 |
* } |
78 |
* public synchronized void cancel() { |
79 |
* completed = true; |
80 |
* if (future != null) future.cancel(false); |
81 |
* } |
82 |
* } |
83 |
* }}</pre> |
84 |
* |
85 |
* <p>A {@link Subscriber} arranges that items be requested and |
86 |
* processed. Items (invocations of {@link Subscriber#onNext}) are |
87 |
* not issued unless requested, but multiple items may be requested. |
88 |
* Many Subscriber implementations can arrange this in the style of |
89 |
* the following example, where a buffer size of 1 single-steps, and |
90 |
* larger sizes usually allow for more efficient overlapped processing |
91 |
* with less communication; for example with a value of 64, this keeps |
92 |
* total outstanding requests between 32 and 64. (See also {@link |
93 |
* #consume(long, Publisher, Consumer)} that automates a common case.) |
94 |
* Because Subscriber method invocations for a given {@link |
95 |
* Subscription} are strictly ordered, there is no need for these |
96 |
* methods to use locks or volatiles unless a Subscriber maintains |
97 |
* multiple Subscriptions (in which case it is better to instead |
98 |
* define multiple Subscribers, each with its own Subscription). |
99 |
* |
100 |
* <pre> {@code |
101 |
* class SampleSubscriber<T> implements Subscriber<T> { |
102 |
* final Consumer<? super T> consumer; |
103 |
* Subscription subscription; |
104 |
* final long bufferSize; |
105 |
* long count; |
106 |
* SampleSubscriber(long bufferSize, Consumer<? super T> consumer) { |
107 |
* this.bufferSize = bufferSize; |
108 |
* this.consumer = consumer; |
109 |
* } |
110 |
* public void onSubscribe(Subscription subscription) { |
111 |
* long initialRequestSize = bufferSize; |
112 |
* count = bufferSize - bufferSize / 2; // re-request when half consumed |
113 |
* (this.subscription = subscription).request(initialRequestSize); |
114 |
* } |
115 |
* public void onNext(T item) { |
116 |
* if (--count <= 0) |
117 |
* subscription.request(count = bufferSize - bufferSize / 2); |
118 |
* consumer.accept(item); |
119 |
* } |
120 |
* public void onError(Throwable ex) { ex.printStackTrace(); } |
121 |
* public void onComplete() {} |
122 |
* }}</pre> |
123 |
* |
124 |
* <p>The default value of {@link #defaultBufferSize} may provide a |
125 |
* useful starting point for choosing request sizes and capacities in |
126 |
* Flow components based on expected rates, resources, and usages. |
127 |
* Or, when flow control is known to be always inapplicable, a |
128 |
* subscriber may initially request an effectively unbounded number of |
129 |
* items, as in: |
130 |
* |
131 |
* <pre> {@code |
132 |
* class UnboundedSubscriber<T> implements Subscriber<T> { |
133 |
* public void onSubscribe(Subscription subscription) { |
134 |
* subscription.request(Long.MAX_VALUE); // effectively unbounded |
135 |
* } |
136 |
* public void onNext(T item) { use(item); } |
137 |
* public void onError(Throwable ex) { ex.printStackTrace(); } |
138 |
* public void onComplete() {} |
139 |
* void use(T item) { ... } |
140 |
* }}</pre> |
141 |
* |
142 |
* @author Doug Lea |
143 |
* @since 1.9 |
144 |
*/ |
145 |
public final class Flow { |
146 |
|
147 |
private Flow() {} // uninstantiable |
148 |
|
149 |
/** |
150 |
* A producer of items (and related control messages) received by |
151 |
* Subscribers. Each current {@link Subscriber} receives the same |
152 |
* items (via method {@code onNext}) in the same order, unless |
153 |
* drops or errors are encountered. If a Publisher encounters an |
154 |
* error that does not allow items to be issued to a Subscriber, |
155 |
* that Subscriber receives {@code onError}, and then receives no |
156 |
* further messages. Otherwise, when it is known that no further |
157 |
* messages will be issued to it, a subscriber receives {@code |
158 |
* onComplete}. Publishers ensure that Subscriber method |
159 |
* invocations for each subscription are strictly ordered in <a |
160 |
* href="package-summary.html#MemoryVisibility"><i>happens-before</i></a> |
161 |
* order. |
162 |
* |
163 |
* <p>Publishers may vary in policy about whether drops (failures |
164 |
* to issue an item because of resource limitations) are treated |
165 |
* as unrecoverable errors. Publishers may also vary about |
166 |
* whether Subscribers receive items that were produced or |
167 |
* available before they subscribed. |
168 |
* |
169 |
* @param <T> the published item type |
170 |
*/ |
171 |
@FunctionalInterface |
172 |
public static interface Publisher<T> { |
173 |
/** |
174 |
* Adds the given Subscriber if possible. If already |
175 |
* subscribed, or the attempt to subscribe fails due to policy |
176 |
* violations or errors, the Subscriber's {@code onError} |
177 |
* method is invoked with an {@link IllegalStateException}. |
178 |
* Otherwise, the Subscriber's {@code onSubscribe} method is |
179 |
* invoked with a new {@link Subscription}. Subscribers may |
180 |
* enable receiving items by invoking the {@code request} |
181 |
* method of this Subscription, and may unsubscribe by |
182 |
* invoking its {@code cancel} method. |
183 |
* |
184 |
* @param subscriber the subscriber |
185 |
* @throws NullPointerException if subscriber is null |
186 |
*/ |
187 |
public void subscribe(Subscriber<? super T> subscriber); |
188 |
} |
189 |
|
190 |
/** |
191 |
* A receiver of messages. The methods in this interface are |
192 |
* invoked in strict sequential order for each {@link |
193 |
* Subscription}. |
194 |
* |
195 |
* @param <T> the subscribed item type |
196 |
*/ |
197 |
public static interface Subscriber<T> { |
198 |
/** |
199 |
* Method invoked prior to invoking any other Subscriber |
200 |
* methods for the given Subscription. If this method throws |
201 |
* an exception, resulting behavior is not guaranteed, but may |
202 |
* cause the Subscription to be cancelled. |
203 |
* |
204 |
* <p>Typically, implementations of this method invoke {@code |
205 |
* subscription.request} to enable receiving items. |
206 |
* |
207 |
* @param subscription a new subscription |
208 |
*/ |
209 |
public void onSubscribe(Subscription subscription); |
210 |
|
211 |
/** |
212 |
* Method invoked with a Subscription's next item. If this |
213 |
* method throws an exception, resulting behavior is not |
214 |
* guaranteed, but may cause the Subscription to be cancelled. |
215 |
* |
216 |
* @param item the item |
217 |
*/ |
218 |
public void onNext(T item); |
219 |
|
220 |
/** |
221 |
* Method invoked upon an unrecoverable error encountered by a |
222 |
* Publisher or Subscription, after which no other Subscriber |
223 |
* methods are invoked by the Subscription. If this method |
224 |
* itself throws an exception, resulting behavior is |
225 |
* undefined. |
226 |
* |
227 |
* @param throwable the exception |
228 |
*/ |
229 |
public void onError(Throwable throwable); |
230 |
|
231 |
/** |
232 |
* Method invoked when it is known that no additional |
233 |
* Subscriber method invocations will occur for a Subscription |
234 |
* that is not already terminated by error, after which no |
235 |
* other Subscriber methods are invoked by the Subscription. |
236 |
* If this method throws an exception, resulting behavior is |
237 |
* undefined. |
238 |
*/ |
239 |
public void onComplete(); |
240 |
} |
241 |
|
242 |
/** |
243 |
* Message control linking a {@link Publisher} and {@link |
244 |
* Subscriber}. Subscribers receive items only when requested, |
245 |
* and may cancel at any time. The methods in this interface are |
246 |
* intended to be invoked only by their Subscribers; usages in |
247 |
* other contexts have undefined effects. |
248 |
*/ |
249 |
public static interface Subscription { |
250 |
/** |
251 |
* Adds the given number {@code n} of items to the current |
252 |
* unfulfilled demand for this subscription. If {@code n} is |
253 |
* negative, the Subscriber will receive an {@code onError} |
254 |
* signal with an {@link IllegalArgumentException} argument. |
255 |
* Otherwise, the Subscriber will receive up to {@code n} |
256 |
* additional {@code onNext} invocations (or fewer if |
257 |
* terminated). |
258 |
* |
259 |
* @param n the increment of demand; a value of {@code |
260 |
* Long.MAX_VALUE} may be considered as effectively unbounded |
261 |
*/ |
262 |
public void request(long n); |
263 |
|
264 |
/** |
265 |
* Causes the Subscriber to (eventually) stop receiving |
266 |
* messages. Implementation is best-effort -- additional |
267 |
* messages may be received after invoking this method. A |
268 |
* cancelled subscription need not ever receive an {@code |
269 |
* onComplete} signal. |
270 |
*/ |
271 |
public void cancel(); |
272 |
} |
273 |
|
274 |
/** |
275 |
* A component that acts as both a Subscriber and Publisher. |
276 |
* |
277 |
* @param <T> the subscribed item type |
278 |
* @param <R> the published item type |
279 |
*/ |
280 |
public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> { |
281 |
} |
282 |
|
283 |
// Support for static methods |
284 |
|
285 |
static final int DEFAULT_BUFFER_SIZE = 256; |
286 |
|
287 |
/** |
288 |
* Returns a default value for Publisher or Subscriber buffering, |
289 |
* that may be used in the absence of other constraints. |
290 |
* |
291 |
* @implNote |
292 |
* The current value returned is 256. |
293 |
* |
294 |
* @return the buffer size value |
295 |
*/ |
296 |
public static int defaultBufferSize() { |
297 |
return DEFAULT_BUFFER_SIZE; |
298 |
} |
299 |
|
300 |
abstract static class CompletableSubscriber<T,U> |
301 |
implements Subscriber<T>, Consumer<T> |
302 |
{ |
303 |
final CompletableFuture<U> status; |
304 |
Subscription subscription; |
305 |
long requestSize; |
306 |
long count; |
307 |
CompletableSubscriber(long bufferSize, CompletableFuture<U> status) { |
308 |
this.status = status; |
309 |
this.requestSize = bufferSize; |
310 |
} |
311 |
public final void onSubscribe(Subscription subscription) { |
312 |
long rs = requestSize; |
313 |
count = requestSize -= (rs >>> 1); |
314 |
(this.subscription = subscription).request(rs); |
315 |
} |
316 |
public final void onError(Throwable ex) { |
317 |
status.completeExceptionally(ex); |
318 |
} |
319 |
public void onNext(T item) { |
320 |
try { |
321 |
if (--count <= 0) |
322 |
subscription.request(count = requestSize); |
323 |
accept(item); |
324 |
} catch (Throwable ex) { |
325 |
subscription.cancel(); |
326 |
status.completeExceptionally(ex); |
327 |
} |
328 |
} |
329 |
} |
330 |
|
331 |
static final class ConsumeSubscriber<T> extends CompletableSubscriber<T,Void> { |
332 |
final Consumer<? super T> consumer; |
333 |
ConsumeSubscriber(long bufferSize, |
334 |
CompletableFuture<Void> status, |
335 |
Consumer<? super T> consumer) { |
336 |
super(bufferSize, status); |
337 |
this.consumer = consumer; |
338 |
} |
339 |
public void accept(T item) { consumer.accept(item); } |
340 |
public void onComplete() { status.complete(null); } |
341 |
} |
342 |
|
343 |
/** |
344 |
* Creates and subscribes a Subscriber that consumes all items |
345 |
* from the given publisher using the given Consumer function, and |
346 |
* using the given bufferSize for buffering. Returns a |
347 |
* CompletableFuture that is completed normally when the publisher |
348 |
* signals {@code onComplete}, or completed exceptionally upon any |
349 |
* error, including an exception thrown by the Consumer (in which |
350 |
* case the subscription is cancelled if not already terminated). |
351 |
* Other attempts to cancel the CompletableFuture need not |
352 |
* cause the computation to terminate. |
353 |
* |
354 |
* @param <T> the published item type |
355 |
* @param bufferSize the request size for subscriptions |
356 |
* @param publisher the publisher |
357 |
* @param consumer the function applied to each onNext item |
358 |
* @return a CompletableFuture that is completed normally |
359 |
* when the publisher signals onComplete, and exceptionally |
360 |
* upon any error |
361 |
* @throws NullPointerException if publisher or consumer are null |
362 |
* @throws IllegalArgumentException if bufferSize not positive |
363 |
*/ |
364 |
public static <T> CompletableFuture<Void> consume( |
365 |
long bufferSize, Publisher<T> publisher, Consumer<? super T> consumer) { |
366 |
if (bufferSize <= 0L) |
367 |
throw new IllegalArgumentException("bufferSize must be positive"); |
368 |
if (publisher == null || consumer == null) |
369 |
throw new NullPointerException(); |
370 |
CompletableFuture<Void> status = new CompletableFuture<>(); |
371 |
publisher.subscribe(new ConsumeSubscriber<T>( |
372 |
bufferSize, status, consumer)); |
373 |
return status; |
374 |
} |
375 |
|
376 |
/** |
377 |
* Equivalent to {@link #consume(long, Publisher, Consumer)} |
378 |
* with {@link #defaultBufferSize}. |
379 |
* |
380 |
* @param <T> the published item type |
381 |
* @param publisher the publisher |
382 |
* @param consumer the function applied to each onNext item |
383 |
* @return a CompletableFuture that is completed normally |
384 |
* when the publisher signals onComplete, and exceptionally |
385 |
* upon any error |
386 |
* @throws NullPointerException if publisher or consumer are null |
387 |
*/ |
388 |
public static <T> CompletableFuture<Void> consume( |
389 |
Publisher<T> publisher, Consumer<? super T> consumer) { |
390 |
return consume(defaultBufferSize(), publisher, consumer); |
391 |
} |
392 |
|
393 |
/** |
394 |
* Temporary implementation for Stream, collecting all items |
395 |
* and then applying stream operation. |
396 |
*/ |
397 |
static final class StreamSubscriber<T,R> extends CompletableSubscriber<T,R> { |
398 |
final Function<? super Stream<T>, ? extends R> fn; |
399 |
final ArrayList<T> items; |
400 |
StreamSubscriber(long bufferSize, |
401 |
CompletableFuture<R> status, |
402 |
Function<? super Stream<T>, ? extends R> fn) { |
403 |
super(bufferSize, status); |
404 |
this.fn = fn; |
405 |
this.items = new ArrayList<T>(); |
406 |
} |
407 |
public void accept(T item) { items.add(item); } |
408 |
public void onComplete() { status.complete(fn.apply(items.stream())); } |
409 |
} |
410 |
|
411 |
/** |
412 |
* Creates and subscribes a Subscriber that applies the given |
413 |
* stream operation to items, and uses the given bufferSize for |
414 |
* buffering. Returns a CompletableFuture that is completed |
415 |
* normally with the result of this function when the publisher |
416 |
* signals {@code onComplete}, or is completed exceptionally upon |
417 |
* any error. Other attempts to cancel the CompletableFuture need |
418 |
* not cause the computation to terminate. |
419 |
* |
420 |
* <p><b>Preliminary release note:</b> Currently, this method |
421 |
* collects all items before executing the stream |
422 |
* computation. Improvements are pending Stream integration. |
423 |
* |
424 |
* @param <T> the published item type |
425 |
* @param <R> the result type of the stream function |
426 |
* @param bufferSize the request size for subscriptions |
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 |
* @throws IllegalArgumentException if bufferSize not positive |
434 |
*/ |
435 |
public static <T,R> CompletableFuture<R> stream( |
436 |
long bufferSize, Publisher<T> publisher, |
437 |
Function<? super Stream<T>, ? extends R> streamFunction) { |
438 |
if (bufferSize <= 0L) |
439 |
throw new IllegalArgumentException("bufferSize must be positive"); |
440 |
if (publisher == null || streamFunction == null) |
441 |
throw new NullPointerException(); |
442 |
CompletableFuture<R> status = new CompletableFuture<>(); |
443 |
publisher.subscribe(new StreamSubscriber<T,R>( |
444 |
bufferSize, status, streamFunction)); |
445 |
return status; |
446 |
} |
447 |
|
448 |
/** |
449 |
* Equivalent to {@link #stream(long, Publisher, Function)} |
450 |
* with {@link #defaultBufferSize}. |
451 |
* |
452 |
* @param <T> the published item type |
453 |
* @param <R> the result type of the stream function |
454 |
* @param publisher the publisher |
455 |
* @param streamFunction the operation on elements |
456 |
* @return a CompletableFuture that is completed normally with the |
457 |
* result of the given function as result when the publisher signals |
458 |
* onComplete, and exceptionally upon any error |
459 |
* @throws NullPointerException if publisher or function are null |
460 |
*/ |
461 |
public static <T,R> CompletableFuture<R> stream( |
462 |
Publisher<T> publisher, |
463 |
Function<? super Stream<T>,? extends R> streamFunction) { |
464 |
return stream(defaultBufferSize(), publisher, streamFunction); |
465 |
} |
466 |
} |