Could someone point me to snippet for making parallel web-requests? I need to make 6 web requests and concatenate the HTML 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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With