Make simultaneous web requests in Java
Could someone point me to snippet for making parallel web-requests? I need to make 6 web requests and concatenate the HT开发者_StackOverflow中文版ML result.
Is there a quick way to accomplish this or do i have to go the threading way?
Thank you.
Use ExecutorService
with Callable<InputStream>
.
Kickoff example:
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Future<InputStream> response1 = executor.submit(new Request("http://google.com"));
Future<InputStream> response2 = executor.submit(new Request("http://stackoverflow.com"));
// ...
ByteArrayOutputStream totalResponse = new ByteArrayOutputStream();
copyAndCloseInput(response1.get(), totalResponse);
copyAndCloseInput(response2.get(), totalResponse);
// ...
executor.shutdown();
with
public class Request implements Callable<InputStream> {
private String url;
public Request(String url) {
this.url = url;
}
@Override
public InputStream call() throws Exception {
return new URL(url).openStream();
}
}
See also:
- Java tutorial: Concurrency
I'd recommend learning about java.util.concurrent.ExecutorService. It allows you to run tasks simultaneously and would work well for the scenario you describe.
精彩评论