Commit 6fa953b3 by jinfeng.wang

fix

parent 01e994a8
package com.mobvista.apptag.utils;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ReadPreference;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @package: com.mobvista.apptag.utils
......@@ -25,22 +23,22 @@ import java.util.Set;
public class GetPackageFromMongo {
public static void main(String[] args) {
try {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
// build the connection options,set the max wait time in (ms)
builder.readPreference(ReadPreference.primaryPreferred())
.retryWrites(true)
.maxConnectionIdleTime(6000);
MongoClientOptions opts = builder.build();
// MongoCredential credential2 = MongoCredential.createCredential("username", "databasename",password2);
String mongoUrl = "internal-beijing-adServerMongo-virginia-1390640500.us-east-1.elb.amazonaws.com";
//add your option to the connection
// MongoClientOptions.Builder builder = new MongoClientOptions.Builder()
MongoClientSettings settings = MongoClientSettings.builder()
.retryWrites(true)
.applyToConnectionPoolSettings(builder ->
builder.maxConnectionIdleTime(6000, TimeUnit.MILLISECONDS))
.applyToSslSettings(builder -> builder.enabled(true))
.applyToClusterSettings(builder -> {
builder.hosts(Arrays.asList(new ServerAddress(mongoUrl, 27017)));
}).build();
// MongoClient mongoClient = new MongoClient(new ServerAddress("server ip",27017), Arrays.asList(credential2),opts);
// 连接到 mongodb 服务
String mongoUrl = "internal-beijing-adServerMongo-virginia-1390640500.us-east-1.elb.amazonaws.com";
MongoClient mongoClient = new MongoClient(new ServerAddress(mongoUrl, 27017), opts);
MongoClient mongoClient = MongoClients.create(settings);
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("new_adn");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment