Vert.x module which uses RxJava to add support for Reactive Extensions (RX) using the RxJava library. This allows VertX developers to use the RxJava type-safe composable API to build VertX verticles.
- The module wraps the VertX core objects to add Observable support so it is tightly bound to the VertX release.
- This module also contains the Netflix RxJava library.
Currently Observable wrappers are provided for
- EventBus
- HttpServer
- HttpClient
- NetServer
- NetClient
- Timer
There are also base Observable adapters that map Handler and AsyncResultHandler to Observable that can be used to call other Handler based APIs.
Support coming soon for
- FileSystem
- SockJSServer
This is a non-runnable module, which means you add it to your module via the "includes" attribute of mod.json.
All standard API methods of the form
void method(args...,Handler<T> handler)
are typically available in the form
Observable<T> method(args...)
where the operation is executed immediately or
Observable<T> observeMethod(args...)
where the operation is executed on subscribe. This latter form is the more 'pure' Rx method and should be used where possible (required to maintain semantics of concat eg)
RxEventBus rxEventBus = new RxEventBus(vertx.eventBus());
rxEventBus.<String>registerHandler("foo").subscribe(new Action1<RxMessage<String>>() {
public void call(RxMessage<String> message) {
// Send a single reply
message.reply("pong!");
}
});
Observable<RxMessage<String>> obs = rxEventBus.send("foo", "ping!");
obs.subscribe(
new Action1<RxMessage<String>>() {
public void call(RxMessage<String> message) {
// Handle response
}
},
new Action1<Throwable>() {
public void call(Throwable err) {
// Handle error
}
}
);
The timer functions are provided via the RxVertx wrapper. The timer is set on-subscribe. To cancel a timer that has not first, or a periodic timer, just unsubscribe.
RxVertx rx = new RxVertx(vertx);
rx.setTimer(100).subscribe(new Action1<Long>() {
public void call(Long t) {
// Timer fired
}
});
The support class RxSupport
provides several helper methods for some standard tasks
There are two primary wrappers
Convert a ReadStream
into an Observable<Buffer>
Stream the output of an Observable
to a WriteStream
.
please note that this method does not handle writeQueueFull
so cannot be used as a pump