Creates a thread pool for resolving urls. Reads in the url file on the local filesystem. For each url it attempts to resolve it keeping a total account of the number resolved, errored, and the amount of time.
()
| 98 | * account of the number resolved, errored, and the amount of time. |
| 99 | */ |
| 100 | public void resolveUrls() { |
| 101 | |
| 102 | try { |
| 103 | |
| 104 | // create a thread pool with a fixed number of threads |
| 105 | pool = Executors.newFixedThreadPool(numThreads); |
| 106 | |
| 107 | // read in the urls file and loop through each line, one url per line |
| 108 | BufferedReader buffRead = new BufferedReader(new InputStreamReader(new FileInputStream(urlsFile), StandardCharsets.UTF_8)); |
| 109 | String urlStr = null; |
| 110 | while ((urlStr = buffRead.readLine()) != null) { |
| 111 | |
| 112 | // spin up a resolver thread per url |
| 113 | LOG.info("Starting: " + urlStr); |
| 114 | pool.execute(new ResolverThread(urlStr)); |
| 115 | } |
| 116 | |
| 117 | // close the file and wait for up to 60 seconds before shutting down |
| 118 | // the thread pool to give urls time to finish resolving |
| 119 | buffRead.close(); |
| 120 | pool.awaitTermination(60, TimeUnit.SECONDS); |
| 121 | } catch (Exception e) { |
| 122 | |
| 123 | // on error shutdown the thread pool immediately |
| 124 | pool.shutdownNow(); |
| 125 | LOG.info(StringUtils.stringifyException(e)); |
| 126 | } |
| 127 | |
| 128 | // shutdown the thread pool and log totals |
| 129 | pool.shutdown(); |
| 130 | LOG.info("Total: " + numTotal.get() + ", Resovled: " + numResolved.get() |
| 131 | + ", Errored: " + numErrored.get() + ", Average Time: " |
| 132 | + totalTime.get() / numTotal.get()); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Create a new ResolveUrls with a file from the local file system. |