Example usage for com.mongodb ReadPreference secondaryPreferred

List of usage examples for com.mongodb ReadPreference secondaryPreferred

Introduction

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

Prototype

public static ReadPreference secondaryPreferred() 

Source Link

Document

Gets a read preference that forces reads to a secondary if one is available, otherwise to the primary.

Usage

From source file:test.elasticsearch.plugin.river.mongodb.RiverMongoDBTestAsbtract.java

License:Apache License

private void initMongoInstances() throws Exception {
    logger.debug("*** initMongoInstances ***");
    CommandResult cr;//from w w  w  .  java 2s .com

    // Create 3 mongod processes
    mongodConfig1 = new MongodConfig(new GenericVersion(mongoVersion),
            new Net(mongoPort1, Network.localhostIsIPv6()), new Storage(null, REPLICA_SET_NAME, 20),
            new Timeout());
    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExe1 = starter.prepare(mongodConfig1);
    mongod1 = mongodExe1.start();
    mongodConfig2 = new MongodConfig(new GenericVersion(mongoVersion),
            new Net(mongoPort2, Network.localhostIsIPv6()), new Storage(null, REPLICA_SET_NAME, 20),
            new Timeout());
    mongodExe2 = starter.prepare(mongodConfig2);
    mongod2 = mongodExe2.start();
    mongodConfig3 = new MongodConfig(new GenericVersion(mongoVersion),
            new Net(mongoPort3, Network.localhostIsIPv6()), new Storage(null, REPLICA_SET_NAME, 20),
            new Timeout());
    mongodExe3 = starter.prepare(mongodConfig3);
    mongod3 = mongodExe3.start();
    String server1 = Network.getLocalHost().getHostName() + ":" + mongodConfig1.net().getPort();
    String server2 = Network.getLocalHost().getHostName() + ":" + mongodConfig2.net().getPort();
    String server3 = Network.getLocalHost().getHostName() + ":" + mongodConfig3.net().getPort();
    logger.debug("Server #1: {}", server1);
    logger.debug("Server #2: {}", server2);
    logger.debug("Server #3: {}", server3);
    Thread.sleep(2000);
    MongoClientOptions mco = MongoClientOptions.builder().autoConnectRetry(true).connectTimeout(15000)
            .socketTimeout(60000).build();
    mongo = new MongoClient(
            new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig1.net().getPort()), mco);
    mongoAdminDB = mongo.getDB(ADMIN_DATABASE_NAME);

    cr = mongoAdminDB.command(new BasicDBObject("isMaster", 1));
    logger.debug("isMaster: " + cr);

    // Initialize replica set
    cr = mongoAdminDB.command(new BasicDBObject("replSetInitiate",
            (DBObject) JSON.parse("{'_id': '" + REPLICA_SET_NAME + "', 'members': [{'_id': 0, 'host': '"
                    + server1 + "'}, {'_id': 1, 'host': '" + server2 + "'}, {'_id': 2, 'host': '" + server3
                    + "', 'arbiterOnly' : true}]} }")));
    logger.debug("replSetInitiate: " + cr);

    Thread.sleep(5000);
    cr = mongoAdminDB.command(new BasicDBObject("replSetGetStatus", 1));
    logger.info("replSetGetStatus: " + cr);

    // Check replica set status before to proceed
    while (!isReplicaSetStarted(cr)) {
        logger.debug("Waiting for 3 seconds...");
        Thread.sleep(1000);
        cr = mongoAdminDB.command(new BasicDBObject("replSetGetStatus", 1));
        logger.debug("replSetGetStatus: " + cr);
    }

    mongo.close();
    mongo = null;

    // Initialize a new client using all instances.
    List<ServerAddress> mongoServers = new ArrayList<ServerAddress>();
    mongoServers.add(new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig1.net().getPort()));
    mongoServers.add(new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig2.net().getPort()));
    mongoServers.add(new ServerAddress(Network.getLocalHost().getHostName(), mongodConfig3.net().getPort()));
    mongo = new MongoClient(mongoServers, mco);
    Assert.assertNotNull(mongo);
    mongo.setReadPreference(ReadPreference.secondaryPreferred());
    mongo.setWriteConcern(WriteConcern.REPLICAS_SAFE);
}