Clean up deprecated method calls in UrlChecker
Closes
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Throwables;
import com.google.common.util.concurrent.SimpleTimeLimiter;
import com.google.common.util.concurrent.TimeLimiter;
import com.google.common.util.concurrent.UncheckedTimeoutException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
private static final AtomicInteger THREAD_COUNTER = new AtomicInteger(1);
private static final ExecutorService THREAD_POOL = Executors
- .newCachedThreadPool(new ThreadFactory() {
- public Thread newThread(Runnable r) {
+ .newCachedThreadPool(r -> {
Thread t = new Thread(r, "UrlChecker-" + THREAD_COUNTER.incrementAndGet()); // Thread safety reviewed
t.setDaemon(true);
return t;
- }
});
private final TimeLimiter timeLimiter;
public UrlChecker() {
- this(new SimpleTimeLimiter(THREAD_POOL));
+ this(SimpleTimeLimiter.create(THREAD_POOL));
}
@VisibleForTesting
@@ -77,8 +73,7 @@ public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
long start = System.nanoTime();
log.fine("Waiting for " + Arrays.toString(urls));
try {
- timeLimiter.callWithTimeout(new Callable<Void>() {
- public Void call() throws InterruptedException {
+ timeLimiter.callWithTimeout((Callable<Void>) () -> {
HttpURLConnection connection = null;
long sleepMillis = MIN_POLL_INTERVAL_MS;
@@ -101,14 +96,15 @@ public Void call() throws InterruptedException {
MILLISECONDS.sleep(sleepMillis);
sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS) ? sleepMillis : sleepMillis * 2;
}
- }
- }, timeout, unit, true);
+ }, timeout, unit);
} catch (UncheckedTimeoutException e) {
throw new TimeoutException(String.format(
"Timed out waiting for %s to be available after %d ms",
Arrays.toString(urls), MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS)), e);
+ } catch (RuntimeException e) {
+ throw e;
} catch (Exception e) {
- throw Throwables.propagate(e);
+ throw new RuntimeException(e);
}
}
@@ -117,8 +113,7 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
long start = System.nanoTime();
log.fine("Waiting for " + url);
try {
- timeLimiter.callWithTimeout(new Callable<Void>() {
- public Void call() throws InterruptedException {
+ timeLimiter.callWithTimeout((Callable<Void>) () -> {
HttpURLConnection connection = null;
long sleepMillis = MIN_POLL_INTERVAL_MS;
@@ -140,14 +135,15 @@ public Void call() throws InterruptedException {
MILLISECONDS.sleep(sleepMillis);
sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS) ? sleepMillis : sleepMillis * 2;
}
- }
- }, timeout, unit, true);
+ }, timeout, unit);
} catch (UncheckedTimeoutException e) {
throw new TimeoutException(String.format(
"Timed out waiting for %s to become unavailable after %d ms",
url, MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS)), e);
+ } catch (RuntimeException e) {
+ throw e;
} catch (Exception e) {
- throw Throwables.propagate(e);
+ throw new RuntimeException(e);
}
}
