Thursday, March 20, 2014

MongoDB 2.4 - Java (Fail over)

import java.net.UnknownHostException;
import java.util.Arrays;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;

public class MongoDB {

public static void main(String[] args) throws UnknownHostException,
InterruptedException {
MongoClient client = new MongoClient(Arrays.asList(new ServerAddress(
"localhost", 27017), new ServerAddress("localhost", 27018),
new ServerAddress("localhost", 27019)));
DBCollection test = client.getDB("database")
.getCollection("collection");

test.drop();

for (int i = 0; i < Integer.MAX_VALUE; i++) {
for (int retries = 0; retries < 3; retries++) {
try {
test.insert(new BasicDBObject("_id", i));
System.out.println("Inserted document: " + i);
break;
} catch (MongoException.DuplicateKey e) {
System.out.println("Document already inserted " + i);
} catch (MongoException e) {
System.out.println(e.getMessage());
System.out.println("Retrying");
Thread.sleep(5000);
}
}

Thread.sleep(500);
}
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.