Created
March 21, 2016 21:08
-
-
Save subh007/c8495c8f0c41aefac9de to your computer and use it in GitHub Desktop.
Listenable callback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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