[2.4.x] Best approach for asynchronous MongoDB with Java

764 views
Skip to first unread message

juan....@reduc.edu.cu

unread,
Dec 26, 2015, 2:28:48 AM12/26/15
to play-fr...@googlegroups.com
Hi. I am starting with mongodb in play framework 2.4.x for java. I took a
look at the modules directory and the three mongodb modules available for
Java use synchronous approaches, with no support for asynchronous features
of latest mongodb Java driver. What is the best approach you wold follow
to go asynchronous?

Cédric Chantepie

unread,
Dec 31, 2015, 3:35:18 AM12/31/15
to play-framework, juan....@reduc.edu.cu
Hi, you can have a look at the official Scala driver or at ReactiveMongo

juan....@reduc.edu.cu

unread,
Dec 31, 2015, 12:37:48 PM12/31/15
to play-fr...@googlegroups.com
> Hi, you can have a look at the official Scala driver or at ReactiveMongo
> <http://reactivemongo.org/>
>

Hi, thanks. I did it, but it is for Scala, not Java, and I am a Java
developer, not a Scala one. I read it is possible to mix both languages
but have no idea about how to do it. I tried writing my models in Scala,
ok. But what about using Reactivemongo API in Java controllers?

Is it possible? Is it practical? Do you know about a working example?

Thanks again, and happy new year for you and other play developers on the
list.

Greg Methvin

unread,
Jan 1, 2016, 10:52:28 PM1/1/16
to play-framework, juan....@reduc.edu.cu
I've never used it, but the reactive streams driver seems like a good one to try.

juan....@reduc.edu.cu

unread,
Jan 2, 2016, 1:34:34 AM1/2/16
to play-fr...@googlegroups.com
Hi. I think so, but it takes me to the same situation as using
mongodb-driver-async: I have to deal with the driver directly, which means
no masrshaling/unmarshaling, no cute annotations on models, etc.

Until date, Jongo, Mongojack and Morphia have no support for async driver,
only sync. I hope they add it soon, but what until then?

I tried to directly marshall/unmarshall models with
Jackson...unsuccessfully. Maybe I just need to do a deeper research on
Jackson marshaling :(. I then tried using reactivemongo from
Java...unsuccessfully (I better start learning Sacala before trying again
:)...but then I will use it from Scala ;)).

> I've never used it, but the reactive streams driver
> <https://github.com/mongodb/mongo-java-driver-reactivestreams> seems like

Greg Methvin

unread,
Jan 2, 2016, 5:57:06 PM1/2/16
to play-framework
For Jackson you might want to try https://github.com/ylemoigne/mongo-jackson-codec.



--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/AOcNw8YM3es/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/658da173088917ba211fe55e121aa8d5.squirrel%40webmail1.reduc.edu.cu.
For more options, visit https://groups.google.com/d/optout.

juan....@reduc.edu.cu

unread,
Jan 3, 2016, 4:01:11 AM1/3/16
to play-fr...@googlegroups.com
Thanks a lot, I finally made it. The example provided in the README[1] is
only valid for the sync driver. To use mongo-jackson-codec with the async
one you can not just call MongoClient.getDefaultCodecRegistry() or create
a new MongoClient instance, as MongoClient is an interface instead of a
class in the async driver, and the only implementation is the
MongoClientImpl class, which is not public. You have to use MongoClients
factory and set up the CodecRegistry trough a MongoClientSettings
instance. This is how my database access module looks like (a lot of code
just copied from the MongoClients and MongoClientImpl classes):

-----------------------------------------------------------
ConnectionString connectionString = new ConnectionString(
configuration.getString("mongodb.uri", "mongodb://127.0.0.1:27017/test")
);

CodecRegistry defaultCodecRegistry = CodecRegistries
.fromProviders(Arrays.asList(new CodecProvider[]{
new ValueCodecProvider(),
new DocumentCodecProvider(),
new BsonValueCodecProvider()
}));

CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
defaultCodecRegistry, CodecRegistries.fromProviders(
new ObjectCodecProvider(
ObjectMapperFactory.createObjectMapper()
)
)
);

MongoClientSettings clientSettings = MongoClientSettings.builder()
.clusterSettings(
ClusterSettings.builder()
.applyConnectionString(connectionString)
.build()
)
.connectionPoolSettings(
ConnectionPoolSettings.builder()
.applyConnectionString(connectionString)
.build()
)
.serverSettings(ServerSettings.builder().build())
.credentialList(connectionString.getCredentialList())
.sslSettings(SslSettings.builder()
.applyConnectionString(connectionString).build()
)
.socketSettings(SocketSettings.builder()
.applyConnectionString(connectionString)
.build()
)
.codecRegistry(codecRegistry)
.build();

MongoClient mongoClient = MongoClients.create(clientSettings);

MongoDatabase database = mongoClient.getDatabase(
connectionString.getDatabase()
);
-------------------------------------------------------------

Maybe there is a simpler way of doing this, maybe with a more recent
version of the driver.

[1] https://github.com/ylemoigne/mongo-jackson-codec/blob/master/README.md
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to play-framewor...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/play-framework/CAO2h8AxdOvzXTbePf%3DpUgPc-StNf8sFVBzB_nc-KAKH_LNLWTA%40mail.gmail.com.

Roshan Kumar

unread,
Jun 3, 2016, 7:31:37 AM6/3/16
to play-framework, juan....@reduc.edu.cu

If you want to ODM that use java async driver you can use it

https://github.com/raimdtu/PlayAsyncJavaMongoODM

It will give you overview.

Reply all
Reply to author
Forward
0 new messages