public class MongoRx extends Object implements org.jooby.Jooby.Module
MongoDB RxJava Driver: provides composable asynchronous and event-based observable sequences for MongoDB.
A MongoDB based driver providing support for ReactiveX (Reactive Extensions) by using the RxJava library. All database calls return an Observable allowing for efficient execution, concise code, and functional composition of results.
This module depends on Rx module, please read the Rx documentation before using
this module.
MongoClientMongoDatabase (when mongo connection string has a database)MongoCollection (when mongo connection string has a collection)Route.Mapper for mongo observables
import org.jooby.mongodb.MongoRx;
{
// required by MongoRx
use(new Rx());
use(new MongoRx());
get("/", req -> {
MongoClient client = req.require(MongoClient.class);
// work with client:
});
}
The mongo-rx module connects to mongodb://localhost. You can change the
connection string by setting the db property in your
application.conf file:
db = "mongodb://localhost/mydb"
Or at creation time:
{
// required by MongoRx
use(new Rx());
use(new MongoRx("mongodb://localhost/mydb"));
}
If your connection string has a database, then you can require a MongoDatabase object:
{
// required by MongoRx
use(new Rx());
use(new MongoRx("mongodb://localhost/mydb"));
get("/", req -> {
MongoDatabase mydb = req.require(MongoDatabase.class);
return mydb.listCollections();
});
}
And if your connection string has a collection:
{
// required by MongoRx
use(new Rx());
use(new MongoRx("mongodb://localhost/mydb.mycol"));
get("/", req -> {
MongoCollection mycol = req.require(MongoCollection.class);
return mycol.find();
});
}
The module let you return MongoObservable directly as route responses:
{
// required by MongoRx
use(new Rx());
use(new MongoRx()
.observableAdapter(observable -> observable.observeOn(Scheduler.io())));
get("/pets", req -> {
MongoDatabase db = req.require(MongoDatabase.class);
return db.getCollection("pets")
.find();
});
}
Previous example will list all the Pets from a collection. Please note you don't
have to deal with MongoObservable, instead the module converts MongoObservable to
Jooby async semantics.
Multiple databases are supported by adding multiple MongoRx instances to your
application:
{
// required by MongoRx
use(new Rx());
use(new MongoRx("db1"));
use(new MongoRx("db2"));
get("/do-with-db1", req -> {
MongoDatabase db1 = req.require("db1", MongoDatabase.class);
});
get("/do-with-db2", req -> {
MongoDatabase db2 = req.require("db2", MongoDatabase.class);
});
}
The keys db1 and db2 are connection strings in your
application.conf:
db1 = "mongodb://localhost/db1"
db2 = "mongodb://localhost/db2"
ObservableAdapter provides a simple way to adapt all Observables returned by the driver.
On such use case might be to use a different Scheduler after returning the results from MongoDB
therefore freeing up the connection thread.
{
// required by MongoRx
use(new Rx());
use(new MongoRx().observableAdapter(o -> o.observeOn(Schedulers.io())));
}
Any computations on Observables returned by the MongoDatabase or MongoCollection
will use the IO scheduler, rather than blocking the MongoDB Connection thread.
Please note the observableAdapter(Function) works if (and only if) your connection
string points to a database. It won't work on mongo://localhost connection string
because there is no database in it.
Driver options are available via connection string.
It is also possible to configure specific options:
db = "mongodb://localhost/pets"
mongo {
readConcern: default
writeConcern: ACKNOWLEDGED
cluster {
replicaSetName: name
requiredClusterType: REPLICA_SET
}
pool {
maxSize: 100
minSize: 10
}
}
Each option matches a MongoClientSettings method.
| Constructor and Description |
|---|
MongoRx()
Creates a new
MongoRx module that connects to localhost unless you
define/override the db property in your application.conf file. |
MongoRx(String db)
Creates a new
MongoRx module. |
| Modifier and Type | Method and Description |
|---|---|
MongoRx |
codecRegistry(org.bson.codecs.configuration.CodecRegistry codecRegistry)
Set a
CodecRegistry to the MongoDatabase created by this module. |
com.typesafe.config.Config |
config() |
void |
configure(org.jooby.Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder) |
MongoRx |
doWith(BiConsumer<com.mongodb.async.client.MongoClientSettings.Builder,com.typesafe.config.Config> configurer)
Allow further configuration on the
MongoClientSettings. |
MongoRx |
doWith(Consumer<com.mongodb.async.client.MongoClientSettings.Builder> configurer)
Allow further configuration on the
MongoClientSettings. |
MongoRx |
observableAdapter(Function<rx.Observable,rx.Observable> adapter)
Set a
ObservableAdapter to the MongoDatabase created by this module. |
public MongoRx(String db)
MongoRx module.db - A connection string or a property key.public MongoRx()
MongoRx module that connects to localhost unless you
define/override the db property in your application.conf file.public MongoRx doWith(BiConsumer<com.mongodb.async.client.MongoClientSettings.Builder,com.typesafe.config.Config> configurer)
MongoClientSettings.configurer - Configurer callback.public MongoRx doWith(Consumer<com.mongodb.async.client.MongoClientSettings.Builder> configurer)
MongoClientSettings.configurer - Configurer callback.public MongoRx observableAdapter(Function<rx.Observable,rx.Observable> adapter)
ObservableAdapter to the MongoDatabase created by this module.adapter - An ObservableAdapter.public MongoRx codecRegistry(org.bson.codecs.configuration.CodecRegistry codecRegistry)
CodecRegistry to the MongoDatabase created by this module.codecRegistry - A codec registry.public com.typesafe.config.Config config()
config in interface org.jooby.Jooby.Modulepublic void configure(org.jooby.Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder)
configure in interface org.jooby.Jooby.ModuleCopyright © 2020. All rights reserved.