package net.autodata.jason; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.ReadPreference; import com.mongodb.ServerAddress; public class testMongoSecondayReads { public static void runTest() throws UnknownHostException { // start the clock! long start = System.currentTimeMillis(); // get the connection to our replica set. Mongo m = getConnection(); // switch to the correct database and collection. DB db = m.getDB( "jason" ); DBCollection coll = db.getCollection("big"); // just do a count of the records found... to make sure we find them all long summ = 0; // there are 10000 records with "id" of 1 thru 10000. just load every 10th one. for (int x = 1; x<=1000;x++) { BasicDBObject doc = new BasicDBObject(); doc.put("id", x*10); DBCursor cur = coll.find(doc); // what server did we read from? System.out.println(cur.getServerAddress()); summ += cur.count(); } System.out.println("Found Count : " + summ); System.out.println("Read All Time: " + (System.currentTimeMillis()-start)); } public static Mongo getConnection() throws UnknownHostException { List servers = new ArrayList(); servers.add(new ServerAddress("jkn-mdb1", 27017)); // this is master servers.add(new ServerAddress("jkn-mdb1", 27019)); servers.add(new ServerAddress("jkn-mdb2", 27017)); servers.add(new ServerAddress("jkn-mdb2", 27019)); Mongo m = new Mongo(servers); // allow read from slaves m.setReadPreference(ReadPreference.SECONDARY); //m.slaveOk(); return m; } }