Package io.vertx.rx.java
Class RxHelper
- java.lang.Object
-
- io.vertx.rx.java.RxHelper
-
public class RxHelper extends Object
A set of helpers for RxJava and Vert.x.- Author:
- Julien Viet
-
-
Constructor Summary
Constructors Constructor Description RxHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Scheduler
blockingScheduler(Vertx vertx)
Create a scheduler for aVertx
object, actions can be blocking, they are not executed on Vertx event loop.static Scheduler
blockingScheduler(Vertx vertx, boolean ordered)
Create a scheduler for aVertx
object, actions can be blocking, they are not executed on Vertx event loop.static <T> ObservableFuture<T>
observableFuture()
Create a newObservableFuture<T>
object: anObservable
implementation implementingHandler<AsyncResult<T>>
.static <T> ObservableHandler<T>
observableHandler()
static <T> ObservableHandler<T>
observableHandler(boolean multi)
static Scheduler
scheduler(Context context)
Create a scheduler for aContext
, actions are executed on the event loop of this context.static Scheduler
scheduler(Vertx vertx)
Create a scheduler for aVertx
object, actions are executed on the event loop.static RxJavaSchedulersHook
schedulerHook(Context context)
Create a scheduler hook for aContext
object, theRxJavaSchedulersHook.getIOScheduler()
uses a blocking scheduler.static RxJavaSchedulersHook
schedulerHook(Vertx vertx)
Create a scheduler hook for aVertx
object, theRxJavaSchedulersHook.getIOScheduler()
uses a blocking scheduler.static <T> Handler<AsyncResult<T>>
toFuture(Action1<T> onNext)
Adapt an item callback as aHandler<AsyncResult<T>>
.static <T> Handler<AsyncResult<T>>
toFuture(Action1<T> onNext, Action1<Throwable> onError)
Adapt an item callback and an error callback as aHandler<AsyncResult<T>>
.static <T> Handler<AsyncResult<T>>
toFuture(Action1<T> onNext, Action1<Throwable> onError, Action0 onComplete)
Adapt an item callback and an error callback as aHandler<AsyncResult<T>>
.static <T> Handler<AsyncResult<T>>
toFuture(Observer<T> observer)
Adapt aSubscriber
as aHandler<AsyncResult<T>>;
.static <T> Handler<T>
toHandler(Action1<T> onNext)
Adapt an item callback as aHandler<T>
.static <T> Handler<T>
toHandler(Observer<T> observer)
Adapt aSubscriber
as aHandler<T>;
.static <T> Handler<T>
toHandler(Observer<T> observer, boolean multi)
Adapt aSubscriber
as aHandler<T>;
.static <T> Observable<T>
toObservable(ReadStream<T> stream)
Adapts a Vert.xReadStream
to an RxJavaObservable
.static <T> Observable<T>
toObservable(ReadStream<T> stream, int maxBufferSize)
Adapts a Vert.xReadStream
to an RxJavaObservable
.static <T,R>
Observable<R>toObservable(ReadStream<T> stream, java.util.function.Function<T,R> adapter)
LiketoObservable(ReadStream)
but with a function that adapts the items.static <T,R>
Observable<R>toObservable(ReadStream<T> stream, java.util.function.Function<T,R> adapter, int maxBufferSize)
LiketoObservable(ReadStream, int)
but with a function that adapts the items.static <T> ReadStream<T>
toReadStream(Observable<T> observable)
Adapts an RxJavaObservable
to a Vert.xReadStream
.static <T> Subscriber<T>
toSubscriber(Handler<AsyncResult<T>> handler)
Adapts a Vert.xHandler<AsyncResult<T>>
to an RxJavaSubscriber
.static <T> WriteStreamSubscriber<T>
toSubscriber(WriteStream<T> stream)
Adapts a Vert.xWriteStream
to an RxJavaSubscriber
.static <R,T>
WriteStreamSubscriber<R>toSubscriber(WriteStream<T> stream, java.util.function.Function<R,T> mapping)
LiketoSubscriber(WriteStream)
, except the providedmapping
function is applied to eachObservable
item.static <T> Observable.Operator<T,Buffer>
unmarshaller(TypeReference<T> mappedTypeRef)
Returns a json unmarshaller for the specified java type as aObservable.Operator
instance.static <T> Observable.Operator<T,Buffer>
unmarshaller(TypeReference<T> mappedTypeRef, ObjectCodec mapper)
Returns a unmarshaller for the specified java type as aObservable.Operator
instance given the the providedObjectMapper
static <T> Observable.Operator<T,Buffer>
unmarshaller(Class<T> mappedType)
Returns a json unmarshaller for the specified java type as aObservable.Operator
instance.static <T> Observable.Operator<T,Buffer>
unmarshaller(Class<T> mappedType, ObjectCodec mapper)
Returns a unmarshaller for the specified java type as aObservable.Operator
instance given the the providedObjectMapper
-
-
-
Method Detail
-
toSubscriber
public static <T> Subscriber<T> toSubscriber(Handler<AsyncResult<T>> handler)
Adapts a Vert.xHandler<AsyncResult<T>>
to an RxJavaSubscriber
.The returned subscriber can be subscribed to an
Observable.subscribe(Subscriber)
orSingle.subscribe(Subscriber)
.- Parameters:
handler
- the handler to adapt- Returns:
- the subscriber
-
toReadStream
public static <T> ReadStream<T> toReadStream(Observable<T> observable)
Adapts an RxJavaObservable
to a Vert.xReadStream
. The returned readstream will be subscribed to theObservable
.- Parameters:
observable
- the observable to adapt- Returns:
- the adapted stream
-
toObservable
public static <T> Observable<T> toObservable(ReadStream<T> stream)
Adapts a Vert.xReadStream
to an RxJavaObservable
. After the stream is adapted to an observable, the original stream handlers should not be used anymore as they will be used by the observable adapter.The adapter supports reactive pull back-pressure.
When back-pressure is enabled, a buffer of
ObservableReadStream.DEFAULT_MAX_BUFFER_SIZE
items is maintained:- When the buffer is full, the stream is paused
- When the buffer is half empty, the stream is resumed
- Parameters:
stream
- the stream to adapt- Returns:
- the adapted observable
-
toObservable
public static <T> Observable<T> toObservable(ReadStream<T> stream, int maxBufferSize)
Adapts a Vert.xReadStream
to an RxJavaObservable
. After the stream is adapted to an observable, the original stream handlers should not be used anymore as they will be used by the observable adapter.The adapter supports reactive pull back-pressure.
When back-pressure is enabled, a buffer of
maxBufferSize
items is maintained:- When the buffer is full, the stream is paused
- When the buffer is half empty, the stream is resumed
- Parameters:
stream
- the stream to adaptmaxBufferSize
- the max size of the buffer used when back-pressure is active- Returns:
- the adapted observable
-
toObservable
public static <T,R> Observable<R> toObservable(ReadStream<T> stream, java.util.function.Function<T,R> adapter)
LiketoObservable(ReadStream)
but with a function that adapts the items.
-
toObservable
public static <T,R> Observable<R> toObservable(ReadStream<T> stream, java.util.function.Function<T,R> adapter, int maxBufferSize)
LiketoObservable(ReadStream, int)
but with a function that adapts the items.
-
observableFuture
public static <T> ObservableFuture<T> observableFuture()
Create a newObservableFuture<T>
object: anObservable
implementation implementingHandler<AsyncResult<T>>
. When the async result handler completes, the observable will produce the result and complete immediatly after, when it fails it will signal the error.- Returns:
- the observable future.
-
observableHandler
public static <T> ObservableHandler<T> observableHandler()
Create a newObservableHandler<T>
object: anObservable
implementation implementingHandler<T>
. When the event handler completes, the observable will produce the event and complete immediatly after.- Returns:
- the observable future.
-
observableHandler
public static <T> ObservableHandler<T> observableHandler(boolean multi)
Create a newObservableHandler<T>
object: anObservable
implementation implementingHandler<T>
. When parameter is false and the event handler completes, the observable will produce the event and complete immediatly after, as a single event is expected.- Parameters:
multi
- true if the handler can emit multiple events- Returns:
- the observable future.
-
toFuture
public static <T> Handler<AsyncResult<T>> toFuture(Observer<T> observer)
Adapt aSubscriber
as aHandler<AsyncResult<T>>;
.- Parameters:
observer
- the subscriber to adapt- Returns:
- a
Handler<AsyncResult<T>>
-
toHandler
public static <T> Handler<T> toHandler(Observer<T> observer)
Adapt aSubscriber
as aHandler<T>;
. When the event handler completes, the observer will complete immediatly after the event is received, as a single event is expected.- Parameters:
observer
- the subscriber to adapt- Returns:
- a
Handler<T>
-
toHandler
public static <T> Handler<T> toHandler(Observer<T> observer, boolean multi)
Adapt aSubscriber
as aHandler<T>;
. When parameter is false and the event handler completes, the observer will complete immediatly after the event is received, as a single event is expected.- Parameters:
observer
- the subscriber to adaptmulti
- true if the handler can emit multiple events- Returns:
- a
Handler<T>
-
toFuture
public static <T> Handler<AsyncResult<T>> toFuture(Action1<T> onNext)
Adapt an item callback as aHandler<AsyncResult<T>>
.- Parameters:
onNext
- theAction1<T>
you have designed to accept the resolution from theHandler<AsyncResult<T>>
- Returns:
- a
Handler<AsyncResult<T>>
-
toHandler
public static <T> Handler<T> toHandler(Action1<T> onNext)
Adapt an item callback as aHandler<T>
.- Parameters:
onNext
- theAction1<T>
you have designed to accept the resolution from theHandler<T>
- Returns:
- a
Handler<T>
-
toFuture
public static <T> Handler<AsyncResult<T>> toFuture(Action1<T> onNext, Action1<Throwable> onError)
Adapt an item callback and an error callback as aHandler<AsyncResult<T>>
.- Parameters:
onNext
- theAction1<T>
you have designed to accept the resolution from theHandler<AsyncResult<T>>
onError
- theAction1<Throwable>
you have designed to accept the eventual failure from theHandler<AsyncResult<T>>
- Returns:
- a
Handler<AsyncResult<T>>
-
toFuture
public static <T> Handler<AsyncResult<T>> toFuture(Action1<T> onNext, Action1<Throwable> onError, Action0 onComplete)
Adapt an item callback and an error callback as aHandler<AsyncResult<T>>
.- Parameters:
onNext
- theAction1<T>
you have designed to accept the resolution from theHandler<AsyncResult<T>>
onError
- theAction1<Throwable>
you have designed to accept the eventual failure from theHandler<AsyncResult<T>>
onComplete
- theAction0
you have designed to accept a completion notification from theHandler<AsyncResult<T>>
- Returns:
- a
Handler<AsyncResult<T>>
-
scheduler
public static Scheduler scheduler(Vertx vertx)
Create a scheduler for aVertx
object, actions are executed on the event loop.- Parameters:
vertx
- the vertx object- Returns:
- the scheduler
-
scheduler
public static Scheduler scheduler(Context context)
Create a scheduler for aContext
, actions are executed on the event loop of this context.- Parameters:
context
- the context object- Returns:
- the scheduler
-
blockingScheduler
public static Scheduler blockingScheduler(Vertx vertx)
Create a scheduler for aVertx
object, actions can be blocking, they are not executed on Vertx event loop.- Parameters:
vertx
- the vertx object- Returns:
- the scheduler
-
blockingScheduler
public static Scheduler blockingScheduler(Vertx vertx, boolean ordered)
Create a scheduler for aVertx
object, actions can be blocking, they are not executed on Vertx event loop.- Parameters:
vertx
- the vertx objectordered
- if true then if when tasks are scheduled several times on the same context, the executions for that context will be executed serially, not in parallel. if false then they will be no ordering guarantees- Returns:
- the scheduler
-
schedulerHook
public static RxJavaSchedulersHook schedulerHook(Context context)
Create a scheduler hook for aContext
object, theRxJavaSchedulersHook.getIOScheduler()
uses a blocking scheduler.- Parameters:
context
- the context object- Returns:
- the scheduler hook
-
schedulerHook
public static RxJavaSchedulersHook schedulerHook(Vertx vertx)
Create a scheduler hook for aVertx
object, theRxJavaSchedulersHook.getIOScheduler()
uses a blocking scheduler.- Parameters:
vertx
- the vertx object- Returns:
- the scheduler hook
-
unmarshaller
public static <T> Observable.Operator<T,Buffer> unmarshaller(Class<T> mappedType, ObjectCodec mapper)
Returns a unmarshaller for the specified java type as aObservable.Operator
instance given the the providedObjectMapper
The marshaller can be used with theObservable.lift(rx.Observable.Operator)
method to transform a Observable<Buffer> into a Observable<T>. The unmarshaller buffers the content until onComplete is called, then unmarshalling happens. Note that the returned observable will emit at most a single object.- Parameters:
mappedType
- the type to unmarshallmapper
- the mapper to use to unmarshell- Returns:
- the unmarshaller operator
-
unmarshaller
public static <T> Observable.Operator<T,Buffer> unmarshaller(Class<T> mappedType)
Returns a json unmarshaller for the specified java type as aObservable.Operator
instance. The marshaller can be used with theObservable.lift(rx.Observable.Operator)
method to transform a Observable<Buffer> into a Observable<T>. The unmarshaller buffers the content until onComplete is called, then unmarshalling happens. Note that the returned observable will emit at most a single object.- Parameters:
mappedType
- the type to unmarshall- Returns:
- the unmarshaller operator
-
unmarshaller
public static <T> Observable.Operator<T,Buffer> unmarshaller(TypeReference<T> mappedTypeRef)
Returns a json unmarshaller for the specified java type as aObservable.Operator
instance. The marshaller can be used with theObservable.lift(rx.Observable.Operator)
method to transform a Observable<Buffer> into a Observable<T>. The unmarshaller buffers the content until onComplete is called, then unmarshalling happens. Note that the returned observable will emit at most a single object.- Parameters:
mappedTypeRef
- the type reference to unmarshall- Returns:
- the unmarshaller operator
-
unmarshaller
public static <T> Observable.Operator<T,Buffer> unmarshaller(TypeReference<T> mappedTypeRef, ObjectCodec mapper)
Returns a unmarshaller for the specified java type as aObservable.Operator
instance given the the providedObjectMapper
The marshaller can be used with theObservable.lift(rx.Observable.Operator)
method to transform a Observable<Buffer> into a Observable<T>. The unmarshaller buffers the content until onComplete is called, then unmarshalling happens. Note that the returned observable will emit at most a single object.- Parameters:
mappedTypeRef
- the type reference to unmarshall- Returns:
- the unmarshaller operator
-
toSubscriber
public static <T> WriteStreamSubscriber<T> toSubscriber(WriteStream<T> stream)
Adapts a Vert.xWriteStream
to an RxJavaSubscriber
.After subscription, the original
WriteStream
handlers should not be used anymore as they will be used by the adapter.- Parameters:
stream
- the stream to adapt- Returns:
- the adapted
Subscriber
-
toSubscriber
public static <R,T> WriteStreamSubscriber<R> toSubscriber(WriteStream<T> stream, java.util.function.Function<R,T> mapping)
LiketoSubscriber(WriteStream)
, except the providedmapping
function is applied to eachObservable
item.
-
-