public class Reactor extends Object implements org.jooby.Jooby.Module
Reactor is a second-generation Reactive library for building non-blocking applications on the JVM based on the Reactive Streams Specification
Flux and Mono into Deferred API.
...
import org.jooby.reactor.Reactor;
...
{
use(new Reactor());
get("/", req -> Flux.just("reactive programming in jooby!"))
.map(Reactor.reactor());
}
Previous example is translated to:
{
use(new Reactor());
get("/", req -> {
return new Deferred(deferred -> {
Flux.just("reactive programming in jooby!")
.consume(deferred::resolve, deferred::reject);
});
});
}
Translation is done with the reactor() route operator. If you are a
reactor programmer then you don't need to worry
for learning a new API and semantic. The reactor() 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 reactor code. That's cool!
But.. what if you have 10 routes? 50 routes?
...
import org.jooby.reactor.Reactor;
...
{
use(new Reactor());
get("/1", req -> Observable...)
.map(Reactor.reactor());
get("/2", req -> Observable...)
.map(Reactor.reactor());
....
get("/N", req -> Observable...)
.map(Reactor.reactor());
}
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.reactor.Reactor;
...
{
use(new Reactor());
with(() -> {
get("/1", req -> Observable...);
get("/2", req -> Observable...);
....
get("/N", req -> Observable...);
}).map(Reactor.reactor());
}
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 reactor(Supplier) operator:
...
import org.jooby.reactor.Reactor;
...
{
use(new Reactor());
with(() -> {
get("/1", req -> Observable...);
get("/2", req -> Observable...);
....
get("/N", req -> Observable...);
}).map(Reactor.reactor(Computations::concurrent));
}
All the routes here will subscribe-on the provided
Scheduler.
| Constructor and Description |
|---|
Reactor() |
| 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> |
reactor()
Map a reactor object like
Flux or Mono into a Deferred object. |
static org.jooby.Route.Mapper<Object> |
reactor(Supplier<reactor.core.scheduler.Scheduler> subscribeOn)
Map a reactor object like
Flux or Mono into a Deferred object. |
public static org.jooby.Route.Mapper<Object> reactor(Supplier<reactor.core.scheduler.Scheduler> subscribeOn)
Flux or Mono into a Deferred object.
...
import org.jooby.reactor.Reactor;
...
{
use(new Reactor());
with(() -> {
get("/1", req -> Observable...);
get("/2", req -> Observable...);
....
get("/N", req -> Observable...);
}).map(Reactor.reactor());
}
subscribeOn - An scheduler to subscribeOn.public static org.jooby.Route.Mapper<Object> reactor()
Flux or Mono into a Deferred object.
...
import org.jooby.reactor.Reactor;
...
{
use(new Reactor());
with(() -> {
get("/1", req -> Observable...);
get("/2", req -> Observable...);
....
get("/N", req -> Observable...);
}).map(Reactor.reactor());
}
public void configure(org.jooby.Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder)
configure in interface org.jooby.Jooby.Modulepublic com.typesafe.config.Config config()
config in interface org.jooby.Jooby.ModuleCopyright © 2016. All rights reserved.