22
22
import static java .util .concurrent .TimeUnit .NANOSECONDS ;
23
23
24
24
import com .google .common .annotations .VisibleForTesting ;
25
- import com .google .common .base .Throwables ;
26
25
import com .google .common .util .concurrent .SimpleTimeLimiter ;
27
26
import com .google .common .util .concurrent .TimeLimiter ;
28
27
import com .google .common .util .concurrent .UncheckedTimeoutException ;
34
33
import java .util .concurrent .Callable ;
35
34
import java .util .concurrent .ExecutorService ;
36
35
import java .util .concurrent .Executors ;
37
- import java .util .concurrent .ThreadFactory ;
38
36
import java .util .concurrent .TimeUnit ;
39
37
import java .util .concurrent .atomic .AtomicInteger ;
40
38
import java .util .logging .Logger ;
@@ -53,18 +51,16 @@ public class UrlChecker {
53
51
54
52
private static final AtomicInteger THREAD_COUNTER = new AtomicInteger (1 );
55
53
private static final ExecutorService THREAD_POOL = Executors
56
- .newCachedThreadPool (new ThreadFactory () {
57
- public Thread newThread (Runnable r ) {
58
- Thread t = new Thread (r , "UrlChecker-" + THREAD_COUNTER .incrementAndGet ()); // Thread safety reviewed
59
- t .setDaemon (true );
60
- return t ;
61
- }
54
+ .newCachedThreadPool (r -> {
55
+ Thread t = new Thread (r , "UrlChecker-" + THREAD_COUNTER .incrementAndGet ()); // Thread safety reviewed
56
+ t .setDaemon (true );
57
+ return t ;
62
58
});
63
59
64
60
private final TimeLimiter timeLimiter ;
65
61
66
62
public UrlChecker () {
67
- this (new SimpleTimeLimiter (THREAD_POOL ));
63
+ this (SimpleTimeLimiter . create (THREAD_POOL ));
68
64
}
69
65
70
66
@ VisibleForTesting
@@ -77,38 +73,38 @@ public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
77
73
long start = System .nanoTime ();
78
74
log .fine ("Waiting for " + Arrays .toString (urls ));
79
75
try {
80
- timeLimiter .callWithTimeout (new Callable <Void >() {
81
- public Void call () throws InterruptedException {
82
- HttpURLConnection connection = null ;
83
-
84
- long sleepMillis = MIN_POLL_INTERVAL_MS ;
85
- while (true ) {
86
- for (URL url : urls ) {
87
- try {
88
- log .fine ("Polling " + url );
89
- connection = connectToUrl (url );
90
- if (connection .getResponseCode () == HttpURLConnection .HTTP_OK ) {
91
- return null ;
92
- }
93
- } catch (IOException e ) {
94
- // Ok, try again.
95
- } finally {
96
- if (connection != null ) {
97
- connection .disconnect ();
98
- }
76
+ timeLimiter .callWithTimeout ((Callable <Void >) () -> {
77
+ HttpURLConnection connection = null ;
78
+
79
+ long sleepMillis = MIN_POLL_INTERVAL_MS ;
80
+ while (true ) {
81
+ for (URL url : urls ) {
82
+ try {
83
+ log .fine ("Polling " + url );
84
+ connection = connectToUrl (url );
85
+ if (connection .getResponseCode () == HttpURLConnection .HTTP_OK ) {
86
+ return null ;
87
+ }
88
+ } catch (IOException e ) {
89
+ // Ok, try again.
90
+ } finally {
91
+ if (connection != null ) {
92
+ connection .disconnect ();
99
93
}
100
94
}
101
- MILLISECONDS .sleep (sleepMillis );
102
- sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS ) ? sleepMillis : sleepMillis * 2 ;
103
95
}
96
+ MILLISECONDS .sleep (sleepMillis );
97
+ sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS ) ? sleepMillis : sleepMillis * 2 ;
104
98
}
105
- }, timeout , unit , true );
99
+ }, timeout , unit );
106
100
} catch (UncheckedTimeoutException e ) {
107
101
throw new TimeoutException (String .format (
108
102
"Timed out waiting for %s to be available after %d ms" ,
109
103
Arrays .toString (urls ), MILLISECONDS .convert (System .nanoTime () - start , NANOSECONDS )), e );
104
+ } catch (RuntimeException e ) {
105
+ throw e ;
110
106
} catch (Exception e ) {
111
- throw Throwables . propagate (e );
107
+ throw new RuntimeException (e );
112
108
}
113
109
}
114
110
@@ -117,37 +113,37 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
117
113
long start = System .nanoTime ();
118
114
log .fine ("Waiting for " + url );
119
115
try {
120
- timeLimiter .callWithTimeout (new Callable <Void >() {
121
- public Void call () throws InterruptedException {
122
- HttpURLConnection connection = null ;
123
-
124
- long sleepMillis = MIN_POLL_INTERVAL_MS ;
125
- while (true ) {
126
- try {
127
- log .fine ("Polling " + url );
128
- connection = connectToUrl (url );
129
- if (connection .getResponseCode () != HttpURLConnection .HTTP_OK ) {
130
- return null ;
131
- }
132
- } catch (IOException e ) {
116
+ timeLimiter .callWithTimeout ((Callable <Void >) () -> {
117
+ HttpURLConnection connection = null ;
118
+
119
+ long sleepMillis = MIN_POLL_INTERVAL_MS ;
120
+ while (true ) {
121
+ try {
122
+ log .fine ("Polling " + url );
123
+ connection = connectToUrl (url );
124
+ if (connection .getResponseCode () != HttpURLConnection .HTTP_OK ) {
133
125
return null ;
134
- } finally {
135
- if (connection != null ) {
136
- connection .disconnect ();
137
- }
138
126
}
139
-
140
- MILLISECONDS .sleep (sleepMillis );
141
- sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS ) ? sleepMillis : sleepMillis * 2 ;
127
+ } catch (IOException e ) {
128
+ return null ;
129
+ } finally {
130
+ if (connection != null ) {
131
+ connection .disconnect ();
132
+ }
142
133
}
134
+
135
+ MILLISECONDS .sleep (sleepMillis );
136
+ sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS ) ? sleepMillis : sleepMillis * 2 ;
143
137
}
144
- }, timeout , unit , true );
138
+ }, timeout , unit );
145
139
} catch (UncheckedTimeoutException e ) {
146
140
throw new TimeoutException (String .format (
147
141
"Timed out waiting for %s to become unavailable after %d ms" ,
148
142
url , MILLISECONDS .convert (System .nanoTime () - start , NANOSECONDS )), e );
143
+ } catch (RuntimeException e ) {
144
+ throw e ;
149
145
} catch (Exception e ) {
150
- throw Throwables . propagate (e );
146
+ throw new RuntimeException (e );
151
147
}
152
148
}
153
149
0 commit comments