MongoDB3.2 数据库连接:
import java.util.ArrayList;
import java.util.List;
import com.dxhr.platform.framework.core.storage.mongo.exception.BadConfigException;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
/**
* @author allen.shen
* @date 2017年4月11日
*
* description:
*
* This MongoFactory is used to new a mongo instance.
*
* If you want to init mongo instance in your spring configure file, do it like
*/
public class MongoFactory {
private MongoConfig config = new MongoConfig();
private MongoClient mongo = null;
private String serverAddressUrls = null;
public MongoFactory() {
}
public MongoFactory(String serverAddress) {
serverAddressUrls = serverAddress;
}
/**
* Creates a Mongo based on a list of replica set members or a list of
* mongos. If you have a standalone server, it will use the
* Mongo(ServerAddress) constructor.
*
* @throws BadConfigException
*/
public MongoClient newMongoInstance() {
List<ServerAddress> seeds = getSeeds();
MongoClientOptions options = null;
if (config.getBooleanProperty("mongo.options.on", false)) {
options = initMongoOptions();
}
if (seeds.size() > 1) {
if (options == null)
mongo = new MongoClient(seeds);
else
mongo = new MongoClient(seeds, options);
} else {
if (options == null)
mongo = new MongoClient(seeds.get(0));
else
mongo = new MongoClient(seeds.get(0), options);
}
return mongo;
}
public MongoClient getMongoClientByCredent(String databaseName){
MongoClient mongoClient;
String user = config.getProperty("username", "test");
String pswd = config.getProperty("password", "test");
List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
MongoCredential credential = MongoCredential.createCredential(user, databaseName, pswd.toCharArray());
credentialList.add(credential);
mongoClient = new MongoClient(getSeeds(), credentialList);
return mongoClient;
}
private List<ServerAddress> getSeeds() {
String servers = null;
if (serverAddressUrls == null) {
servers = config.getProperty("mongo.db.address");
} else {
servers = serverAddressUrls;
}
if (servers != null && servers.length() > 0) {
String[] serverArray = servers.split(",");
String[] host_port = null;
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
for (String server : serverArray) {
host_port = server.split(":");
try {
if (host_port.length == 2) {
seeds.add(new ServerAddress(host_port[0], Integer.parseInt(host_port[1])));
} else {
seeds.add(new ServerAddress(host_port[0], 27017));
}
} catch (NumberFormatException e) {
throw new BadConfigException("Bad mongodb port defined : " + host_port[1]);
}
}
return seeds;
} else {
throw new BadConfigException("No mongodb host defined!");
}
}
private MongoClientOptions initMongoOptions() {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder()
.socketKeepAlive(true)
.cursorFinalizerEnabled(true)
.connectionsPerHost(config.getIntProperty("mongo.options.connectionsPerHost", 10))
.threadsAllowedToBlockForConnectionMultiplier(
config.getIntProperty("mongo.options.threadsAllowedToBlockForConnectionMultiplier", 5))
.connectTimeout(config.getIntProperty("mongo.options.connectTimeout", 10000))
.socketTimeout(config.getIntProperty("mongo.options.socketTimeout", 0));
if (config.getBooleanProperty("mongo.options.readReference.secondary", false)) {
builder.readPreference(ReadPreference.secondaryPreferred());
}
return builder.build();
}
}