Example usage for com.mongodb ServerAddress ServerAddress

List of usage examples for com.mongodb ServerAddress ServerAddress

Introduction

In this page you can find the example usage for com.mongodb ServerAddress ServerAddress.

Prototype

public ServerAddress(final InetSocketAddress inetSocketAddress) 

Source Link

Document

Creates a ServerAddress

Usage

From source file:com.novemberain.quartz.mongodb.MongoDBJobStore.java

License:Open Source License

private Mongo connectToMongoDB() throws SchedulerConfigException {

    if (mongoUri != null) {
        return connectToMongoDB(mongoUri);
    }/*w ww .j  a  v  a2  s  .c  om*/

    MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
    optionsBuilder.writeConcern(WriteConcern.SAFE);

    if (mongoOptionAutoConnectRetry != null)
        optionsBuilder.autoConnectRetry(mongoOptionAutoConnectRetry);
    if (mongoOptionMaxConnectionsPerHost != null)
        optionsBuilder.connectionsPerHost(mongoOptionMaxConnectionsPerHost);
    if (mongoOptionConnectTimeoutMillis != null)
        optionsBuilder.connectTimeout(mongoOptionConnectTimeoutMillis);
    if (mongoOptionSocketTimeoutMillis != null)
        optionsBuilder.socketTimeout(mongoOptionSocketTimeoutMillis);
    if (mongoOptionSocketKeepAlive != null)
        optionsBuilder.socketKeepAlive(mongoOptionSocketKeepAlive);
    if (mongoOptionThreadsAllowedToBlockForConnectionMultiplier != null)
        optionsBuilder.threadsAllowedToBlockForConnectionMultiplier(
                mongoOptionThreadsAllowedToBlockForConnectionMultiplier);

    MongoClientOptions options = optionsBuilder.build();

    try {
        ArrayList<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
        for (String a : addresses) {
            serverAddresses.add(new ServerAddress(a));
        }
        return new MongoClient(serverAddresses, options);

    } catch (UnknownHostException e) {
        throw new SchedulerConfigException("Could not connect to MongoDB", e);
    } catch (MongoException e) {
        throw new SchedulerConfigException("Could not connect to MongoDB", e);
    }
}

From source file:com.qwazr.connectors.MongoDbConnector.java

License:Apache License

@Override
public void load(File data_directory) {
    List<ServerAddress> serverAddresses = new ArrayList();
    for (MongoServerAddress server : servers) {
        ServerAddress serverAddress = server.port == null ? new ServerAddress(server.hostname)
                : new ServerAddress(server.hostname, server.port);
        serverAddresses.add(serverAddress);
    }/*from  www  .  ja  v a2s .co  m*/
    if (credentials == null || credentials.isEmpty()) {
        mongoClient = new MongoClient(serverAddresses);
    } else {
        List<MongoCredential> mongoCredentials = new ArrayList<MongoCredential>(credentials.size());
        for (MongoDbCredential credential : credentials)
            mongoCredentials.add(MongoCredential.createMongoCRCredential(credential.username,
                    credential.database, credential.password.toCharArray()));
        mongoClient = new MongoClient(serverAddresses, mongoCredentials);
    }

}

From source file:com.qwazr.library.mongodb.MongoDbConnector.java

License:Apache License

@Override
public void load() {
    final List<ServerAddress> serverAddresses = new ArrayList();
    for (MongoServerAddress server : servers) {
        ServerAddress serverAddress = server.port == null ? new ServerAddress(server.hostname)
                : new ServerAddress(server.hostname, server.port);
        serverAddresses.add(serverAddress);
    }/* w w  w. j  a va 2s  .  c  om*/
    if (credentials == null || credentials.isEmpty()) {
        mongoClient = new MongoClient(serverAddresses);
    } else {
        List<MongoCredential> mongoCredentials = new ArrayList<MongoCredential>(credentials.size());
        for (MongoDbCredential credential : credentials)
            mongoCredentials.add(MongoCredential.createMongoCRCredential(credential.username,
                    credential.database, credential.password.toCharArray()));
        mongoClient = new MongoClient(serverAddresses, mongoCredentials);
    }
}

From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java

License:Open Source License

public void addServerAddress(String hostname) throws UnknownHostException {
    this.servers.add(new ServerAddress(hostname));
}

From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java

License:Open Source License

public void setServer(String hostname) throws UnknownHostException {
    theServer = new ServerAddress(hostname);
}

From source file:com.stratio.decision.configuration.MongoConfiguration.java

License:Apache License

@Bean
public MongoClient mongoClient() {

    List<ServerAddress> serverAddresses = new ArrayList();
    MongoClient mongoClient = null;//from ww  w .j  a v  a  2 s. c  o  m
    try {

        for (String mongoHost : configurationContext.getMongoHosts()) {
            String[] elements = mongoHost.split(":");
            if (elements.length < 2) {
                //no port
                serverAddresses.add(new ServerAddress(elements[0]));
            } else {
                serverAddresses.add(new ServerAddress(elements[0], Integer.parseInt(elements[1])));
            }
        }
        if (configurationContext.getMongoUsername() != null
                && configurationContext.getMongoPassword() != null) {
            mongoClient = new MongoClient(serverAddresses,
                    Arrays.asList(MongoCredential.createPlainCredential(configurationContext.getMongoUsername(),
                            "$external", configurationContext.getMongoPassword().toCharArray())));
        } else {
            log.warn(
                    "MongoDB user or password are not defined. User: [{}], Password: [{}]. trying anonymous connection.",
                    configurationContext.getMongoUsername(), configurationContext.getMongoPassword());
            mongoClient = new MongoClient(serverAddresses);
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    log.debug("Creating Spring Bean for mongoclient");
    return mongoClient;

}

From source file:com.stratio.deep.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

@Override
public Partition[] getPartitions(S config) {
    MongoClient mongoClient = null;//from w w  w.j av a2 s  . c o  m

    try {

        mongoDeepJobConfig = initConfig(config, mongoDeepJobConfig);

        DBCollection collection;
        ServerAddress address = new ServerAddress(mongoDeepJobConfig.getHost());

        List<ServerAddress> addressList = new ArrayList<>();
        addressList.add(address);
        mongoClient = new MongoClient(addressList);

        mongoClient.setReadPreference(ReadPreference.nearest());
        DB db = mongoClient.getDB(mongoDeepJobConfig.getDatabase());
        collection = db.getCollection(mongoDeepJobConfig.getCollection());
        return isShardedCollection(collection) ? calculateShardChunks(collection) : calculateSplits(collection);
    } catch (UnknownHostException e) {

        throw new DeepGenericException(e);
    } finally {
        if (mongoClient != null) {
            mongoClient.close();
        }

    }
}

From source file:com.stratio.deep.mongodb.reader.MongoReader.java

License:Apache License

/**
 * Init void./*from  w  ww. j a  v a 2  s  .  c  om*/
 *
 * @param partition the partition
 */
public void init(Partition partition) {
    try {

        List<ServerAddress> addressList = new ArrayList<>();

        for (String s : (List<String>) ((DeepPartition) partition).splitWrapper().getReplicas()) {
            addressList.add(new ServerAddress(s));
        }

        //Credentials
        List<MongoCredential> mongoCredentials = new ArrayList<>();

        if (mongoDeepJobConfig.getUsername() != null && mongoDeepJobConfig.getPassword() != null) {
            MongoCredential credential = MongoCredential.createMongoCRCredential(
                    mongoDeepJobConfig.getUsername(), mongoDeepJobConfig.getDatabase(),
                    mongoDeepJobConfig.getPassword().toCharArray());
            mongoCredentials.add(credential);

        }

        mongoClient = new MongoClient(addressList, mongoCredentials);
        mongoClient.setReadPreference(ReadPreference.valueOf(mongoDeepJobConfig.getReadPreference()));
        db = mongoClient.getDB(mongoDeepJobConfig.getDatabase());
        collection = db.getCollection(mongoDeepJobConfig.getCollection());

        dbCursor = collection.find(generateFilterQuery((MongoPartition) partition),
                mongoDeepJobConfig.getDBFields());

    } catch (UnknownHostException e) {
        throw new DeepExtractorInitializationException(e);
    }
}

From source file:com.stratio.streaming.functions.SaveToMongoActionExecutionFunction.java

License:Apache License

private MongoClient getMongoClient() throws UnknownHostException {
    if (mongoClient == null) {
        List<ServerAddress> serverAddresses = new ArrayList();
        for (String mongoHost : mongoHosts) {
            String[] elements = mongoHost.split(":");
            if (elements.length < 2) {
                //no port
                serverAddresses.add(new ServerAddress(elements[0]));
            } else {
                serverAddresses.add(new ServerAddress(elements[0], Integer.parseInt(elements[1])));
            }//from  ww  w . j a  va  2  s.  c o m
        }
        if (username != null && password != null) {
            mongoClient = new MongoClient(serverAddresses, Arrays.asList(
                    MongoCredential.createPlainCredential(username, "$external", password.toCharArray())));
        } else {
            log.warn(
                    "MongoDB user or password are not defined. User: [{}], Password: [{}]. trying anonymous connection.",
                    username, password);
            mongoClient = new MongoClient(serverAddresses);
        }
    }
    return mongoClient;
}

From source file:com.supermy.im.mongo.MongoRepository.java

License:Apache License

@Bean(name = "mongoClient")
public MongoClient mongoClient() {
    System.out.println("*******************" + mongoAddress);

    ClusterSettings clusterSettings = ClusterSettings.builder()
            .hosts(Arrays.asList(new ServerAddress(mongoAddress))).build();
    MongoCredential credential = MongoCredential.createCredential(mongoUser, mydb, mongoPasswd.toCharArray());

    MongoClientSettings settings = MongoClientSettings.builder()
            .streamFactoryFactory(new NettyStreamFactoryFactory()).clusterSettings(clusterSettings)
            .credentialList(Arrays.asList(credential)).build();

    //        MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));

    MongoClient mongoClient = MongoClients.create(settings);
    return mongoClient;
}