add check for null hosts in RestClientBuilder, so it fails early
Also delayed call to HttpAsyncClient#start so that if something goes wrong while creating the RestClient, the http client threads don't linger. In fact, if the constructor fails it is not possible to call close against the RestClient.
This commit is contained in:
parent
e5006ed7b5
commit
283090e2ae
|
@ -59,6 +59,9 @@ public final class RestClientBuilder {
|
|||
if (hosts == null || hosts.length == 0) {
|
||||
throw new IllegalArgumentException("no hosts provided");
|
||||
}
|
||||
for (HttpHost host : hosts) {
|
||||
Objects.requireNonNull(host, "host cannot be null");
|
||||
}
|
||||
this.hosts = hosts;
|
||||
}
|
||||
|
||||
|
@ -123,8 +126,9 @@ public final class RestClientBuilder {
|
|||
failureListener = new RestClient.FailureListener();
|
||||
}
|
||||
CloseableHttpAsyncClient httpClient = createHttpClient();
|
||||
RestClient restClient = new RestClient(httpClient, maxRetryTimeout, defaultHeaders, hosts, failureListener);
|
||||
httpClient.start();
|
||||
return new RestClient(httpClient, maxRetryTimeout, defaultHeaders, hosts, failureListener);
|
||||
return restClient;
|
||||
}
|
||||
|
||||
private CloseableHttpAsyncClient createHttpClient() {
|
||||
|
|
|
@ -50,12 +50,16 @@ public class RestClientBuilderTests extends RestClientTestCase {
|
|||
}
|
||||
|
||||
try {
|
||||
RestClient.builder(new HttpHost[]{new HttpHost("localhost", 9200), null}).build();
|
||||
RestClient.builder(new HttpHost("localhost", 9200), null);
|
||||
fail("should have failed");
|
||||
} catch(NullPointerException e) {
|
||||
assertEquals("host cannot be null", e.getMessage());
|
||||
}
|
||||
|
||||
try (RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build()) {
|
||||
assertNotNull(restClient);
|
||||
}
|
||||
|
||||
try {
|
||||
RestClient.builder(new HttpHost("localhost", 9200))
|
||||
.setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
|
||||
|
|
Loading…
Reference in New Issue