Class ReadStreamSubscriber<R,J>
- java.lang.Object
-
- rx.Subscriber<R>
-
- io.vertx.rx.java.ReadStreamSubscriber<R,J>
-
- All Implemented Interfaces:
ReadStream<J>
,StreamBase
,Observer<R>
,Subscription
public class ReadStreamSubscriber<R,J> extends Subscriber<R> implements ReadStream<J>
An RxJavaSubscriber
that turns anObservable
into aReadStream
.The stream implements the
pause()
andresume()
operation by maintaining a buffer ofBUFFER_SIZE
elements between theObservable
and theReadStream
.When the subscriber is created it requests
0
elements to activate the subscriber's back-pressure. Setting the handler initially on theReadStream
triggers a request ofBUFFER_SIZE
elements. When the item buffer is half empty, new elements are requested to fill the buffer back toBUFFER_SIZE
elements.The
#endHandler(Handler
is called when the) Observable
is completed or has failed and no pending elements, emitted before the completion or failure, are still in the buffer, i.e the handler is not called when the stream is paused.- Author:
- Julien Viet
-
-
Field Summary
Fields Modifier and Type Field Description static int
BUFFER_SIZE
-
Constructor Summary
Constructors Constructor Description ReadStreamSubscriber(java.util.function.Function<R,J> adapter, java.util.function.Consumer<Subscriber<R>> doSubscribe)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <R,J>
ReadStream<J>asReadStream(Observable<R> observable, java.util.function.Function<R,J> adapter)
ReadStream<J>
endHandler(Handler<Void> handler)
Set an end handler.ReadStream<J>
exceptionHandler(Handler<Throwable> handler)
Set an exception handler on the read stream.ReadStream<J>
fetch(long amount)
Fetch the specifiedamount
of elements.ReadStream<J>
handler(Handler<J> handler)
Set a data handler.void
onCompleted()
void
onError(Throwable e)
void
onNext(R item)
ReadStream<J>
pause()
Pause theReadStream
, it sets the buffer infetch
mode and clears the actual demand.ReadStream<J>
resume()
Resume reading, and sets the buffer inflowing
mode.-
Methods inherited from class rx.Subscriber
add, isUnsubscribed, onStart, request, setProducer, unsubscribe
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.vertx.core.streams.ReadStream
collect, pipe, pipeTo
-
-
-
-
Field Detail
-
BUFFER_SIZE
public static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ReadStreamSubscriber
public ReadStreamSubscriber(java.util.function.Function<R,J> adapter, java.util.function.Consumer<Subscriber<R>> doSubscribe)
-
-
Method Detail
-
asReadStream
public static <R,J> ReadStream<J> asReadStream(Observable<R> observable, java.util.function.Function<R,J> adapter)
-
handler
public ReadStream<J> handler(Handler<J> handler)
Description copied from interface:ReadStream
Set a data handler. As data is read, the handler will be called with the data.- Specified by:
handler
in interfaceReadStream<R>
- Returns:
- a reference to this, so the API can be used fluently
-
pause
public ReadStream<J> pause()
Description copied from interface:ReadStream
Pause theReadStream
, it sets the buffer infetch
mode and clears the actual demand.While it's paused, no data will be sent to the data
handler
.- Specified by:
pause
in interfaceReadStream<R>
- Returns:
- a reference to this, so the API can be used fluently
-
fetch
public ReadStream<J> fetch(long amount)
Description copied from interface:ReadStream
Fetch the specifiedamount
of elements. If theReadStream
has been paused, reading will recommence with the specifiedamount
of items, otherwise the specifiedamount
will be added to the current stream demand.- Specified by:
fetch
in interfaceReadStream<R>
- Returns:
- a reference to this, so the API can be used fluently
-
resume
public ReadStream<J> resume()
Description copied from interface:ReadStream
Resume reading, and sets the buffer inflowing
mode. If theReadStream
has been paused, reading will recommence on it.- Specified by:
resume
in interfaceReadStream<R>
- Returns:
- a reference to this, so the API can be used fluently
-
endHandler
public ReadStream<J> endHandler(Handler<Void> handler)
Description copied from interface:ReadStream
Set an end handler. Once the stream has ended, and there is no more data to be read, this handler will be called.- Specified by:
endHandler
in interfaceReadStream<R>
- Returns:
- a reference to this, so the API can be used fluently
-
exceptionHandler
public ReadStream<J> exceptionHandler(Handler<Throwable> handler)
Description copied from interface:ReadStream
Set an exception handler on the read stream.- Specified by:
exceptionHandler
in interfaceReadStream<R>
- Specified by:
exceptionHandler
in interfaceStreamBase
- Parameters:
handler
- the exception handler- Returns:
- a reference to this, so the API can be used fluently
-
onCompleted
public void onCompleted()
- Specified by:
onCompleted
in interfaceObserver<R>
-
-