public class Streams extends Object
Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure on the JVM.
Ratpack uses the Reactive Streams API when consuming streams of data (e.g Response.sendStream(org.reactivestreams.Publisher)
).
This class provides some useful reactive utilities that integrate other parts of the Ratpack API with Reactive Stream types. It is not designed to be a fully featured reactive toolkit. If you require more features than provided here, consider using Ratpack's RxJava or Reactor integration.
Constructor and Description |
---|
Streams() |
Modifier and Type | Method and Description |
---|---|
static <T> TransformablePublisher<T> |
buffer(org.reactivestreams.Publisher<T> publisher)
Returns a publisher that allows the given publisher to emit as fast as it can, while applying flow control downstream.
|
static <T> TransformablePublisher<T> |
constant(T item)
Creates a new publisher, that indefinitely streams the given object to all subscribers.
|
static <T> TransformablePublisher<T> |
fanOut(org.reactivestreams.Publisher<Collection<T>> publisher)
Returns a publisher that publishes each element from Collections that are produced from the given input publisher.
|
static <I,O> TransformablePublisher<O> |
flatMap(org.reactivestreams.Publisher<I> input,
Function<? super I,? extends Promise<? extends O>> function)
Returns a publisher that publishes items from the given input publisher after transforming each item via the given, promise returning, function.
|
static <T> TransformablePublisher<T> |
gate(org.reactivestreams.Publisher<T> publisher,
Action<? super Runnable> valveReceiver)
Allows requests from the subscriber of the return publisher to be withheld from the given publisher until an externally defined moment.
|
static <I,O> TransformablePublisher<O> |
map(org.reactivestreams.Publisher<I> input,
Function<? super I,? extends O> function)
Returns a publisher that publishes items from the given input publisher after transforming each item via the given function.
|
static <T> TransformablePublisher<T> |
merge(org.reactivestreams.Publisher<? extends T>... publishers)
Returns a publisher that merges the given input publishers into a single stream of elements.
|
static <T> TransformablePublisher<T> |
multicast(org.reactivestreams.Publisher<T> publisher)
Returns a publisher that will stream events emitted from the given publisher to all of its subscribers.
|
static <T> TransformablePublisher<T> |
periodically(LaunchConfig launchConfig,
long delay,
TimeUnit timeUnit,
Function<Integer,T> producer) |
static <T> TransformablePublisher<T> |
periodically(ScheduledExecutorService executorService,
long delay,
TimeUnit timeUnit,
Function<Integer,T> producer)
Executes the given function periodically, publishing the return value to the subscriber.
|
static <T> TransformablePublisher<T> |
publish(Iterable<T> iterable)
Converts an iterable to a publishable.
|
static <T> Promise<T> |
toPromise(ExecControl execControl,
org.reactivestreams.Publisher<T> publisher)
Creates a promise for the given publisher's single item.
|
static <T> Promise<T> |
toPromise(org.reactivestreams.Publisher<T> publisher)
Calls
toPromise(ExecControl, Publisher) with ExecControl.current() and the given publisher. |
static <T> TransformablePublisher<T> |
transformable(org.reactivestreams.Publisher<T> publisher)
Wraps the publisher in Ratpack's
TransformablePublisher to make composing a pipeline easier. |
static <T> TransformablePublisher<T> |
wiretap(org.reactivestreams.Publisher<T> publisher,
Action<? super StreamEvent<? super T>> listener)
Allows listening to the events of the given publisher as they flow to subscribers.
|
static <T> TransformablePublisher<T> |
yield(Function<? super YieldRequest,T> producer)
Creates a new publisher, backed by the given data producing function.
|
public static <T> TransformablePublisher<T> transformable(org.reactivestreams.Publisher<T> publisher)
TransformablePublisher
to make composing a pipeline easier.
The return publisher is effectively the same publisher in terms of the Publisher.subscribe(org.reactivestreams.Subscriber)
method.
T
- the type of item the publisher emitspublisher
- the publisher to wrappublic static <T> TransformablePublisher<T> publish(Iterable<T> iterable)
Upon subscription, a new iterator will be created from the iterable. Values are pulled from the iterator in accordance with the requests from the subscriber.
Any exception thrown by the iterable/iterator will be forwarded to the subscriber.
T
- the type of item emittediterable
- the data sourcepublic static <T> TransformablePublisher<T> yield(Function<? super YieldRequest,T> producer)
T
- the type of item emittedproducer
- the data sourcepublic static <T> TransformablePublisher<T> constant(T item)
This is rarely useful for anything other than testing.
T
- the type of item emitteditem
- the item to indefinitely streampublic static <I,O> TransformablePublisher<O> map(org.reactivestreams.Publisher<I> input, Function<? super I,? extends O> function)
The returned publisher does not perform any flow control on the data stream.
If the given transformation errors, the exception will be forwarded to the subscriber and the subscription to the input stream will be cancelled.
I
- the type of input itemO
- the type of output iteminput
- the stream of input datafunction
- the transformationpublic static <I,O> TransformablePublisher<O> flatMap(org.reactivestreams.Publisher<I> input, Function<? super I,? extends Promise<? extends O>> function)
The returned publisher does not perform any flow control on the data stream.
If the given transformation errors, or if the returned promise fails, the exception will be forwarded to the subscriber and the subscription to the input stream will be cancelled.
I
- the type of input itemO
- the type of output iteminput
- the stream of input datafunction
- the transformationpublic static <T> TransformablePublisher<T> buffer(org.reactivestreams.Publisher<T> publisher)
When the return publisher is subscribed to, a subscription will be made to the given publisher with a request for Long.MAX_VALUE
items.
This effectively allows the given publisher to emit each item as soon as it can.
The return publisher will manage the back pressure by holding excess items from the given publisher in memory until the downstream subscriber is ready for them.
This is a simple, naive, flow control mechanism. If the given producer emits far faster than the downstream subscriber requests, the intermediate queue will grow large and consume substantial memory. However, it is useful or adapting non-infinite publishers that cannot meaningfully respect back pressure.
T
- the type of itempublisher
- a data sourcepublic static <T> TransformablePublisher<T> gate(org.reactivestreams.Publisher<T> publisher, Action<? super Runnable> valveReceiver)
When the return publisher is subscribed to, the given publisher will be subscribed to. All requests made by the subscriber of the return publisher will not be forwarded to the subscription of the given publisher until the runnable given to the given action is run. Once the runnable is run, all requests are directly forwarded to the subscription of the given publisher.
The return publisher supports multi subscription, creating a new subscription to the given publisher each time. The given action will be invoke each time the return publisher is subscribed to with a distinct runnable for releasing the gate for that subscription.
The given action will be invoked immediately upon subscription of the return publisher. The runnable given to this action may be invoked any time (i.e. it does not need to be run during the action). If the action errors, the given publisher will not be subscribed to and the error will be sent to the return publisher subscriber.
T
- the type of item emittedpublisher
- the data sourcevalveReceiver
- an action that receives a runnable “valve” that when run allows request to start flowing upstreampublic static <T> TransformablePublisher<T> periodically(ScheduledExecutorService executorService, long delay, TimeUnit timeUnit, Function<Integer,T> producer)
When the return publisher is subscribed to, the given function is executed immediately (via the executor) with 0
as the input.
The function will then be repeatedly executed again after the given delay (with an incrementing input) until the function returns null
.
That is, a return value from the function of null
signals that the data stream has finished.
The function will not be executed again after returning null
.
Each new subscription to the publisher will cause the function to be scheduled again. Due to this, it is generally desirable to wrap the return publisher in a multicasting publisher.
If the function throws an exception, the error will be sent to the subscribers and no more invocations of the function will occur.
The returned publisher is implicitly buffered to respect back pressure via buffer(org.reactivestreams.Publisher)
.
T
- the type of itemexecutorService
- the executor service that will periodically execute the functiondelay
- the delay valuetimeUnit
- the delay time unitproducer
- a function that produces values to emitpublic static <T> TransformablePublisher<T> periodically(LaunchConfig launchConfig, long delay, TimeUnit timeUnit, Function<Integer,T> producer)
public static <T> TransformablePublisher<T> wiretap(org.reactivestreams.Publisher<T> publisher, Action<? super StreamEvent<? super T>> listener)
When the return publisher is subscribed to, the given publisher will be subscribed to. All events (incl. data, error and completion) emitted by the given publisher will be forwarded to the given listener before being forward to the subscriber of the return publisher.
If the listener errors, the upstream subscription will be cancelled (if appropriate) and the error sent downstream.
If the listener errors while listening to an error event, the listener error will be added as a surpressed exception
to the original exception which will then be sent downstream.
T
- the type of item emittedpublisher
- the data sourcelistener
- the listener for emitted itemspublic static <T> TransformablePublisher<T> multicast(org.reactivestreams.Publisher<T> publisher)
The return publisher allows the given publisher to emit as fast as it can, while applying flow control downstream to multiple subscribers. Each subscriber can signal its own demand. If the given publisher emits far faster than the downstream subscribers request, the intermediate queue of each subscriber will grow large and consume substantial memory. However, given this publisher is likely to be used with a periodic publisher or a regular indefinite stream it is unlikely to be a problem.
When a subscriber subscribes to the return publisher then it will not receive any events that have been emitted before it subscribed.
T
- the type of itempublisher
- a data sourcepublic static <T> TransformablePublisher<T> fanOut(org.reactivestreams.Publisher<Collection<T>> publisher)
For each item the return publisher receives from the given input publisher, the return publisher will iterate over its elements and publish a new item for each element to its downstream subscriber e.g. if the return publisher receives a Collection with 10 elements then the downstream subscriber will receive 10 calls to its onNext method.
The returned publisher is implicitly buffered to respect back pressure via buffer(org.reactivestreams.Publisher)
.
T
- the type of item emittedpublisher
- the data source@SafeVarargs public static <T> TransformablePublisher<T> merge(org.reactivestreams.Publisher<? extends T>... publishers)
The returned publisher obeys the following rules:
The returned publisher is implicitly buffered to respect back pressure via buffer(org.reactivestreams.Publisher)
.
T
- the type of item emittedpublishers
- the data sources to mergepublic static <T> Promise<T> toPromise(org.reactivestreams.Publisher<T> publisher)
toPromise(ExecControl, Publisher)
with ExecControl.current()
and the given publisher.T
- the type of promised valuepublisher
- the publiser the convert to a promisepublic static <T> Promise<T> toPromise(ExecControl execControl, org.reactivestreams.Publisher<T> publisher)
The given publisher is expected to produce zero or one items.
If it produces zero, the promised value will be null
.
The it produces exactly one item, the promised value will be that item.
If the stream produces more than one item, the promise will fail with an IllegalStateException
.
As soon as a second item is received, the subscription to the given publisher will be cancelled.
The single item is not provided to the promise subscriber until the stream completes, to ensure that it is indeed a one element stream. If the stream errors before sending a second item, the promise will fail with that error. If it fails after sending a second item, that error will be ignored.
T
- the type of promised valueexecControl
- the exec control to create the promise frompublisher
- the publisher to extract the promised item fromtoPromise(Publisher)