use of com.linkedin.d2.balancer.D2Client in project rest.li by linkedin.
the class CacheClientExample method main.
public static void main(String[] args) throws Exception {
//get client configuration
JSONObject json = parseConfig();
String zkConnectString = (String) json.get("zkConnectString");
Long zkSessionTimeout = (Long) json.get("zkSessionTimeout");
String zkBasePath = (String) json.get("zkBasePath");
Long zkStartupTimeout = (Long) json.get("zkStartupTimeout");
Long zkLoadBalancerNotificationTimeout = (Long) json.get("zkLoadBalancerNotificationTimeout");
String zkFlagFile = (String) json.get("zkFlagFile");
String fsBasePath = (String) json.get("fsBasePath");
final Map<String, Long> trafficProportion = (Map<String, Long>) json.get("trafficProportion");
final List<String> keys = (List<String>) json.get("keys");
final Long clientShutdownTimeout = (Long) json.get("clientShutdownTimeout");
final Long clientStartTimeout = (Long) json.get("clientStartTimeout");
System.out.println("Finished parsing client config");
//create d2 client
final D2Client d2Client = new D2ClientBuilder().setZkHosts(zkConnectString).setZkSessionTimeout(zkSessionTimeout, TimeUnit.MILLISECONDS).setZkStartupTimeout(zkStartupTimeout, TimeUnit.MILLISECONDS).setLbWaitTimeout(zkLoadBalancerNotificationTimeout, TimeUnit.MILLISECONDS).setFlagFile(zkFlagFile).setBasePath(zkBasePath).setFsBasePath(fsBasePath).build();
System.out.println("Finished creating d2 client, starting d2 client...");
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
final CountDownLatch latch = new CountDownLatch(1);
//start d2 client by connecting to zookeeper
startClient(d2Client, executorService, clientStartTimeout, new Callback<None>() {
@Override
public void onError(Throwable e) {
System.exit(1);
}
@Override
public void onSuccess(None result) {
latch.countDown();
}
});
latch.await();
System.out.println("D2 client is sending traffic ");
ScheduledFuture task = executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
sendTraffic(trafficProportion, d2Client, keys);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1000, TimeUnit.MILLISECONDS);
System.out.println("Press enter to shutdown");
System.out.println("===========================================================\n\n");
System.in.read();
task.cancel(false);
System.out.println("Shutting down...");
shutdown(d2Client, executorService, clientShutdownTimeout);
}
Aggregations