I'm trying to read a string from a file, do an HTTP request with that string, and if the request returns a 200 then do another HTTP request with it.
I thought a good model for this would be the producer consumer model, but for some reason I'm totally stuck. The whole process just stops at a certain point for some reason and I have no idea why.
public static void main(String[] args) throws InterruptedException, IOException {
ArrayBlockingQueue<String> subQueue = new ArrayBlockingQueue<>(3000000);
ThreadPoolExecutor consumers = new ThreadPoolExecutor(100, 100, 10000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10));
ThreadPoolExecutor producers = new ThreadPoolExecutor(100, 100, 10000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10000000));
consumers.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
String fileName = "test";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String address = new JSONObject(line).getString("Address");
producers.submit(new Thread(() -> {
if (requestReturn200(address)) {
try {
subQueue.put(address);
} catch (InterruptedException e) {
System.out.println("Error producing.");
}
}
}));
}
producers.shutdown();
}
while (subQueue.size() != 0 || !producers.isShutdown()) {
String address = subQueue.poll(1, TimeUnit.SECONDS);
if (address != null) {
consumers.submit(new Thread(() -> {
try {
System.out.println("Doing..." + address);
doOtherHTTPReqeust(address);
} catch (Exception e) {
System.out.println("Fatal error consuming);
}
}));
} else {
System.out.println("Null");
}
}
consumers.shutdown();
}
Any and all help would be greatly appreciated.