Skip to content

Instantly share code, notes, and snippets.

@subh007
Created March 21, 2016 21:08
Show Gist options
  • Save subh007/c8495c8f0c41aefac9de to your computer and use it in GitHub Desktop.
Save subh007/c8495c8f0c41aefac9de to your computer and use it in GitHub Desktop.
Listenable callback
public class AsyncRequest{
ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(8));
public void makeRequest(String url) throws Exception{
// task to execute in executor
Callable<String> asyncTask = new Callable<String>() {
@Override
public String call() throws Exception {
// Download the url
return url;
}
};
ListenableFuture<String> future = executor.submit(asyncTask);
Futures.addCallback(future, new FutureCallback<String>() {
@Override
public void onSuccess(String result) {
System.out.println(result);
}
@Override
public void onFailure(Throwable t) {
System.out.println("got some exeception.");
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment