Mridang Agarwalla
Mridang Agarwalla

Reputation: 44998

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 HTML result.

Is there a quick way to accomplish this or do i have to go the threading way?

Thank you.

Upvotes: 4

Views: 3936

Answers (2)

BalusC
BalusC

Reputation: 1108722

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:

Upvotes: 7

laz
laz

Reputation: 28638

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.

Upvotes: 1

Related Questions