Skip to content

Instantly share code, notes, and snippets.

@tgrall
Last active December 22, 2015 22:29
Show Gist options
  • Save tgrall/6540011 to your computer and use it in GitHub Desktop.
Save tgrall/6540011 to your computer and use it in GitHub Desktop.
Replica Read Sample Code
package com.couchbase.devday;
import com.couchbase.client.CouchbaseClient;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Ex10ReplicaRead {
public static void main(String[] args) {
System.out.println("--------------------------------------------------------------------------");
System.out.println("\tCouchbase - Replica Read");
System.out.println("--------------------------------------------------------------------------");
List<URI> uris = new LinkedList<URI>();
for(Handler h : Logger.getLogger("com.couchbase.client").getParent().getHandlers()) {
if(h instanceof ConsoleHandler) {
h.setLevel(Level.OFF);
}
}
Properties systemProperties = System.getProperties();
systemProperties.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.SunLogger");
System.setProperties(systemProperties);
Logger logger = Logger.getLogger("com.couchbase.client");
logger.setLevel(Level.OFF);
for(Handler h : logger.getParent().getHandlers()) {
if(h instanceof ConsoleHandler){
h.setLevel(Level.OFF);
}
}
uris.add(URI.create("http://vm1:8091/pools"));
uris.add(URI.create("http://vm2:8091/pools"));
CouchbaseClient cb = null;
try {
cb = new CouchbaseClient(uris, "beer-sample", "");
// loop and tes replica read
boolean replicaReadDone = false;
String key = "21st_amendment_brewery_cafe-21a_ipa";
String value = null;
// infinite loop
do {
try {
value = (String)cb.get(key);
System.out.println("Master node read");
} catch (Exception e ) {
value = (String)cb.getFromReplica(key);
System.out.println("Doing a Replica Read");
}
System.out.println("\t" + value);
System.out.println("-- -- -- --\n\n\n");
Thread.sleep(500);
} while(!replicaReadDone);
System.out.println("\n\n");
cb.shutdown(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.err.println("Error connecting to Couchbase: " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment