I have a requirement to run multiple cURL commands on Java. I have been looking around for various methodologies to accomplish this. One such mechanism is to make use of ProcessBuilder. The code I have written is given below:
private void performCurl() {
ProcessBuilder processBuilder = new ProcessBuilder();
List<String> curlArgs = getMyCurlArgs(); // curl -k -v https://www.amazon.com -H <and so on>
String listString = String.join(" ", curlArgs);
processBuilder.command(curlArgs);
processBuilder.redirectErrorStream(true);
Process proc = processBuilder.start();
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(poolSize);
Future<String> futureOpt;
try {
futureOpt = fixedThreadPool.submit(() -> {
StringBuilder sb = new StringBuilder();
InputStream ins = proc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
br.lines().forEach(sb::append);
try {
ins.close();
br.close();
} catch (IOException e) {
// my exception
}
return sb.toString();
});
boolean terminatedNormally = proc.waitFor(15, TimeUnit.SECONDS);
if (!terminatedNormally)
throw new SocketTimeoutException("Timed Out");
} finally {
fixedThreadPool.shutdown();
proc.destroy();
}
String content = futureOpt.get(); // This content is what I use.
}
Now the code above works as expected. cURL scrapes the website and provides the HTML content.
The problem is that ProcessBuilder with cURL is extremely CPU intensive. Especially given the fact that ProcessBuilder makes use of Operating System resources.
My question now is:
a) Can I use ProcessBuilder in a more efficient manner?
b) Or, are there other mechanisms to trigger parallel cURL requests on Java?
Are there any other alternatives to run cURL requests in parallel?