A library that supports using MongoDB for data persistence in Dropwizard applications.
Java Groovy
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gradle/wrapper
src
.gitignore
.travis.yml
CHANGES.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
README.md
RELEASING.md
SECURITY.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

dropwizard-mongo

Build Status Code Coverage Download

A library that supports using MongoDB for data persistence in Dropwizard applications.

See our security policy for handling of security-related matters.

Usage

First, add a dependency to your build file. Releases are published to Bintray JCenter.

Gradle:

...
repositories {
    jcenter()
}
...
dependencies {
    compile "com.commercehub.dropwizard:dropwizard-mongo:VERSION"
}
...

Maven:

...
<repositories>
  <repository>
    <id>jcenter</id>
    <url>http://jcenter.bintray.com</url>
  </repository>
</repositories>
...
<dependency>
  <groupId>com.commercehub.dropwizard</groupId>
  <artifactId>dropwizard-mongo</artifactId>
  <version>VERSION</version>
</dependency>
...

Next, add a field of type MongoClientFactory to your application's configuration class:

public class AppConfiguration extends Configuration {

    @Valid
    @NotNull
    private MongoClientFactory mongo;

    @JsonProperty
    public MongoClientFactory getMongo() {
        return mongo;
    }

    @JsonProperty
    public void setMongo(MongoClientFactory mongo) {
        this.mongo = mongo;
    }

}

In your Application class, use the MongoClientFactory to build a ManagedMongoClient and DB:

public class App extends Application<AppConfiguration> {

    @Override
    public void run(AppConfiguration config, Environment environment) {
        ManagedMongoClient mongoClient = config.getMongo().build();
        environment.lifecycle().manage(mongoClient);
        MongoDatabase db = mongoClient.getDatabase(config.getMongo().getDbName());
    }

}

Finally, add the necessary bits to your configuration file:

mongo:
  uri: mongodb://localhost:27017/?maxPoolSize=50&maxIdleTimeMS=300000
  dbName: mydb

Compatibility

dropwizard-mongo dropwizard mongo-java-driver java
3.x 0.9.x 2.x 7, 8
4.x 0.9.x 3.x 7, 8
5.x 1.0.x 3.x 8

License

This library is available under the Apache License, Version 2.0.

Copyright © 2014-2016 Commerce Technologies, LLC