I am trying to create a web service that can spawn a crawler using com.ui4j.api.browser.BrowserFactory
(JavaFX webview) since getBrowser
method can only return one instance, as is also documented.
I am trying to run this concurrently in separate threads so that I can spawn multiple browsers at once.
Have also followed : Thread safe and concurrent use, multiple instances?
But I don't understand how can I add criteria's dynamically / create callables dynamically since all this code is running behind a Webservice made from JavaSpark
This is my code :
import static spark.Spark.get;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Runcrawl {
public static void main(String[] args) {
get("/crawl/:keyword", (req, res) - > {
ExecutorService executor = Executors.newWorkStealingPool(3);
return executor.submit(() - > {
String threadName = Thread.currentThread().getName();
System.out.println("threadName : " + threadName);
return launchBrowser();
}).get();
});
}
}
Right now I am using executor.submit().get()
, which runs fine but blocks the thread thus not letting me launch a separate browser.
Thanks, any help would be appreciated.