MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
int frequency = StringUtil.parseInt(PropertiesUtil.getProperty("mongo","frequency"));
if(frequency>0){
builder.heartbeatFrequency(frequency);
}
MongoClientOptions options = builder.build();
String host = PropertiesUtil.getProperty("mongo","host");
int port = Integer.parseInt(PropertiesUtil.getProperty("mongo","port"));
String database = PropertiesUtil.getProperty("mongo","db");
String user = PropertiesUtil.getProperty("mongodb","user");
String password = PropertiesUtil.getProperty("mongodb","password");
ServerAddress serverAddress = new ServerAddress(host, port);
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add(serverAddress);
MongoCredential credentials = MongoCredential.createMongoCRCredential(user, database, password.toCharArray());
List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
credentialsList.add(credentials);
MongoClient mongoClient = new MongoClient(seeds, credentialsList, options);
MongoDatabase db = mongoClient.getDatabase(database);
mongo-javadriver-3.0.1 创建数据库连接
最新推荐文章于 2024-07-30 00:19:15 发布