public class Rx extends Exec
Reactive programming via rxjava.
RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.
Observable (and family) into Deferred API.
schedulers and make sure they go down on application
shutdown time.
...
import org.jooby.rx.Rx;
...
{
use(new Rx());
get("/", req -> Observable.from("reactive programming in jooby!"))
.map(Rx.rx());
}
Previous example is translated to:
{
use(new Rx());
get("/", req -> {
return new Deferred(deferred -> {
Observable.from("reactive programming in jooby!")
.subscribe(deferred::resolve, deferred::reject);
});
});
}
Translation is done with the rx() route operator. If you are a
rxjava programmer then you don't need to worry
for learning a new API and semantic. The rx() route operator deal and take cares of
the Deferred API.
We just learn that we are not force to learn a new API, just write rxjava code. That's cool!
But.. what if you have 10 routes? 50 routes?
...
import org.jooby.rx.Rx;
...
{
use(new Rx());
get("/1", req -> Observable...)
.map(Rx.rx());
get("/2", req -> Observable...)
.map(Rx.rx());
....
get("/N", req -> Observable...)
.map(Rx.rx());
}
This is better than written N routes using the Deferred API route by route... but still
there is one more option to help you (and your fingers) to right less code:
...
import org.jooby.rx.Rx;
...
{
use(new Rx());
with(() -> {
get("/1", req -> Observable...);
get("/2", req -> Observable...);
....
get("/N", req -> Observable...);
}).map(Rx.rx());
}
Beautiful, hugh?
The with operator let you group any number of routes and apply
common attributes and/or operator to all them!!!
You can provide a Scheduler to the rx() operator:
...
import org.jooby.rx.Rx;
...
{
use(new Rx());
with(() -> {
get("/1", req -> Observable...);
get("/2", req -> Observable...);
....
get("/N", req -> Observable...);
}).map(Rx.rx(Schedulers::io));
}
All the routes here will subscribe-on the
provided Scheduler.
This module provides the default Scheduler from
rxjava. But also let you define your own
scheduler using the Exec module.
rx.schedulers.io = forkjoin rx.schedulers.computation = fixed rx.schedulers.newThread = "fixed = 10"
The previous example defines a:
Schedulers.io()Schedulers.computation()Schedulers.newThread()
Of course, you can define/override all, some or none of them. In any case the Scheduler
will be shutdown at application shutdown time.
| Constructor and Description |
|---|
Rx() |
| Modifier and Type | Method and Description |
|---|---|
com.typesafe.config.Config |
config() |
void |
configure(org.jooby.Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder) |
static org.jooby.Route.Mapper<Object> |
rx()
Map a rx object like
Observable, Single or Completable into a
Deferred object. |
static org.jooby.Route.Mapper<Object> |
rx(Supplier<rx.Scheduler> subscribeOn)
Map a rx object like
Observable, Single or Completable into a
Deferred object. |
public static org.jooby.Route.Mapper<Object> rx()
Observable, Single or Completable into a
Deferred object.
...
import org.jooby.rx.Rx;
...
{
use(new Rx());
with(() -> {
get("/1", req -> Observable...);
get("/2", req -> Observable...);
....
get("/N", req -> Observable...);
}).map(Rx.rx());
}
public static org.jooby.Route.Mapper<Object> rx(Supplier<rx.Scheduler> subscribeOn)
Observable, Single or Completable into a
Deferred object.
...
import org.jooby.rx.Rx;
...
{
use(new Rx());
with(() -> {
get("/1", req -> Observable...);
get("/2", req -> Observable...);
....
get("/N", req -> Observable...);
}).map(Rx.rx());
}
subscribeOn - An scheduler to subscribeOn.public void configure(org.jooby.Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder)
Copyright © 2016. All rights reserved.