Example usage for com.mongodb MongoClient setReadPreference

List of usage examples for com.mongodb MongoClient setReadPreference

Introduction

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

Prototype

@Deprecated
public void setReadPreference(final ReadPreference readPreference) 

Source Link

Document

Sets the default read preference to use for reads operations executed on any DBCollection created indirectly from this instance, via a DB instance created from #getDB(String) .

Usage

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

/** @return The {@link DBObject} representing the object with the given id */
public static DBObject getById(AbstractMongoDBServer entity, String id) {
    LOG.info("Getting {} from {}", new Object[] { id, entity });
    MongoClient mongoClient = clientForServer(entity);
    // Secondary preferred means the driver will let us read from secondaries too.
    mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
    try {//from  w w  w.  j a va2 s  . c o  m
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        return testCollection.findOne(new BasicDBObject("_id", new ObjectId(id)));
    } finally {
        mongoClient.close();
    }
}

From source file:com.edgytech.umongo.MongoPanel.java

License:Apache License

public void readWriteOptions(ButtonBase button) {
    MongoClient mongo = getMongoNode().getMongoClient();
    OptionDialog od = UMongo.instance.getGlobalStore().getOptionDialog();
    od.update(mongo.getOptions(), mongo.getWriteConcern(), mongo.getReadPreference());
    if (!od.show()) {
        return;//  w w  w.j  a va2 s. c  o  m
    }
    mongo.setOptions(od.getQueryOptions());
    mongo.setWriteConcern(od.getWriteConcern());
    mongo.setReadPreference(od.getReadPreference());
    refresh();
}

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

License:Apache License

@Override
public Partition[] getPartitions(S config) {
    MongoClient mongoClient = null;

    try {/*from ww w  .j a  v  a  2  s .  co m*/

        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:uk.ac.ebi.eva.pipeline.configuration.MongoConfiguration.java

License:Apache License

private static MongoClient getMongoClient(MongoConnection mongoConnection) throws UnknownHostException {
    String authenticationDatabase = null;
    String user = null;// w  w  w  .ja va  2 s  .  c  o  m
    String password = null;
    MongoClient mongoClient;

    // The Mongo API is not happy to deal with empty strings for authentication DB, user and password
    if (mongoConnection.getAuthenticationDatabase() != null
            && !mongoConnection.getAuthenticationDatabase().trim().isEmpty()) {
        authenticationDatabase = mongoConnection.getAuthenticationDatabase();
    }
    if (mongoConnection.getUser() != null && !mongoConnection.getUser().trim().isEmpty()) {
        user = mongoConnection.getUser();
    }
    if (mongoConnection.getPassword() != null && !mongoConnection.getPassword().trim().isEmpty()) {
        password = mongoConnection.getPassword();
    }

    if (user == null || password == null) {
        mongoClient = new MongoClient(MongoDBHelper.parseServerAddresses(mongoConnection.getHosts()));
    } else {
        mongoClient = new MongoClient(MongoDBHelper.parseServerAddresses(mongoConnection.getHosts()),
                Collections.singletonList(MongoCredential.createCredential(mongoConnection.getUser(),
                        authenticationDatabase, mongoConnection.getPassword().toCharArray())));
    }
    mongoClient.setReadPreference(mongoConnection.getReadPreference());

    return mongoClient;
}