Nic Cottrell
Nic Cottrell

Reputation: 9685

How to load the Java Mongo driver's MongoClientOptions from a file?

It feels weird to configure MongoClientOptions inside my code like options.socketTimeout(30000); where I have to recompile to change a setting in production. I can't see any good way to load config via the builder from an XML or YAML file.

Am I missing something in the docs, or is there a neat implementation/snippet someone can share?

Upvotes: 1

Views: 259

Answers (1)

Dima
Dima

Reputation: 4128

I don't think there is anything in the driver for managing how and where you get these settings from. Not sure it's drivers' job.. I keep properties file with needed mongo settings and do something like this:

import org.apache.commons.configuration.PropertiesConfiguration;
PropertiesConfiguration props = new PropertiesConfiguration();
props.load(fileName);

MongoClientOptions copts = MongoClientOptions.builder()
   .connectionsPerHost(props.getInt("connectionsPerHost", 100))
   .connectTimeout(props.getInt("connectTimeout", 10000))
   ...
   .build();

Upvotes: 2

Related Questions