Skip to content

Instantly share code, notes, and snippets.

@JanPaulEttles
Created December 8, 2011 15:13
Show Gist options
  • Select an option

  • Save JanPaulEttles/1447236 to your computer and use it in GitHub Desktop.

Select an option

Save JanPaulEttles/1447236 to your computer and use it in GitHub Desktop.
tomcat and mongo #2
private final Mongo mongo;
public MongoService() throws UnknownHostException, NamingException {
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
int hosts = ContextUtil.getEntryAsInt("mongo.hosts");
for (int i = 0; i < hosts; i++) {
String address = ContextUtil.getEntryAsString("mongo.host.address." + i);
int port = ContextUtil.getEntryAsInt("mongo.host.port." + i);
addrs.add(new ServerAddress(address, port));
}
mongo = new Mongo(addrs);
//if we have more 1 instance, then lets check the slave reads ok param
if(hosts > 1) {
if(ContextUtil.getEntryAsBoolean("mongo.slave")) {
mongo.slaveOk();
}
}
}
public DB getDb(String dbName) {
return mongo.getDB(dbName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment