Skip to content

Commit

Permalink
change CountDownLatch to CompletableFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
maoling committed May 21, 2019
1 parent 0cc1edd commit 52c3ad0
Showing 1 changed file with 11 additions and 11 deletions.
Expand Up @@ -16,8 +16,10 @@
*/
package org.apache.zookeeper.cli;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
Expand Down Expand Up @@ -59,27 +61,25 @@ public CliCommand parse(String[] cmdArgs) throws CliParseException {
@Override
public boolean exec() throws CliException {
String path = args[1];
CountDownLatch latch = new CountDownLatch(1);
final int[] resultCode = {-1};
CompletableFuture<Integer> cf = new CompletableFuture<>();

try {
zk.sync(path, new AsyncCallback.VoidCallback() {
public void processResult(int rc, String path, Object ctx) {
resultCode[0] = rc;
latch.countDown();
cf.complete(rc);
}
}, null);

if (latch.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) {
out.println("Sync returned " + resultCode[0]);
} else {
try {
int resultCode = cf.get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
out.println("Sync returned " + resultCode);
} catch (TimeoutException ex) {
out.println("Sync is timeout within " + CONNECTION_TIMEOUT + " ms");
}

} catch (IllegalArgumentException | InterruptedException ex) {
} catch (IllegalArgumentException | InterruptedException | ExecutionException ex) {
throw new MalformedPathException(ex.getMessage());
}


return false;
}
}

0 comments on commit 52c3ad0

Please sign in to comment.