Failing faster when URL content is not found, howto
- by Jam
I have a thread pool that loops over a bunch of pages and checks to see if some string is there or not. If String is found, or not found response is near instant, however if server is offline or application is not running getting a rejection seems to take seconds
How can I change my code to fail faster?
for (Thread thread : pool) {
thread.start();
}
for (Thread thread : pool) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Here is my run method
@Override
public void run() {
for (Box b : boxes) {
try {
connection = new URL(b.getUrl()).openConnection();
scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter("\\Z");
content = scanner.next();
if (content.equals("YES")) {
} else {
System.out.println("\tFAILED ON " + b.getName() + " BAD APPLICATION STATE");
}
} catch (Exception ex) {
System.out.println("\tFAILED ON " + b.getName() + " BAD APPLICATION STATE");
}
}
}